Inventories

By default, Ansible looks at the /etc/ansible/hosts file for hosts specified in your playbook. As mentioned, I find it more clear to specify the host file via the -i option as we have been doing up to this point. To expand on our previous example, we can write our inventory host file as follows:

      [ubuntu]
192.168.199.170

[nexus]
192.168.199.148
192.168.199.149

[nexus:vars]
username=cisco
password=cisco

[nexus_by_name]
switch1 ansible_host=192.168.199.148
switch2 ansible_host=192.168.199.149

As you may have guessed, the square bracket headings specifies group names, so later on in the playbook, we can point to this group. For example, in cisco_1.yml and cisco_2.yml, I can act on all hosts specified under the nexus group to the group name of nexus:

    ---
- name: Configure SNMP Contact
hosts: "nexus"
gather_facts: false
connection: local
<skip>

A host can exist in more than one groups. The group can also be nested as children:

    [cisco]
router1
router2

[arista]
switch1
switch2

[datacenter:children]
cisco
arista

In the previous example, the datacenter group includes both the cisco and arista members.

We will discuss variables in the next section. But you can optionally specify variables belonging to the host and group in the inventory file as well. In our first inventory file example, [nexus:vars] specifies variables for the whole nexus group. The ansible_host variable declares variable for each of the host on the same line.

For more information on inventory file, check out the official documentation (http://docs.ansible.com/ansible/intro_inventory.html).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.135.36