Ansible Inventory
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed some basic concepts of Ansible. We have also learned how to install and configure Ansible.
https://cloudaffaire.com/ansible-introduction/
In this blog post, we will discuss Ansible Inventory. We will also learn different aspect of inventory through a demo.
What is Ansible Inventory?
Ansible works against multiple systems in your infrastructure at the same time. It does this by selecting portions of systems listed in Ansible’s inventory, which defaults to being saved in the location /etc/ansible/hosts. You can specify a different inventory file using the -i <path> option on the command line. Not only is this inventory configurable, but you can also use multiple inventory files at the same time and pull inventory from dynamic or cloud sources or different formats (YAML, ini, etc).
Ansible Inventory Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
##----------------------- ## Ansible: Inventory ## ##----------------------- ## Systems used for this demo # hostnames ip os role # --------- ------------ -------- ------------ # system1 192.168.0.10 Centos 7 Control Node # system2 192.168.0.20 Centos 7 Managed Node One # system3 192.168.0.30 Centos 7 Managed Node Two ######################### ## Default Inventory ## ######################### ## Default inventory location sudo cat /etc/ansible/hosts ## Add new entry to the host file sudo vi /etc/ansible/hosts ------------------- [remote] 192.168.0.20 ------------------- :wq ## list all inventory ansible-inventory --list ## Test if inventory is working ansible remote -m shell -a "hostname" ######################## ## Custom Inventory ## ######################## ## You can specify custom inventory file using the -i ## Create custom inventory files vi myinventory1 ------------------- [remote] 192.168.0.20 ------------------- :wq vi myinventory2 ------------------- [remote] 192.168.0.30 ------------------- :wq ## Use custom inventory files ansible remote -m shell -a "hostname" -i /home/debjeet/myinventory1 -i /home/debjeet/myinventory2 ############################ ## Groups and subgroups ## ############################ ## Hosts in the inventory can be classified in groups and subgroups. ## Single host can be member of multiple groups and sub-groups ## Create groups and subgroups in inventory sudo vi /etc/ansible/hosts -------------------- [localserver] 192.168.0.10 [remoteserver] 192.168.0.20 192.168.0.30 [systems:children] localserver remoteserver [prod:children] systems -------------------- :wq ## Test by executing an ad-hoc command ## Will be executed in all three systems ansible -m command -a "df -h" prod ############### ## Pattern ## ############### ## Inventory also supports pattern, you can combine multiple host with similar pattern ## pattern: [start:end:gap] sudo vi /etc/ansible/hosts ------------------- [systems] 192.168.0.[10:30:10] ------------------- :wq ## Test the inventory by executing an ad-hoc command ## Will be executed iagainst 192.168.0.10, 192.168.0.20 and 192.168.0.30 ansible -m command -a "df -h" systems ################################### ## Host with non standard port ## ################################### ## Ansible supports non-standrd port ## If u want to try, on system2 (192.168.0.20), Open /etc/ssh/sshd_config file and ## look for line Port 22 and change line to Port 2222. Restart sshd. sudo vi /etc/ansible/hosts -------------------- [remote] 192.168.0.20:2222 -------------------- :wq ## Test the inventory by executing an ad-hoc command ansible -m command -a "df -h" remote ######################################### ## Change host connection properties ## ######################################### ## Apart from port, ansible also supports multiple host connaction argument ## For complete argument list https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html sudo vi /etc/ansible/hosts ------------------- remote ansible_host=192.168.0.20 ansible_port=2222 ansible_connection=ssh ansible_user=debjeet ------------------- :wq ## Execute an ad-hoc command to test ansible -m command -a "df -h" remote |
Hope you have enjoyed this article. In the next blog post, we will discuss Ansible Dynamic Inventory and will configure Ansible dynamic inventory for AWS.
To get more details on Ansible, please refer below Ansible documentation.