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

I am confused. If I want to store IP addresses, what data type is better?

When it comes to IP addresses (IPv4), common IP a...

Metaverse, drones, 5G... may become technologies worth investing in in 2022?

2022 is coming to us with the vigorous spring new...

Traditional routing, SD-WAN gives you a reminder

The advantages of SD-WAN are obvious. It can real...

China Unicom experts: 5G should embrace AI from five aspects

5G uses large-scale antenna systems and ultra-den...

Inventory: Excellent NaaS providers in 2021

NaaS, short for Network as a Service, is a servic...

Static routing or dynamic routing, an example to make it clear!

What is routing? Routing refers to the path that ...

Six tips for optimizing network security vendor integration

As the network security situation becomes increas...

5G refreshes your digital reading experience

Digital reading has unlimited potential [[400428]...

CloudCone: $17.99/year KVM-1GB/50GB/1TB/Los Angeles MC Data Center

CloudCone sent an email at the beginning of the m...

LiCloud: $16.99/year KVM-756MB/10GB/399GB/Hong Kong Data Center

In April, I shared information about LiCloud.io. ...

The battle of 1G to 5G, the communication reshuffle is about to begin

Recently, there has been a big conflict between H...

How much do you know about the development of Wi-Fi?

As someone who uses Wi-Fi every day, have you eve...

What? You still don’t know the best assistant for 5G? Come in!

who I am Hello everyone, my name is OpenStack, a ...