Ansible is one of the popular Opensource Software Configuration Management tools compared with other tools. Which is mainly used for IT Automation, Provisioning, and Application Deployment purpose. Ansible will play its role with multiple managed nodes like App servers, Database, and more. It uses SSH to connect with the nodes where it needs to do configuration management. But, in order to connect with the Nodes with SSH, we need to have a proper setup of the list of nodes and its SSH credentials. Hence, we have the Ansible Inventory. In this article, we will discuss How to Create an Ansible Inventory.
Read this to know How to Install and Configure Ansible
This article can be a Cheat book for creating Ansible Inventory. We will see the Basic concepts and terms to advanced configuration with the Inventory file.
Table of Contents
Basics of Inventory.
Inventory is a File which is written in a format based on what plugin you have installed. Like INI (stands for initialization), YAML and more. Let’s see some example
etc/ansible/hosts
is the default the Ansible inventory location and it follows the INI format. This will look like:
host1.domain.com [appservers] host2.domain.com host3.domain.com [databaseservers] hostDB-a.domain.com hostDB-b.domain.com
- “
host1.domain.com
” is a hostname of a node which is in the default list- In this “
[appservers]
” is the tag name or group name of the group of nodes. - “host2.domain.com” is listed under “
[appservers]
” group and “hostDB1.domain.com
” is listed under “[databaseservers]
” group.
- In this “
- The same will be written in YAML in the following format.
all: hosts: host1.domain.com children: appservers: hosts: host2.domain.com: host3.domain.com: databaseservers: hosts: hostDB-a.domain.com: hostDB-b.domain.com:
- In this, “
all:
” covers all the hostname of the nodes listed.- “
hosts:
” under “all
” will have all ungrouped hostnames. - “
children:
” is the keyword for nesting groups. - “
appservers:
” and “databaseservers:
” are the group names of hosts listed under that.
- “
Mentioning Ranges of Hosts.
In the Ansible Inventory file, we can mention the range of the hostname can be mentioned as follows in the INI
[appservers] host[2:6].domain.com [databaseservers] hostDB-[a-n].domain.com
Here, “host[2:6]
” will be taken as host2, host3, host4, host5 and host6. This can also be alpha characters like “hostDB-[a:n]
”.
Same can be mentioned in YAML format as follows
... appservers: host[2:6].domain.com: databaseservers: hostDB-[a-n].domain.com:
Making Groups in Inventory
Either we can directly add hostnames in the Ansible Inventory files, or we can make a set by declaring the groups in a defined format. We can call the directly mentioned hostnames as the default groups.
Default Groups
We have two default groups called “all
” and “ungrouped
”. By default, all the unmentioned hostnames fall under the “all
” group. “ungrouped
” group will have all other hosts that do not comes under the “all
” group.
Groups of Groups
We can also make Group of Groups which will make variable declaration easy. Which can be written as follows
In INI:
[appservers] host2.domain.com host3.domain.com [databaseservers] hostDB-a.domain.com hostDB-b.domain.com [service-A:children] appservers databaseservers
Here “[service-A:children]
” keyword will make the group of group.
Same can be written in YAML as
children: appservers: hosts: host2.domain.com: host3.domain.com: databaseservers: hosts: hostDB-a.domain.com: hostDB-b.domain.com:
Variables in inventory
Basic addition
We can add variables in Inventory files as mentioned by below example.
In INI:
[appserver] host1.domain.com ansible_connection=ssh ansible_user=myotheruser http_port=80
In this, “ansible_connection
” is the variable name and “ssh
” is the value.
The same is mentioned In YAML as follows:
appserver: host1.domain.com: ansible_connection: ssh ansible_user: myotheruser http_port: 80
Alias names with Variables
We can also, give aliases names to the hostnames using the variables as follows.
In INI:
[appserver] serverAliasName ansible_port=8080 ansible_host=54.87.23.2.11
In YAML:
appserver: serverAliasName: ansible_port: 8080 ansible_host: 54.87.23.2.11
Adding group variables.
We can add varibles to the overall groups to avoid repeating adding variables to one by one as mentioned below
In INI:
[appserver] host2.domain.com host3.domain.com [appserver:vars] ansible_port=8080 ansible_host=54.87.23.2.11
In this “appserver:vars
” is the keyword for declaring variables for the group and under that variable like “ansible_port=8080
” can be declared.
In YAML:
appserver: hosts: host2.domain.com: host3.domain.com: vars: ansible_port: 8080 ansible_host: 54.87.23.2.11
Similarly, in YAML format, we can declare as separate keyword called “vars:
” and under that, we can mention variables as “ansible_port: 8080
”.
Organizing variables from different files
Ansible variables can be added with the separate files to manage and maintain easily. When Ansible starts running, it will search and load variables from the variable’s files from the respective paths. Say,
/etc/ansible/group_vars/appserver /etc/ansible/group_vars/databaseservers /etc/ansible/host_vars/serverAliasName
Inside the variable file’s variables can be written as YAML or JSON format.
Connection properties of hosts
In order to connect to the hosts files, we need to mention credentials and other important parameters to make the connection works. This connection properties can be written as variables.
- ansible_connection – though Ansible uses SSH connection, we need to mention the specific type of connection. Which are, “
smart
”, “SSH
”, “paramiko
”. By default, it will be smart. - ansible_host – as the name goes, it is the hostname.
- ansible_port – port number of ssh connection if it is not 22 by default.
- ansible_user – username of the ssh connection.
- ansible_password – password for the ssh connection.
- ansible_ssh_private_key_file – path to the private key to make an ssh connection to multiple hosts
- ansible_ssh_common_args – extra arguments like sftp, scp, and ssh.
- ansible_sftp_extra_args
- ansible_scp_extra_args
- ansible_ssh_extra_args
- ansible_ssh_pipelining
- ansible_ssh_executable
- ansible_become or ansible_sudo or ansible_su – this is to make privilege connection
- ansible_become_method – mention privilege method.
- ansible_become_user – mention another privileged user
- ansible_become_password – password for privileged user
- ansible_become_flags
- ansible_python_interpreter – point the python interpreter location.
For more variable for the connection always use --help
with the Ansible tool.
Conclusion
As we discussed, Ansible is becoming one of the unavoidable IT automation tools for various purposes like configuration management, provisioning, and application deployment. As part of the initial steps, we have discussed How to Create an Ansible Inventory. In our upcoming article, we will discuss more advanced concepts and procedures of ansible. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOps, Agile, DevSecOps, and App Development.
Experienced DevSecOps Practitioner, Tech Blogger, Expertise in Designing Solutions in Public and Private Cloud. Opensource Community Contributor.
May I sugest 2 corrections:
1. change … other hosts that do not comes under the “all” group. to … other hosts that are not member of any group.
2. as an IPv4 has only 4 byte, change all IP-addresses 54.87.23.2.11 to 54.87.23.12