FreeKB - Ansible Default hosts file
Ansible - Default hosts 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 append managed nodes to the default hosts 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:

 

After choosing INI, YAML, or some other file format, you would then update your default hosts file with the list of managed nodes. You could then use the ansible command to perform a task against the hosts in your default hosts file.

ansible all -m ping

 

Or you could use the ansible-playbook command to run a playbook against the hosts in your default hosts file. As long as you do not use the -i command line option, your default hosts file will be used.

ansible-playbook foo.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 1dd7c 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   |