Ansible – Make the controller aware of it’s clients

Before the controller can manage the clients, it first need to provide ansible an  inventory of the clients it is allowed to manage. This inventory needs to be in the form of a file called “hosts”.

You can choose where you store the hosts file. But the default location ansible will look for the hosts file is specified in ansible’s main config file:

 

[root@controller ~]# cat /etc/ansible/ansible.cfg | grep "^inventory"
inventory = /etc/ansible/hosts

In my case, my file looks like:

 

$ cat /etc/ansible/hosts
# Ungrouped hosts, specify before any group headers.
ansibleclient01.local
ansibleclient02.local

# A collection of hosts belonging to the 'clients' group
[clients]
ansibleclient01.local
ansibleclient02.local

 

 

 

After that you can check if your controller  can ping it’s clients, you can run the following command:

 

$ ansible ansibleclient01.local -m ping
ansibleclient01.local | SUCCESS => {
 "changed": false,
 "ping": "pong"
}

 

Or you can ping a group of clients:

 

$ ansible clients --module-name ping
ansibleclient01.local | SUCCESS => {
 "changed": false,
 "ping": "pong"
}
ansibleclient02.local | SUCCESS => {
 "changed": false,
 "ping": "pong"
}

Note: here the “change” is showing as false, this means the ping module hasn’t change the client’s state in anyway.

 

You can also run custom commands using the “command” module:

 

$ ansible clients --module-name command --args "cat /etc/hostname"
ansibleclient01.local | SUCCESS | rc=0 >>
ansibleclient01.local

ansibleclient02.local | SUCCESS | rc=0 >>
ansibleclient02.local

 

Notice that the “command” module requires arguements.