About Ansible Hosts File

About Ansible Hosts File
Written by Nilima PaulFebruary 2, 2022
9 min read
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Hosts File.

About Ansible Hosts File

In ansible, have records are those documents that are utilized for putting away data about distant hubs data, which we want to make due. This record can be set anyplace however its area should be set up either in a design document or provide on the order line. This is one of those significant documents without which you will do a great deal of additional work to handle it. Without fail, ansible runs a playbook, it checks for the recorded hosts in the host's document and gets the accessible data against those has, then, at that point, utilize this data to make association, login, and execute errands on remote hosts.

What is an Ansible Hosts File?

In recently arrangement Ansible climate, however the default area for has record is/and so forth/ansible/has like underneath scrap from/and so on/ansible/ansible.cfg.

However, it tends to be changed in/and so on/ansible/ansible.cfg record, which is the default ansible design document. You can set your default has document area in it. Which can be set against the boundary "stock", Like underneath:

Likewise, in the event that you have different hosts records, you can set a catalog area against the boundary "stock" in/and so on/ansible/ansible.cfg and dwells every one of your hosts' documents in that index. Those documents will be acknowledged by Ansible in a sequential request of their record names.

Likewise, that hosts document which can be utilized from the/and so forth/ansible/ansible.cfg record, climate factors "ANSIBLE_CONFIG", the client's present working index or client's home catalog. In such cases, values set in design records have lower priority. Ansible utilizations the first "ansible.cfg" record it finds and overlooks others. Ansible looks for "ansible.cfg" in these referenced areas in the underneath request and clearly, as indicated by the design record the stock document will be acknowledged 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 enjoy different upper hands over others, however both turn out great in all cases and simple to reuse, read, and alter. In any case, I would say to utilize the ".ini" design as in this you not will undoubtedly utilize any watchwords without fail. Likewise, it is not difficult to keep up with.

Hardly any factors control the conduct of association 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


Overseeing and keeping up with the Hosts document/stock record is a significant errand in the event that you have a changing rundown of hosts. Be that as it may, underway, it is better all of the time to have a powerful stock to get the current rundown of remote hosts. Along these lines, you will continuously have constant information and won't need to go through hours to oversee and connect this information to actuals.

Ansible hosts file
Was this blog helpful?
You must be Logged in to comment
Code Block
Nilima Paul
Technology Security Analyst
201 Blog Posts
0 Discussion Threads
Trending Technologies
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Penetration Testing16

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance