Ansible add_host Module: Dynamically Expand Your Inventory

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:

Parameters

  1. name (required): The name of the host to be added to the inventory.
  2. groups (optional): A comma-separated list of groups to which the host will be assigned. These groups must be defined in your inventory beforehand.
  3. ansible_host (optional): The IP address or hostname of the remote host. If not specified, the value from the name parameter will be used as the host’s name.
  4. ansible_port (optional): The SSH port number to connect to the remote host. If not specified, Ansible will use the default SSH port (22).
  5. 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).
  6. ansible_password (optional): The password for the remote user specified in the ansible_user parameter. This is useful if you need to authenticate with a password instead of using SSH keys.
  7. 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.
  8. 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.
  9. ansible_ssh_extra_args (optional): Extra SSH arguments that will be appended to the default arguments when connecting to the remote host.
  10. 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.
  11. 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.
  12. ansible_connection (optional): The type of connection to use when connecting to the remote host. By default, Ansible uses SSH connections (ssh). Other options include local for running commands on the control node, or paramiko for using the Paramiko Python SSH library.
  13. ansible_winrm_transport (optional, Windows hosts only): The transport method to use when connecting to Windows hosts. Options include ntlm (NTLM authentication) or kerberos (Kerberos authentication). By default, Ansible uses the plaintext transport.
  14. 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.
  15. 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 to True.
  16. ansible_become (optional): Enable or disable privilege escalation (become) for the remote task. Set to yes to enable privilege escalation.
  17. ansible_become_user (optional): The user to become when privilege escalation is enabled. By default, Ansible uses the root user.
  18. ansible_become_password (optional): The password to use for privilege escalation. This is required if ansible_become is set to yes and password authentication is used for privilege escalation.
  19. ansible_become_method (optional): The method to use for privilege escalation. By default, Ansible uses sudo. Other options include su, pbrun, pfexec, etc.
  20. ansible_become_exe (optional): The path to the executable used for privilege escalation. By default, Ansible will use the default executable for the chosen ansible_become_method.
  21. ansible_become_flags (optional): Extra flags to pass to the privilege escalation executable.
  22. ansible_become_pass (optional): The password for the user specified in the ansible_become_user parameter. This is required if you need to authenticate with a password for privilege escalation.
  23. 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.

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.

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.

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.

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.