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.
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.
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
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.
Also, you can simplify the decalarion more, if you have similar hostname starts with same characters.
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.
You can also define the connection type and also user depend on the target hosts:
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.
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:
The name of the host to connect to, if different from the alias you wish to give to it.
The ssh port number, if not 22
The default ssh user name to use.
Specific to the SSH connection:
The ssh password to use (never store this variable in plain text; always use a vault)
Private key file used by ssh. Useful if using multiple keys and you don’t want to use SSH agent.
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).
This setting is always appended to the default sftp command line.
This setting is always appended to the default scp command line.
This setting is always appended to the default ssh command line.
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.
Equivalent to ansible_sudo or ansible_su, allows to force privilege escalation
Allows to set privilege escalation method
Equivalent to ansible_sudo_user or ansible_su_user, allows to set the user you become through privilege escalation
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.)
Equivalent to ansible_sudo_exe or ansible_su_exe, allows you to set the executable for the escalation method selected
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.