Ansible Preparation for batch operations of hundreds of units
Background: Currently, there are 100 servers on the same intranet, and business programs are required to be deployed uniformly and the host name is modified. Only one document is provided
host_user.txt
, content with three columns "IP User Password".host_user.txt content example:
10.0.0.11 root xxxx
10.0.0.12 root xxxx
10.0.0.13 root xxxx
Technical Difficulties:
1. How to avoid password-free configuration of one service
2. How to avoid adding business hosts in the ansible configuration host inventory
Idea: Want to be basedhost_user.txt
Automatically generate the content of the fileansible/hosts
Files can be implemented through Shell scripts or Python scripts. Provide two methods to generate dynamicallyansible/hosts
document.
Method 1: Use Shell Scripts
You can use a simple shell script to readand format it as required by Ansible
hosts
File format.
Shell scripts
#!/bin/bash
# Define input files and output files
input_file=""
output_file="/etc/ansible/hosts"
# Clear the output file content
> $output_file
# traverse each line and format it into the required format of ansible
while IFS=" " read -r ip user pass; do
echo "$ip ansible_ssh_port=22 ansible_ssh_user=$user ansible_ssh_pass='$pass'" >> $output_file
done < "$input_file"
echo "Ansible hosts file has been generated at $output_file"
Instructions for use:
-
Save the above script as a
.sh
document(generate_hosts.sh
)。 -
Grant execution permissions:
chmod +x generate_hosts.sh
-
Execute the script:
./generate_hosts.sh
This script willThe content of the file is generated with a formatted
/etc/ansible/hosts
document.
Shell script analysis:
while IFS=" " read -r ip user pass;
Statement
This statement is used to read data from a file or standard input line by line, and to split the contents of each line into different fields by space. The specific explanation is as follows:
-
IFS=" "
:IFS
It is the abbreviation of "Internal Field Separator", which defines the character that the shell uses as a separator when splitting a string. By default,IFS
are spaces, tabs and line breaks, but here we explicitly specify as a space" "
, means to divide each line by space. -
read -r ip user pass
:-
read
The command is used to read a line from the input and assign it to a variable. -
-r
Options tellread
Don't escape backslashes (\
), this is to avoid handling backslashes as special characters. -
ip user pass
is the variable name we want to extract from each line.read
Each line will be divided by spaces, and the first part will be assigned toip
, the second part is assigned touser
, the third part is assigned topass
. If there are more than three fields in a row, the following content will be assigned topass
。
-
Method 2: Use Python scripts
Python, the following is how it is implemented through Python scripts.
Python scripts
#!/usr/bin/env python3
# Input and output file paths
input_file = ''
output_file = '/etc/ansible/hosts'
# Open and clear the output file
with open(output_file, 'w') as f:
pass # Clear the file content
# Read the input file and format it into the required format of ansible
with open(input_file, 'r') as infile:
with open(output_file, 'a') as outfile:
for line in infile:
ip, user, password = ().split()
# Write formatted content to the output file
(f"{ip} ansible_ssh_port=22 ansible_ssh_user={user} ansible_ssh_pass='{password}'\n")
print(f"Ansible hosts file has been generated at {output_file}")
Instructions for use:
-
Save Python scripts as
.py
file (generate_hosts.py`). -
Grant execution permissions:
chmod +x generate_hosts.py
-
Execute the script:
./generate_hosts.py
How scripts work:
-
Shell scripts: Read
File, each line contains the IP address, username, and password. It then formats and writes this information into
/etc/ansible/hosts
document. -
Python scripts: The function is similar to that of Shell scripts, read
File, extract IP address, username and password, and output to
/etc/ansible/hosts
document.
You can choose one of the ways to automatically generate the Ansible host manifest file and use it directly to manage 100 servers. Here I recommend using Shell to be convenient and fast.