FreeKB - Ansible Create your own inventory file
Ansible - Create your own inventory file

Tasks are run against managed nodes, also known as hosts.

 

After a clean install of Ansible, inventory in ansible.cfg is commented out, like this.

#inventory = /path/to/hosts

 

In this scenario, the default hosts file is /etc/ansible/hosts. After a clean install of Ansible, the default hosts file is completely commented out. If you were to issue command ansible all -m ping, the following would be displayed.

Likewise, if you were to uncomment inventory in ansible.cfg without defining your inventory, the following would be displayed.

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

 

Managed nodes are defined in inventory. There are multiple ways to define your inventory. Refer to Ansible - Defining your inventory. One option is to create your own inventory file on your Ansible control node, which is what this article deals with. The hosts / inventory file can use different file formats. The most commonly used formats are INI and YAML. Here is an example using the INI format.

server1.example.com
server2.example.com
[linux]
server3.example.com
server4.example.com
[windows]
server5.example.com
server6.example.com

 

And here is the YAML format.

linux:
  hosts:
    server1.example.com:
    server2.example.com:
    server3.example.com:
windows:
  hosts:
    server4.example.com:
    server5.example.com:
    server6.example.com:

 

Children can be nested, like this in YAML.

usa:
  children:
    california:
      children:
        losangeles:
          server1.example.com:
          server2.example.com:

 

Or like this in INI.

[losangeles]
server1.example.com
server2.example.com

[california:children]
losangeles

[usa:children]
california

 

Let's say you create a file named inventory.yml. You could then use the ansible-playbook command with -i inventory.yml to run the foo.yml playbook against the hosts in inventory.yml.

Be aware that if you do not use the -i command line option, the default hosts file will be used.

ansible-playbook foo.yml -i inventory.yml

 

Notice that the hosts / inventory file is static, meaning it contains a predefined list of managed node, which is the antithesis of automation. You can dynamically maintain a list of managed nodes.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 7eb70 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |