Ansible add_host

Ansible add_host
Techiio-author
Written by Nilima PaulFebruary 2, 2022
13 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

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

Introduction to Ansible add_host

Ansible add_host is an Ansible module that empowers us to add has and bunches progressively in the in-memory stock of the Ansible playbook during its execution. Along these lines, those hosts or gatherings can be utilized as the focus in the next plays in the same playbook. This sounds confounding however this is a brilliant component that you can utilize while chipping away at new servers, which are not in stock yet. Adding to that, the adaptability is such high that you can utilize factors to address a host or gathering name. This makes your work simple when you are in circumstances when the server is recently assembled and designed by the same playbook.

What is Ansible add_host?

To work with Ansible add_host module, you should know about its satisfactory boundaries as a whole and their default esteems, as this turns out to be more significant for a module utilization perspective as you can't characterize every one of the boundaries which have default esteems, however such default esteems get acknowledged to your current circumstance due to the module's default conduct.

The following is the rundown of the relative multitude of significant boundaries and their satisfactory or default esteems. Kindly additionally note that this rundown can increment in future Ansible delivery or updates.

  • gatherings: This is the name of the gathering to which the host will be added progressively. For this boundary, usable false names can be group names and gatherings.
  • name: This is the name or IP address of the host that should be added to in-memory stock. This can incorporate a port number and a colon. For this boundary, usable false names can be hostname and host.

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

  • This module can deal with Microsoft Windows target machines.
  • Ansible add_host module will run just one time for all target has in a play.
  • For the cycle utilization of this module, utilize the with-circle development.
  • For more up-to-date forms of Ansible, this module will have reverse similarity, given by the Ansthe ible center group.
  • This module can acknowledge variable's definition under it so a host or gathering can be characterized with more subtleties, these factors can be Ansible Variables like ansible_host and ansible_port or these factors can be utilized resisted like foo, host1, and so forth
  • We can utilize args with this module to pass factors or contentions while working with it. This likewise chips away at emphases when utilized with with_items.

How do 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 find in this example, a host can be added to a previous host bunch in the in-memory stock, yet be cautious with this as it will be utilized only for one an ideal opportunity for full play and work in basically the same manner. The utilization cases for this are however restricted, yet everybody of us will confront it, sooner or later on schedule at our organization work.

Examples of Ansible add_host

Presently by utilizing models, we will see more with regards to the Ansible add_host module, which you may need to use in your everyday activities. We will take a few models, yet before going there, we should initially comprehend our lab, which we utilized for testing reasons. In the lab, there is an ansible-regulator server and two objective machines have one and host-two. We will make playbooks and run Ansible orders on the ansible-regulator hub and see the outcomes on remote hosts.

Example #1

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

Which can be checked like below:

Code:

ansible serverlist --list-hosts

Output:

blogpost

Then, at that point, in this playbook, in the first play segment, we have utilized the Ansible add_host module to add have two to the host bunch server list in the in-memory stock, and afterward, a short time later different plays will consider the host-two as a target. To check the acknowledged host list, we are utilizing the Ansible ping module, which is utilized to look at the network and confirmation on track remote machines.

Code:

---
- name: This is the first play
hosts: server list
gather_facts: no
tasks:
- name: Here we are using the Ansible Ping module to check the current host's list in the host group server list.
ping:
- name: Here we are using the Ansible add_host module to add a host to the in-memory inventory
add_host:
name: host-two
groups: server list
- hosts: server list
gather_facts: no
tasks:
- name: Again using Ansible Ping module to check the current host's list in host group server list 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 underneath yield, here we can see that at first when the host list was taken from the stock record, then, at that point, the framework had designated just one host which has one. Then, at that point, in the wake of adding host-two progressively utilizing the Ansible add_host module, in the second play we can see currently have two alongside have one is designated.

Output:

blogpost

Example #2

In this model, we have a variable record containing some factor that contains values for a new host, have a gathering to which new host will be added, port of correspondence to this host. We then, at that point, import this variable record into our fundamental playbook. Then, at that point, involving the variable qualities in Ansible module add_host to characterize the new host. A short time later plays in this playbook will consider this has two also for focus on machine's rundown.

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 the first play
hosts: server list
gather_facts: no
tasks:
- include_vars: var_file.yaml
- name: Here we are working on a target 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 the second play
hosts: server list
gather_facts: no
tasks:
- name: Here we are working on a 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 result you can see beneath, where you see that first the assistance nginx is restarted on just host-one in first play, however in the wake of adding host-two, in second play, this help is restarted on the two hosts and furthermore the variable characterized under add_host module is additionally acknowledged on have two in particular.

Code:

ansible-playbook ansible_add_host_l.yaml

Output:

blogpost

Conclusion

As we found in this article, this module gives you force of involving factors in a well proficient manner to deal with new servers or such servers which are only not in your objective hosts bunch in a stock. While working with this module, you should watch out for language structure, as this doesn't have grammar as an ordinary stock and this is characterized in playbook itself.

Ansible
Ansible add_host
Linux
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software91
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

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