Ansible add_host Module: Dynamically Expand Your Inventory
Introduction
As a DevOps professional exploring Cloud and DevOps technologies, you’ll undoubtedly encounter Ansible, a powerful infrastructure automation tool. In this blog post, we will take a comprehensive look at the add_host
module, a crucial component of Ansible. With the add_host
module, you can dynamically expand your inventory during playbook execution, empowering you to scale your infrastructure with ease. We’ll cover its concepts, all the parameters it supports, and walk through step-by-step examples with explanations.
Concepts
Before we delve into practical examples, let’s grasp the essential concepts behind the add_host
module.
Dynamic Inventory
Traditionally, Ansible inventories were static files with fixed host configurations. However, dynamic inventories offer more flexibility by generating host information on the fly. The add_host
module enables you to add hosts dynamically during playbook execution, adapting to evolving infrastructure needs.
Host Variables
Host variables are key-value pairs associated with individual hosts. They enable you to customize playbook behavior for specific hosts. When using the add_host
module, you can assign host variables dynamically as you add hosts to the inventory.
Groups and Group Variables
Ansible groups organize hosts based on shared characteristics, simplifying playbook application to multiple hosts at once. With the add_host
module, you can not only add hosts but also assign them to specific groups and define group variables.
Usage and Examples
Let’s explore the parameters and options supported by the add_host
module. We’ll provide step-by-step examples to illustrate its functionality.
Syntax
The basic syntax for using the add_host
module in an Ansible playbook is as follows:
1 2 3 4 5 6 7 8 9 10 |
- name: Add a host dynamically add_host: name: groups: ansible_host: ansible_port: ansible_user: vars: |
Parameters
name
(required): The name of the host to be added to the inventory.groups
(optional): A comma-separated list of groups to which the host will be assigned. These groups must be defined in your inventory beforehand.ansible_host
(optional): The IP address or hostname of the remote host. If not specified, the value from thename
parameter will be used as the host’s name.ansible_port
(optional): The SSH port number to connect to the remote host. If not specified, Ansible will use the default SSH port (22).ansible_user
(optional): The remote user to use when connecting to the host. If not specified, Ansible will use the default user (usually the current user).ansible_password
(optional): The password for the remote user specified in theansible_user
parameter. This is useful if you need to authenticate with a password instead of using SSH keys.ansible_ssh_private_key_file
(optional): The path to the private key file to use for SSH authentication. This parameter is required if you are using SSH keys for authentication.ansible_ssh_common_args
(optional): Additional SSH command-line arguments to pass to the SSH client. For example, you can use this parameter to specify a different SSH port or enable SSH agent forwarding.ansible_ssh_extra_args
(optional): Extra SSH arguments that will be appended to the default arguments when connecting to the remote host.ansible_ssh_executable
(optional): The path to the SSH executable to use for connecting to the remote host. By default, Ansible uses the system’s default SSH executable.ansible_ssh_timeout
(optional): The timeout (in seconds) for SSH connections to the remote host. If the connection is not established within this timeout, Ansible will consider it as a failure.ansible_connection
(optional): The type of connection to use when connecting to the remote host. By default, Ansible uses SSH connections (ssh
). Other options includelocal
for running commands on the control node, orparamiko
for using the Paramiko Python SSH library.ansible_winrm_transport
(optional, Windows hosts only): The transport method to use when connecting to Windows hosts. Options includentlm
(NTLM authentication) orkerberos
(Kerberos authentication). By default, Ansible uses theplaintext
transport.ansible_winrm_server_cert_validation
(optional, Windows hosts only): Controls whether Ansible should validate the SSL certificate presented by the Windows host when using WinRM.ansible_ssh_host_key_checking
(optional): Controls whether Ansible should check the host’s SSH key against the known_hosts file. By default, Ansible sets this toTrue
.ansible_become
(optional): Enable or disable privilege escalation (become) for the remote task. Set toyes
to enable privilege escalation.ansible_become_user
(optional): The user to become when privilege escalation is enabled. By default, Ansible uses the root user.ansible_become_password
(optional): The password to use for privilege escalation. This is required ifansible_become
is set toyes
and password authentication is used for privilege escalation.ansible_become_method
(optional): The method to use for privilege escalation. By default, Ansible usessudo
. Other options includesu
,pbrun
,pfexec
, etc.ansible_become_exe
(optional): The path to the executable used for privilege escalation. By default, Ansible will use the default executable for the chosenansible_become_method
.ansible_become_flags
(optional): Extra flags to pass to the privilege escalation executable.ansible_become_pass
(optional): The password for the user specified in theansible_become_user
parameter. This is required if you need to authenticate with a password for privilege escalation.vars
(optional): A dictionary of additional variables to set for the newly added host. These variables can be used in playbooks and templates.
With this comprehensive list of parameters, you have the flexibility to tailor the add_host
module to suit your specific infrastructure and automation needs.
Example 1: Adding a Single Host
Let’s start with a simple example of adding a single host to the inventory.
1 2 3 4 5 |
- name: Add a single host dynamically add_host: name: webserver01 ansible_host: 192.168.1.100 ansible_user: myuser |
In this example, we are adding a host with the name webserver01
and the IP address 192.168.1.100
to the inventory. We also specify the remote user as myuser
.
Example 2: Adding Hosts to Multiple Groups
You can add the same host to multiple groups in the inventory. Let’s add the webserver01
to both webservers
and production
groups.
1 2 3 4 5 6 |
- name: Add a host to multiple groups dynamically add_host: name: webserver01 groups: webservers,production ansible_host: 192.168.1.100 ansible_user: myuser |
In this example, we add webserver01
to both webservers
and production
groups.
Example 3: Adding Hosts with Custom Variables
The add_host
module allows you to set custom host variables. Let’s add a host with some custom variables.
1 2 3 4 5 6 7 8 |
- name: Add a host with custom variables add_host: name: appserver01 groups: applications ansible_host: 192.168.1.101 ansible_user: myuser app_name: myapp app_version: 2.1.0 |
In this example, we add appserver01
to the applications
group and set custom variables app_name
and app_version
.
Example 4: Using vars
to Set Variables
Apart from the parameters mentioned above, you can use the vars
section to define additional variables for the newly added host.
1 2 3 4 5 6 7 8 9 |
- name: Add a host with custom variables using vars add_host: name: dbserver01 groups: databases ansible_host: 192.168.1.102 ansible_user: myuser vars: db_engine: mysql db_version: 8.0.21 |
In this example, we add dbserver01
to the databases
group and set variables db_engine
and db_version
using the vars
section.
Conclusion
The add_host
module is a powerful tool in Ansible that enables you to dynamically expand your inventory during playbook execution. By learning the concepts, parameters, and examples, you can harness its potential to scale your infrastructure effortlessly. Empower your automation journey with Ansible’s add_host
module.