How to make your ansible inventory ?
Ansible is capable of working with one host or multiple hosts at the same time. But for multiple hosts its required to create a group of hosts. A file can be created to define a host or a group of hosts known as inventory. The default location of inventory is a file called '/etc/ansible/hosts'. But you can create the inventory file with any name and can call it using -i <path-to-inventory-file> while running the playbooks.
Ansible inventory file can be created using YAML or INI format. INI is the most common format and have better control while working with large number of groups. More information are available in inventory documentation.
If there is a need to assign some specific variable or some common variable to a group of hosts, it can be easily done using inventory file. Let's discus this using an example.
Let say you need to deploy 2 linux vms in vSphere platform and you have the following folder structure to keep your playbooks in terms of ansible roles, you have your variable file and a dynamic inventory file that can have your hosts and system login details.
In the above image, two directories and two files are available under linuxbackup folder.
inventory: Location to keep all variables and dynamic inventory
roles: Location to have ansible roles
linbkpvm.yaml: A playbook having the list of all ansible roles and their respective tags
Common variable file:
All the common variable that can be applied to both the VMs can be mapped in a single variable file.
VM specific variable entry can be managed under dynamic_inventory file:
- linuxbkp
- golinuxbkp
Now let's understand why we have created two different groups for the same VMs. Its because hos you are going to make the connection. To deploy VMs in vSphere, you have to start the connection from your ansible provision server where ansible & pyvmomi ansible modules are already installed. Follow the steps as mentioned in how to build your ansible provision server for vmware server and make your provision server ready.
As you can see in the above playbook, there are two types of connection method and its based on the hosts group:
- local [when starting the deployment in vSphere level]
- ssh [When making the changes in Guest Operating System level]
Hence, a specific role can be called using the defined tag and have to pass the inventory file as mentioned in the below commands:
- To deploy the backup server, use the below command:
$ ansible-playbook -i inventory linbkpvm.yaml --tags "deploy"
- To do the guest os customization, use the below command:
$ ansible-playbook -i inventory linbkpvm.yaml --tags "oscustom"
- To delete the backup server use the below command:
$ ansible-playbook -i inventory linbkpvm.yaml --tags "delete"
Comments
Post a Comment