Overview
In Ansible, sometimes we have to apply script to A LOT of servers. Of course it isn't good idea to do it individually. We can set up inventory with groups of servers. An the Ansible engine will apply the changes to that group. Of course we need to set up inventory fail, which is simply a text file in either:
- YML Format
- INI Format
Here, we will use the INI format as I find it better to organize, please use whichever you prefer. You can think of the inventory as teh hosts file in Linux. It is also convenient as it is called the same, just it resides in: /etc/ansible/hosts
Setup
To setup inventory file, we need to go to the base location for ansible: “/etc/ansible” and create a file called “hosts” Then we can add something like this:
[kerberos_master] 192.168.50.10 [kerberos_nodes] 192.168.50.11 192.168.50.12 ansible_port=5678 [kerberos_servers:children] kerberos_master kerberos_nodes [kerberos:vars] ansible_user=vagrant ansible_password=vagrant ansible_connection=ssh
Now, that is very small inventory, but it illustrates a lot of features of the intenroy. So let's discuss them:
Groups
You can see, we have two groups:
- kerberos_master
- kerberos_nodes
And both of them are nested into 3rd group called: kerberos_servers
Variables
Furthermore, we can set variables which will affect a certain groups or a particular server. For example we have set that for all servers, the engine will use:
- User: Vagrant
- Password: Vagrant
- Connection: SSH (as they are Linux servers), but can be also winrm (if they were Windows)
Alternatively, we can set a vaiable for particular host or group, as seen here:
192.168.50.12 ansible_port=5678
In that case, the engine will ignore the default port (22) and it will use port 5678 to connect.
Check
So we can create that file:
root@DESKTOP-N65RKDP:/etc/ansible# cat hosts [kerberos_master] 192.168.50.10 [kerberos_nodes] 192.168.50.11 192.168.50.12 [kerberos:children] kerberos_master kerberos_nodes [kerberos:vars] ansible_user=vagrant ansible_password=vagrant ansible_connection=ssh
We can also execute a module against a certain group or server:
root@DESKTOP-N65RKDP:/etc/ansible# ansible kerberos -m ping 192.168.50.10 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.50.11 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.50.12 | SUCCESS => { "changed": false, "ping": "pong" } root@DESKTOP-N65RKDP:/etc/ansible#