Introduction to Ansible add_host
Ansible add_host is an Ansible module which enables us to add hosts and groups dynamically in the in-memory inventory of Ansible playbook during its execution. In this way, those hosts or groups can be used as target in next plays in same playbook. This sounds confusing but this is a wonderful feature which 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 when server is just built and configured by 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 for 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 release or updates.
- groups: This is the name of group to which host will be added dynamically. For this parameter, usable aliases can be groupname and group.
- name: This is the name or IP address of the host which needs to be added to 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 below points while working with this Ansible module add_host:
- This module can work on Microsoft Windows target machines.
- 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 Ansible core team.
- This module can accept 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 for one time for full play and work similarly. The use cases for this are though limited, but everyone of us will face it, at some point in time at our administration job.
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 our lab, we used for testing purpose. 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 below, where at the beginning a host group named serverlist is mentioned, this host group only contains one host.
Which can be checked like below:
Code:
ansible serverlist --list-hosts
Output:
Then in this playbook, in 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 afterwards the other plays will consider the host-two as target. To check the realized host list, we are using Ansible ping module, which is used to check the connectivity and authentication on target remote machines.
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 below output, here we can see that at first when host list was taken from inventory file, then system had targeted only one host which is host-one. Then after adding host-two dynamically using Ansible add_host module, in 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 variable that contains values for new host, host group to which new host will be added, port of communication to this host. We then import this variable file in our main playbook. Then using the variable values in Ansible module add_host to define the new host. Afterwards plays in this playbook will consider this host-two as well for target machine’s list.
Then running this playbook like 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 first play, but after adding host-two, in second play, this service is restarted on both hosts and also the variable defined under 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 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. While working with this module, you must be careful of syntax, as this does not have syntax as a normal inventory and this is defined in playbook itself.
Recommended Articles
This is a guide to Ansible add_host. Here we discuss the introduction to Ansible add_host, how does it work along with respective examples. You may also have a look at the following articles to learn more –
16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses