Trending September 2023 # How Does Ansible Add_Host Works With Examples # Suggested October 2023 # Top 18 Popular |

Trending September 2023 # How Does Ansible Add_Host Works With Examples # Suggested October 2023 # Top 18 Popular

You are reading the article How Does Ansible Add_Host Works With Examples updated in September 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Does Ansible Add_Host Works With Examples

Introduction to Ansible add_host

Ansible add_host is an Ansible module that enables us to add hosts and groups dynamically in the in-memory inventory of the Ansible playbook during its execution. In this way, those hosts or groups can be used as targets in the next plays in the same playbook. This sounds confusing, but this is a wonderful feature that you can use when working on new servers which are not in inventory yet. Adding to that, the flexibility is such high that you can use variables to represent a host or group name. This makes your work easy when you are in situations where the server is just built and configured by the same playbook.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What is Ansible add_host?

To work with Ansible add_host module, you must be aware of all of its acceptable parameters and their default values, as this becomes more important from a module usage point of view as you can’t define all the parameters which have default values, but such default values get realized to your environment because of the module’s default behavior.

groups: This is the name of the group to which the host will be added dynamically. For this parameter, usable aliases can be group names and groups.

name: This is the name or IP address of the host which needs to be added to the in-memory inventory. This can include a port number and a colon. For this parameter, usable aliases can be hostname and host.

Also, please note the below points while working with this Ansible module add_host:

This module can work on Microsoft Windows target machines.

The ansible add_host module will run only one time for all target hosts in a play.

For the iteration use of this module, use the with-loop construct.

For newer versions of Ansible, this module will have backward compatibility provided by the Ansible core team.

This module can accept a variable’s definition under it so that a host or group can be defined with more details, these variables can be Ansible Variables like ansible_host and ansible_port, or these variables can be used defied like foo, host1, etc.

We can use args with this module to pass variables or arguments while working with it. This also works on iterations when used with with_items.

How does Ansible add_host works?

This Ansible module works and has syntax like another Ansible module, which is in YAML. So using it should not be a problem for an Ansible Administrator.

Below is the sample for the syntax:

As you can see in this sample, a host can be added to a pre-existing host group in the in-memory inventory, but be careful with this as it will be used just one time for full play and work similarly.

Examples of Ansible add_host

Now by using examples, we will see more about the Ansible add_host module, which you might have to use in your day-to-day operations. We will take some examples, but before going there, we should first understand the lab we used for testing purposes. In the lab, there is an ansible-controller server and two target machines, host-one and host-two. We will create playbooks and run Ansible commands on the ansible-controller node and see the results on remote hosts.

Example #1

In this example, we will have a playbook like the one below, where at the beginning, a host group named server list is mentioned; this host group only contains one host.


ansible serverlist --list-hosts


Then in this playbook, in the first play section, we have used Ansible add_host module to add host-two to the host group serverlist in the in-memory inventory, and then afterward, the other plays will consider host-two as the target.



Now running this playbook like below with some verbosity:

ansible-playbook ansible_add_host.yaml -v

We will get the below output; here, we can see that at first, when the host list was taken from the inventory file, the system targeted only one host, which is host-one. Then after adding host-two dynamically using Ansible add_host module, in the second play, we can see now host-two, along with host-one, is targeted.


Example #2

In this example, we have a variable file containing some variables that contain values for a new host, the host group to which the new host will be added, port of communication to this host. We then import this variable file into our main playbook. Then using the variable values in the Ansible module, add_host, to define the new host. Afterward, plays in this playbook will consider this host-two as well for the target machine’s list.

Then run this playbook like the one below with some verbosity:


ansible-playbook ansible_add_host_1.yaml -v

Contents of the variable file var_file.yaml are as below:

host_port: 2200

Contents of the playbook ansible_add_host_1.yaml are as below:


ignore_errors: yes

In the output, you can see below, where you see that first, the service nginx is restarted on only host-one in the first play, but after adding host-two, in the second play, this service is restarted on both hosts and also the variable defined under the add_host module is also realized on host-two only.


ansible-playbook ansible_add_host_l.yaml



As we saw in this article, this module gives you the power of using variables in a well-efficient way to work on new servers or such servers which are just not in your target hosts group in an inventory.

Recommended Articles

This is a guide to Ansible add_host. Here we discuss the introduction to Ansible add_host, how it works, along with respective examples. You may also have a look at the following articles to learn more –

You're reading How Does Ansible Add_Host Works With Examples

Update the detailed information about How Does Ansible Add_Host Works With Examples on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!