Why do we use Ansible?
To manage nodes. Nodes can be Linux/windows machines, Network Devices, Cloud, etc. So to manage that device we need to establish the connections. The information that requires to establish a connection is stored in some file, known as an inventory file.
What is Inventories/hosts file?
Ansible works against multiple managed nodes or “hosts” in your infrastructure at the same time, using a list or group of lists known as inventory.
It contains the host's details which will be operated by Ansible. They contain group and host definitions which can be referenced in Ansible playbooks or in CLI arguments from the Ansible ad-hoc CLI tool.
Inventory file eg,
---
10.0.10.50 #IP Address
mail.example.com #Domain Name
[iosdevices] #Group Name
iosv-1 #Host name
iosv-2
[iosXEdevices]
one.example.com
two.example.com
- The host can be declared individually or group-wise.
- It can be a domain name or variable name referring to some IP or domain name.
- The group name is represented in a square bracket.
- The hosts is the default file name of inventory.
- You can specify a different inventory file by updating config information.
By default, the parent inventory file ("./hosts") is called. To attach different inventory files, during the execution of the playbook use the Adhoc command “–i inventory_name”.
To change the default location of the host file mention the new file path in ansible.cfg file.
E.g: ansible-playbook –i hosts playbook_name.yml
All Available Inventory Variables.
# common Variables
ansible_host
ansible_port
ansible_user
ansible_pipelining
ansible_password
ansible_timeout
ansible_private_key_file
ansible_connection
# become
ansible_become
ansible_become_method
ansible_become_user
ansible_become_pass
ansible_become_password
ansible_become_exe
ansible_become_flags
ansible_host
ansible_port
ansible_user
ansible_pipelining
ansible_password
ansible_timeout
ansible_private_key_file
ansible_connection
# become
ansible_become
ansible_become_method
ansible_become_user
ansible_become_pass
ansible_become_password
ansible_become_exe
ansible_become_flags
# connection common
ansible_ssh_host
ansible_ssh_user
ansible_ssh_pass
ansible_ssh_port
ansible_ssh_pipelining
ansible_ssh_timeout
ansible_ssh_private_key_file
# networking modules
ansible_network_os
ansible_connection_user
# base
ansible_module_compression
ansible_shell_type
ansible_shell_executable
ansible_ssh_host
ansible_ssh_user
ansible_ssh_pass
ansible_ssh_port
ansible_ssh_pipelining
ansible_ssh_timeout
ansible_ssh_private_key_file
# networking modules
ansible_network_os
ansible_connection_user
# base
ansible_module_compression
ansible_shell_type
ansible_shell_executable
Types of inventory/hosts
- All detail in a single file(Not Recommended, it's ok with fewer hosts).
- Distributed management System(host, group directory).
1. Single file inventory.
We assign information to the host using the below syntax.
E.g: Inventory File.
---
[iosdevices]
iosv-1 ansible_host=192.168.0.51 ansible_ssh_pass=cisco ansible_user=cisco
iosv-2 ansible_host=192.168.0.52 ansible_ssh_pass=cisco ansible_user=cisco
We can also assign common fields. Using this we can eliminate redundant code.
Eg,
---
[iosdevices]
iosv-1 ansible_host=192.168.0.51
iosv-2 ansible_host=192.168.0.52
[iosdevices:vars] #Common Fields.
ansible_user=admin
ansible_ssh_pass=admin
For now, just Focus on the Syntax. Don't worry about the fields.
In the above code, the iosv-1 and iosv-2 is a hostname that is used during the execution of playbook. The common value of both fields is written with the vars keyword.
Note: Don't use space while using equals(=) for the assignment. To separate variables use space between them.
2. Distributed inventory System(host_vars/group_vars).
Everybody likes clarity. which makes it easy to manage the project and make fewer mistakes. When we write all information about the device in a single file it is complex to read and manage. To overcome this we will create a separate file for the host.
Here we have two directories host_vars and group_vars which are manipulated by default by Ansible. It is suggested not to store data(extra information) in an inventory file. Which makes the inventory look like a dump.
The information about the node which we are storing in a single file can be written somewhere else. For example, to apply a set of variables to the host iosv-1, create a blank file named iosv-1 under the location host_vars folder, like ./host_vars/iosv-1, and add information there.
For each host create a separate file. Refer to the below images.
This is the best technique to use host information. Just declare field names in the inventory file and write their variables in host_vars/host_name. The structure looks like below.
[ios_devices]
device1
device2
[ios_devices2]
iosv-2
Host information looks like below which is as per file name.
Note: The assignment variable in host files is different from an assignment in the inventory file. Take care of it.
In the above Examples, we learn to manage the host information. The functioning of the group variable is the same. You can figure it out on your own.
Note: The assignment variable in host files is different from an assignment in the inventory file.
Hope You Like it. Don't forget to give a comment.