Deep Dive into Ansible Configuration and Host Inventory: Easily Manage Automation Tasks

Deep Dive into Ansible Configuration and Host Inventory: Easily Manage Automation Tasks

When using Ansible for automated management, the hosts inventory file and the ansible.cfg configuration file are two crucial tools. They not only help us organize and manage target hosts, create host groups, but also determine how Ansible performs tasks. To make these configurations easier to understand,

This article will explain in depth the functions and configuration methods of these two core files, and elaborate on how Ansible gradually loads these settings when executing tasks. Through this article, I hope to help you understand Ansible more deeply and improve your application capabilities in automated operation and maintenance.

1. Ansible configuration file

The default file name of the Ansible configuration file is ansible.cfg. It is usually located in one of the following four locations, and Ansible will look for and load the configuration file in order:

  • ANSIBLE_CONFIG environment variable: Ansible will first look at this environment variable, which specifies the path to the configuration file. If this variable is set, Ansible will use that file.
  • ./ansible.cfg: If ANSIBLE_CONFIG is not set, Ansible looks for ansible.cfg file in the current working directory.
  • ~/.ansible.cfg: If the file is not found in the current working directory, Ansible will continue to look for the file in the user's home directory.
  • /etc/ansible/ansible.cfg: If none of the above three locations are found, Ansible will use the default global configuration file.

Ansible configuration files use the INI format and consist of multiple sections. Common configuration sections include:

  • [defaults]: Configure common parameters for Ansible.
  • [inventory]: Configure options related to host inventory.
  • [privilege_escalation]: Configure options related to privilege escalation.
  • [paramiko_connection]: Configure SSH connection options using the Paramiko library (applicable to RHEL6 and earlier).
  • [ssh_connection]: Configure SSH connection options using the OpenSSH library (applicable to RHEL6 and later versions).
  • [persistent_connection]: Configure persistent connection options.
  • [accelerate]: Configure acceleration mode options.
  • [selinux]: Configure SELinux related options.
  • [colors]: Configure the color options for Ansible command output.
  • [diff]: Configure whether to print the difference before and after the task.

Configuration file loading order

The order in which Ansible configuration files are loaded is very important. Configurations loaded later will overwrite previous settings. The specific loading order is as follows:

  • Command line options: If a configuration file is specified on the command line via -c, Ansible will use that configuration first.
  • Environment variable ANSIBLE_CONFIG: If this environment variable is set, Ansible will use the configuration file in this path.
  • Configuration file in the working directory: Look for ansible.cfg file in the current working directory.
  • Configuration files in the user directory: If none of the above files are found, Ansible will look for ~/.ansible.cfg.
  • Global default configuration file: If the configuration file is still not found, Ansible will use the system-wide default /etc/ansible/ansible.cfg.

2. Host List File

In Ansible, the hosts inventory file is used to define and manage the target host information. For large-scale host management, manually listing the details of each host is very cumbersome. Fortunately, Ansible supports the use of abbreviated methods to define a series of consecutive hosts. Common abbreviated methods include:

1. Use range notation

If the host names or IP addresses are continuous, you can use range notation to simplify the writing of inventory files. For example, if the IP addresses are from 192.168.1.1 to 192.168.1.100, you can use the following:

 [webservers] 192.168.1.[1:100]

2. Use hostname pattern matching

If the hostname follows a certain naming convention (such as web01 to web50), you can simplify the writing by pattern matching. For example:

 [webservers] web[01:50].example.com

3. Nesting of groups

Ansible also supports nesting of groups, that is, combining multiple groups into a new group. In this way, you can manage different types of hosts more flexibly and perform cross-group tasks when necessary. For example, define two host groups group1 and group2, and combine them into a servers group:

 [group1] 192.168.1.[1:20] ansible_ssh_port=10022 ansible_user=admin ansible_ssh_pass=admin123 [group2] 192.168.1.[21:330] ansible_ssh_port=22 ansible_user=test1 ansible_ssh_pass=test@1233 [servers:children] group1 group2

In the above example, group1 and group2 define different host ranges, and these two groups are combined in the servers group. In this way, you can flexibly manage tasks.

Conclusion

Through flexible configuration files and host inventory files, Ansible provides powerful and convenient automated management functions. Understanding the loading order, overwriting rules, and abbreviations of these configuration files can help you use Ansible more efficiently for automated operation and maintenance management. I hope this article can provide you with clear ideas and help you give full play to the advantages of Ansible in your daily work.

<<:  A Preliminary Study on Microsecond-Level High-Performance Network

>>:  In 2025, what directions in the communications industry are worth paying attention to?

Recommend

6 SD-WAN trends to watch in 2020

SD-WAN reached a new inflection point in 2019. Du...

Which collaboration metrics determine the business value of an application?

The criteria for evaluating the business value of...

What secrets do you not know about the spanning tree protocol?

1. Spanning Tree Protocol (STP) Compaq was long a...

What have the three major operators done to prevent and control the epidemic?

On February 14, the Ministry of Industry and Info...

GSMA: By 2030, 5G will contribute more than $600 billion to the global economy

On February 24, the 2021 Mobile World Congress Sh...

Let’s talk about how 5G applications can empower thousands of industries

​Based on the transmission characteristics of lar...

How to better migrate data centers

Migrating a data center is no small feat, and it ...