Updated July 7, 2023
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.
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.
Below is the list of all the important parameters and their acceptable or default values. Please also note that this list can increase in future Ansible releases or updates.
- 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:
- name: Adding host to groups
add_host:
hostname: <hostname or ip of host>
groups:
- <group1>
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.
Code:
ansible serverlist --list-hosts
Output:
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.
Code:
---
- name : This is first play
hosts: serverlist
gather_facts: no
tasks:
- name: Here we are using Ansible Ping module to check the current hosts list in host group serverlist.
ping:
- name: Here we are using Ansible add_host module to add a host to in-memory inventory
add_host:
name: host-two
groups: serverlist
- hosts: serverlist
gather_facts: no
tasks:
- name: Again using Ansible Ping module to check the current hosts list in host group serverlist after using Ansible add_host module
ping:
Now running this playbook like below with some verbosity:
Code:
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.
Output:
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:
Code:
ansible-playbook ansible_add_host_1.yaml -v
Contents of the variable file var_file.yaml are as below:
Code:
---
new_host: host-two
host_port: 2200
Contents of the playbook ansible_add_host_1.yaml are as below:
Code:
---
- name: This is first play
hosts: serverlist
gather_facts: no
tasks:
- include_vars: var_file.yaml
- name: Here we are working on taregt system to start a service
service:
name: nginx
state: restarted
- name: Here we are adding new host
add_host:
name: '{{ new_host }}:{{ host_port }}'
groups: serverlist
var1: 22
- name: This is second play
hosts: serverlist
gather_facts: no
tasks:
- name: Here we are working on taregt system to start a service
service:
name: nginx
state: restarted
- name: Here we are printing the value of variable passed while defined host-two
debug:
msg: "variable value is {{ var1 }}"
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.
Code:
ansible-playbook ansible_add_host_l.yaml
Output:
Conclusion
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 –