Ansible Inventory Introduction – Ansible Beginner Tutorials

This tutorial explains you about the basics of Ansible Inventory, at the end of this tutorial you will be able to understand What is Ansible Inventory, how to change or create a new inventory rather than using the default inventory, how to define the hosts and host groups.In the previous posts, we have explained the below topics. Refer those links to understand this topic from basics.

1. What is Ansible, How Ansible works, Ansible Introduction, Ansible Basic Tutorials
2. Ansible Inventory Introduction – Ansible Beginner Tutorials
3. Ansible Ad hoc Commands – Ansible Tutorial for Beginners
4. Managing Ansible Configuration Files Explained with Examples
5. Understanding Ansible Playbook – Write your First Playbook
6. Ansible Roles Explained with Examples – Ansible Tutorials
7. How to use Ansible Vault to Protect Ansible Playbooks

Ansible Inventory Introduction – Ansible Beginner Tutorials

Lets get started.

Ansible Inventory Management - Ansible Tutorials for Beginners

What is Ansible Inventory?

Ansible is an automation tool which works for multiple systems in a infrastructure at the same time. So we should have system lists to manage it. we call that as a inventory. Ansible inventory file has the list of all managed host names one line per host. Inventory management is also important factor in Ansible.

oracle-server1.learnitguide.net
oracle-server2.learnitguide.net

Note : You can define your host names in ipaddress or hostname or fqdn.

By default, this ansible host inventory file is located under /etc/ansible/hosts. Hence if you run a Ansible Playbook or Ansible Ad-hoc commands, that will look the hosts groups under the default file. You can specify a different inventory file using the “-i <path>” option on the command line as shown below.

ansible-playbook -i /root/database_servers install.yml

But remember, We must enable password less SSH authentication between ansible master server host and client hosts, else you would get an error as “Failed to connect to the host via ssh: Permission denied”. Refer this link to know how to Configure SSH Passwordless Login Authentication

ALSO WATCH THIS “ANSIBLE INVENTORY MANAGEMENT” TUTORIAL VIDEO FREE ON OUR YOUTUBE CHANNEL

Hosts Groups Declaration

You can declare the different groups in single host inventory file. For example, If you have database and web servers, then we can declare the list of server names under each groups.

[database-servers]
oracle-a.learnitguide.net
oracle-b.learnitguide.net
oracle-c.learnitguide.net
oracle-d.learnitguide.net

[webservers]
web1.learnitguide.net
web2.learnitguide.net
web3.learnitguide.net

Also, you can simplify the decalarion more, if you have similar hostname starts with same characters.

[database-servers]
oracle-[a-d].learnitguide.net

[webservers]
web[1-3].learnitguide.net

Either in alphabetic format or numeric format.

Note : A host can be in more than one group.

Inventory File Parameters

By default, Ansible works on SSH port number 22. if you have different port to connect the hosts, then define the hosts as below.

web1.learnitguide.net:4301

You can also define the connection type and also user depend on the target hosts:

[webservers]
web1.learnitguide.net     ansible_connection=ssh        ansible_user=mjohn
web2.learnitguide.net     ansible_connection=ssh        ansible_user=peter

As shown above, some of the ansible parameters mentioned below can be used on need and these are commonly used parameters. You can get more parameters from ansible.org.

Host connection:
ansible_connection
Connection type to the host. This can be the name of any of ansible’s connection plugins. SSH protocol types are smart, ssh or paramiko. The default is smart.

General for all connections:
ansible_host
The name of the host to connect to, if different from the alias you wish to give to it.

ansible_port
The ssh port number, if not 22

ansible_user
The default ssh user name to use.

Specific to the SSH connection:
ansible_ssh_pass
The ssh password to use (never store this variable in plain text; always use a vault)

ansible_ssh_private_key_file
Private key file used by ssh. Useful if using multiple keys and you don’t want to use SSH agent.

ansible_ssh_common_args
This setting is always appended to the default command line for sftp, scp, and ssh. Useful to configure a ProxyCommand for a certain host (or group).

ansible_sftp_extra_args
This setting is always appended to the default sftp command line.

ansible_scp_extra_args
This setting is always appended to the default scp command line.

ansible_ssh_extra_args
This setting is always appended to the default ssh command line.

ansible_ssh_pipelining
Determines whether or not to use SSH pipelining. This can override the pipelining setting in ansible.cfg.

ansible_ssh_executable (added in version 2.2)
This setting overrides the default behavior to use the system ssh. This can override the ssh_executable setting in ansible.cfg.

Privilege escalation
ansible_become
Equivalent to ansible_sudo or ansible_su, allows to force privilege escalation

ansible_become_method
Allows to set privilege escalation method

ansible_become_user
Equivalent to ansible_sudo_user or ansible_su_user, allows to set the user you become through privilege escalation

ansible_become_pass
Equivalent to ansible_sudo_pass or ansible_su_pass, allows you to set the privilege escalation password (never store this variable in plain text; always use a vault.)

ansible_become_exe
Equivalent to ansible_sudo_exe or ansible_su_exe, allows you to set the executable for the escalation method selected

ansible_become_flags
Equivalent to ansible_sudo_flags or ansible_su_flags, allows you to set the flags passed to the selected escalation method. This can be also set globally in ansible.cfg in the sudo_flags option

Hope you have got an idea about Ansible Inventory. Going forward, we will play more with ansible tool with some test cases.

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.