@ -330,6 +331,97 @@ override any that have the same name.</p>
<p>So in other words, you can use those variables in arguments/actions as well. You might use this to name
<p>So in other words, you can use those variables in arguments/actions as well. You might use this to name
a conf.d file appropriately or something similar. Who knows?</p>
a conf.d file appropriately or something similar. Who knows?</p>
<p>So that’s the Cobbler integration support – using the cobbler script as an example, it should be trivial to adapt Ansible to pull inventory, as well as variable information, from any data source. If you create anything interesting, please share with the mailing list, and we can keep it in the source code tree for others to use.</p>
<p>So that’s the Cobbler integration support – using the cobbler script as an example, it should be trivial to adapt Ansible to pull inventory, as well as variable information, from any data source. If you create anything interesting, please share with the mailing list, and we can keep it in the source code tree for others to use.</p>
<h3>Example: AWS EC2 External Inventory Script<aclass="headerlink"href="#example-aws-ec2-external-inventory-script"title="Permalink to this headline">¶</a></h3>
<p>If you use Amazon Web Services EC2, maintaining an inventory file might not be the best approach. For this reason, you can use the <aclass="reference external"href="https://github.com/ansible/ansible/blob/devel/examples/scripts/ec2_external_inventory.py">EC2 external inventory</a> script.</p>
<p>You can use this script in one of two ways. The easiest is to use Ansible’s <ttclass="docutils literal"><spanclass="pre">-i</span></tt> command line option and specify the path to the script.</p>
<p>The second option is to copy the script to <cite>/etc/ansible/hosts</cite> and <cite>chmod +x</cite> it. You will also need to copy the <ttclass="docutils literal"><spanclass="pre">ec2.ini</span></tt> file to <cite>/etc/ansible/ec2.ini</cite>. Then you can run ansible as you would normally.</p>
<p>To successfully make an API call to AWS, you will need to configure Boto (the Python interface to AWS). There are a <aclass="reference external"href="http://docs.pythonboto.org/en/latest/boto_config_tut.html">variety of methods</a> available, but the simplest is just to export two environment variables:</p>
<p>After a few moments, you should see your entire EC2 inventory across all regions in JSON.</p>
<p>Since each region requires its own API call, if you are only using a small set of regions, feel free to edit <ttclass="docutils literal"><spanclass="pre">ec2.ini</span></tt> and list only the regions you are interested in. There are other config options in <ttclass="docutils literal"><spanclass="pre">ec2.ini</span></tt> including cache control, and destination variables.</p>
<p>At their heart, inventory files are simply a mapping from some name to a destination address. The default <ttclass="docutils literal"><spanclass="pre">ec2.ini</span></tt> settings are configured for running Ansible from outside EC2 (from your laptop for example). If you are running Ansible from within EC2, internal DNS names and IP addresses may make more sense than public DNS names. In this case, you can modify the <ttclass="docutils literal"><spanclass="pre">destination_variable</span></tt> in <ttclass="docutils literal"><spanclass="pre">ec2.ini</span></tt> to be the private DNS name of an instance. This is particularly important when running Ansible within a private subnet inside a VPC, where the only way to access an instance is via its private IP address. For VPC instances, <cite>vpc_destination_variable</cite> in <ttclass="docutils literal"><spanclass="pre">ec2.ini</span></tt> provides a means of using which ever <aclass="reference external"href="http://docs.pythonboto.org/en/latest/ref/ec2.html#module-boto.ec2.instance">boto.ec2.instance variable</a> makes the most sense for your use case.</p>
<p>The EC2 external inventory provides mappings to instances from several groups:</p>
<dlclass="docutils">
<dt>Instance ID</dt>
<dd>These are groups of one since instance IDs are unique.
<dd>Instances belong to one or more security groups. A group is created for each security group, with all characters except alphanumerics, dashes (-) converted to underscores (_). Each group is prefixed by <ttclass="docutils literal"><spanclass="pre">security_group_</span></tt>
<dd>Each instance can have a variety of key/value pairs associated with it called Tags. The most common tag key is ‘Name’, though anything is possible. Each key/value pair is its own group of instances, again with special characters converted to underscores, in the format <ttclass="docutils literal"><spanclass="pre">tag_KEY_VALUE</span></tt>
<p>When the Ansible is interacting with a specific server, the EC2 inventory script is called again with the <ttclass="docutils literal"><spanclass="pre">--host</span><spanclass="pre">HOST</span></tt> option. This looks up the HOST in the index cache to get the instance ID, and then makes an API call to AWS to get information about that specific instance. It then makes information about that instance available as variables to your playbooks. Each variable is prefixed by <ttclass="docutils literal"><spanclass="pre">ec2_</span></tt>. Here are some of the variables available:</p>
<ulclass="simple">
<li>ec2_architecture</li>
<li>ec2_description</li>
<li>ec2_dns_name</li>
<li>ec2_id</li>
<li>ec2_image_id</li>
<li>ec2_instance_type</li>
<li>ec2_ip_address</li>
<li>ec2_kernel</li>
<li>ec2_key_name</li>
<li>ec2_launch_time</li>
<li>ec2_monitored</li>
<li>ec2_ownerId</li>
<li>ec2_placement</li>
<li>ec2_platform</li>
<li>ec2_previous_state</li>
<li>ec2_private_dns_name</li>
<li>ec2_private_ip_address</li>
<li>ec2_public_dns_name</li>
<li>ec2_ramdisk</li>
<li>ec2_region</li>
<li>ec2_root_device_name</li>
<li>ec2_root_device_type</li>
<li>ec2_security_group_ids</li>
<li>ec2_security_group_names</li>
<li>ec2_spot_instance_request_id</li>
<li>ec2_state</li>
<li>ec2_state_code</li>
<li>ec2_state_reason</li>
<li>ec2_status</li>
<li>ec2_subnet_id</li>
<li>ec2_tag_Name</li>
<li>ec2_tenancy</li>
<li>ec2_virtualization_type</li>
<li>ec2_vpc_id</li>
</ul>
<p>Both <ttclass="docutils literal"><spanclass="pre">ec2_security_group_ids</span></tt> and <ttclass="docutils literal"><spanclass="pre">ec2_security_group_names</span></tt> are comma-separated lists of all security groups. Each EC2 tag is a variable in the format <ttclass="docutils literal"><spanclass="pre">ec2_tag_KEY</span></tt>.</p>
<p>To see the complete list of variables available for an instance, run the script by itself:</p>
and lots of other people (you should see our Google Analytics data). Tweet at `Michael DeHaan <http://twitter.com/laserllama>`_ or `email him <mailto:michael.dehaan@gmail.com>`_ to get your company or project listed here. (It's free!)
and lots of other people (you should see our Google Analytics data). Tweet at `Michael DeHaan <http://twitter.com/laserllama>`_ or `email him <mailto:michael.dehaan@gmail.com>`_ to get your company or project listed here. (It's free!)
<p>and lots of other people (you should see our Google Analytics data). Tweet at <aclass="reference external"href="http://twitter.com/laserllama">Michael DeHaan</a> or <aclass="reference external"href="mailto:michael.dehaan%40gmail.com">email him</a> to get your company or project listed here. (It’s free!)</p>
<p>and lots of other people (you should see our Google Analytics data). Tweet at <aclass="reference external"href="http://twitter.com/laserllama">Michael DeHaan</a> or <aclass="reference external"href="mailto:michael.dehaan%40gmail.com">email him</a> to get your company or project listed here. (It’s free!)</p>