Updated April 4, 2023
Introduction to Ansible Hosts File
In ansible, host files are those files that are used for storing information about remote nodes information, which we need to manage. This file can be placed anywhere but its location needs to be set up either in a configuration file or give on the command line. This is one of those important files without which you will be doing a lot of extra work to tackle it. Every time, ansible runs a playbook, it checks for the listed hosts in the host’s file and fetches the available information against those hosts, then use this information to make connection, login and execute tasks on remote hosts.
What is Ansible Hosts File?
In newly setup Ansible environment, though the default location for hosts file is /etc/ansible/hosts like below snippet from /etc/ansible/ansible.cfg.
But it can be modified in /etc/ansible/ansible.cfg file, which is the default ansible configuration file. You can set your default hosts file location in it. Which can be set against the parameter “inventory”, Like below:
Also, if you have multiple hosts files, then you can set a directory location against the parameter “inventory” in /etc/ansible/ansible.cfg and resides all your hosts’ files in that directory. Those files will be realized by Ansible in alphabetical order of their file names.
Also, that hosts file which can be used from the /etc/ansible/ansible.cfg file, environment variables “ANSIBLE_CONFIG”, the user’s current working directory or user’s home directory. In such cases, values set in configuration files have lower precedence. Ansible uses the first “ansible.cfg” file it finds and ignores others. Ansible searches for “ansible.cfg” in these mentioned locations in the below order and obviously, according to the configuration file the inventory file will be realized while running playbooks.
- ANSIBLE_CONFIG (environment variable if set)
- cfg (in the current directory)
- ~/.ansible.cfg (in your home directory)
- /etc/ansible/ansible.cfg
How to Hosts File Works in Ansible?
On Ansible controller host, the default location of the hosts’ file is /etc/ansible/hosts, but if that file is missing or hosts files are available at other locations.
Then, an order of preference will be followed which is below: –
- <current directory>/hosts
- /home/user/.ansible/hosts
- /etc/ansible/hosts
There are two types of formats supported in Ansible environments
- INI (the plain text format): This is a plain file with a key name and its value. These can also be grouped based on your environment or some common property like below:
- YAML (YAML Ain’t Markup Language, which is a structural language; supports parent-children formatting):
Below facts to note in this case:
- It must start with the “all” group and contain “hosts”, “vars” and “children” entries.
- defined sub-entries of any Host entries will be treated as
- Vars entries are normal group
- Children are ‘child groups’, which can also have their own “vars” or “hosts” “children” and so
- File extension must be “.yaml” or “.yml” or “.json”.
- In this format file, Single host entry can end without “:”, but more than one entry has to be ended with “:”, like in below example
- Indentation is very important to maintain, otherwise, you will have syntax
Example: Below is the contents of the file name “abc.yaml”
Both of the above have different advantages over others, but both work fine in all cases and easy to reuse, read, and modify. But I would say to use the “.ini” format as in this you are not bound to use any keywords every time. Also, it is easy to maintain.
Few variables control the behavior of connection with remote hosts from the Ansible control server.
- ansible_host: – Name of the server you want to connect to
- ansible_connection: – This is the connection type you want to use; default is SSH, but below are the other options if you SSH don’t work in your
- local: – To deploy tasks locally on the Ansible control node
- docker: – To deploy tasks on containers on the remote host, using local Docker container client. Below are supported parameters for
- ansible_host: – Name of Docker
- ansible_user: – User must exists inside
- ansible_pasword: –Password for above
- ansible_become: – If set to true, then escalated privileges will be
- ansible_docker_extra_args: – Strings with any additional arguments understood by
- ansible_port: – Connection port number if not default 22
- ansible_user: – Username to use when connecting to remote
- ansible_password: – Password to connect to remote hosts, don’t store in plain text, use ansible vault.
- ansible_ssh_private_key_file: – Private key file to use if not using ssh-agent
- ansible_become: – To allow force privileges
- ansible_become_method: – To set privileges escalation method
- ansible_become_user: – To set the privilege user
- ansible_become_password: – To give the password for the escalated user. don’t store in plain text, use ansible
- ansible_shell_type: If don’t need to use sh shell but csh shell or
- ansible_python_interpreter: – To set the python interpreter on target machines, if there are multiple versions of python or the python executable is named other than python like 6
- ansible_*_interpreter: –Similar to above and works for anything like Ruby or
Example to Implement Ansible Hosts File
Ansible hosts file can have to be created or modified like below. Here we give some examples and then explain: –
- See in the below example, how we simply list all the remote hosts to be managed either by hostnames or IPs. If using the hostname, then DNS resolution must work, else it will fail in execution due to failure to resolve the hostname.
- A range of hosts with similar hostnames in a range can be given below.
- A group of servers can be created like below and then execution can be done on the group and all the remote nodes under this group will get the deployment.
- If the default hosts file is not used and you have created a different hosts file, then you can specify it while running ansible-playbook like below:
- For the variable which we described above in this article, we can define them in a hosts file like below. Also, note that a variable will be only used for a host or group against which the variable is defined. If for some variable is not defined for other nodes, then default values will be used.
Conclusion
Managing and maintaining the Hosts file/inventory file is an important task if you have a changing list of hosts. But in production, it is always better to have a dynamic inventory to get the current list of remote hosts. Thus, you will always have real-time data and will not have to spend hours to manage and correlate this data to actuals.
Recommended Articles
This is a guide to Ansible Hosts File. Here we discuss an introduction to Ansible Hosts, how does host file work with examples to implement. You can also go through our other related articles to learn more –