Location>code7788 >text

Ansible Preparation for batch operations of hundreds of units

Popularity:475 ℃/2025-02-25 22:57:55

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 providedhost_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.txtAutomatically generate the content of the fileansible/hostsFiles can be implemented through Shell scripts or Python scripts. Provide two methods to generate dynamicallyansible/hostsdocument.

Method 1: Use Shell Scripts

You can use a simple shell script to readand format it as required by AnsiblehostsFile 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:

  1. Save the above script as a.shdocument(generate_hosts.sh)。

  2. Grant execution permissions:

    chmod +x generate_hosts.sh
    
  3. Execute the script:

    ./generate_hosts.sh
    

This script willThe content of the file is generated with a formatted/etc/ansible/hostsdocument.

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=" "IFSIt is the abbreviation of "Internal Field Separator", which defines the character that the shell uses as a separator when splitting a string. By default,IFSare 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

    • readThe command is used to read a line from the input and assign it to a variable.
    • -rOptions tellreadDon't escape backslashes (\), this is to avoid handling backslashes as special characters.
    • ip user passis the variable name we want to extract from each line.readEach 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:

  1. Save Python scripts as.pyfile (generate_hosts.py`).

  2. Grant execution permissions:

    chmod +x generate_hosts.py
    
  3. Execute the script:

    ./generate_hosts.py
    

How scripts work:

  • Shell scripts: ReadFile, each line contains the IP address, username, and password. It then formats and writes this information into/etc/ansible/hostsdocument.
  • Python scripts: The function is similar to that of Shell scripts, readFile, extract IP address, username and password, and output to/etc/ansible/hostsdocument.

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.