Ansible 数百台批量操作前期准备工作

Ansible 数百台批量操作前期准备工作

背景: 当前有100台服务器在同一个内网,需要统一部署业务程序并且对主机修改主机名,只提供了一个文档host_user.txt,内容 " IP 用户 密码 " 三列。

host_user.txt 内容示例:

10.0.0.11 root xxxx

10.0.0.12 root xxxx

10.0.0.13 root xxxx

技术难点:

1、如何避免一台台服务配置免密等

2、如何避免在ansible配置主机清单中统一添加业务主机

思路: 想根据 host_user.txt 文件的内容自动生成 ansible/hosts 文件,可以通过 Shell 脚本或 Python 脚本来实现。提供两种方法来动态生成 ansible/hosts 文件。

方法 1:使用 Shell 脚本

可以使用一个简单的 Shell 脚本来读取 user.txt 并格式化成 Ansible 所需的 hosts 文件格式。

Shell 脚本

shell 复制代码
#!/bin/bash

# 定义输入文件和输出文件
input_file="user.txt"
output_file="/etc/ansible/hosts"

# 清空输出文件内容
> $output_file

# 遍历user.txt中的每一行,格式化为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"

使用说明:

  1. 将上述脚本保存为一个 .sh 文件( generate_hosts.sh)。

  2. 赋予执行权限:

    bash 复制代码
    chmod +x generate_hosts.sh
  3. 执行脚本:

    bash 复制代码
    ./generate_hosts.sh

这个脚本会根据 user.txt 文件的内容生成一个格式化好的 /etc/ansible/hosts 文件。

shell脚本分析:

while IFS=" " read -r ip user pass; 语句

这个语句用于逐行读取文件或标准输入中的数据,并将每一行的内容按空格分割成不同的字段。具体解释如下:

  • IFS=" "IFS 是 "内部字段分隔符"(Internal Field Separator)的缩写,定义了 Shell 在分割字符串时用作分隔符的字符。默认情况下,IFS 是空格、制表符和换行符,但在这里我们明确指定为一个空格 " ",意味着按空格来分割每一行。

  • read -r ip user pass

    • read 命令用于从输入中读取一行并将它分配给变量。
    • -r 选项告诉 read 不要转义反斜杠(\),这是为了避免将反斜杠作为特殊字符处理。
    • ip user pass 是我们想要从每一行中提取的变量名。read 会将每一行按空格分割,依次将第一部分赋给 ip,第二部分赋给 user,第三部分赋给 pass。如果一行有超过三个字段,后面的内容将被赋给 pass

方法 2:使用 Python 脚本

Python,以下是通过 Python 脚本实现的方式。

Python 脚本

python 复制代码
#!/usr/bin/env python3

# 输入和输出文件路径
input_file = 'user.txt'
output_file = '/etc/ansible/hosts'

# 打开并清空输出文件
with open(output_file, 'w') as f:
    pass  # 清空文件内容

# 读取输入文件并格式化为ansible所需格式
with open(input_file, 'r') as infile:
    with open(output_file, 'a') as outfile:
        for line in infile:
            ip, user, password = line.strip().split()
            # 将格式化内容写入输出文件
            outfile.write(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}")

使用说明:

  1. 将 Python 脚本保存为 .py 文件(generate_hosts.py`)。

  2. 赋予执行权限:

    bash 复制代码
    chmod +x generate_hosts.py
  3. 执行脚本:

    bash 复制代码
    ./generate_hosts.py

脚本工作原理:

  • Shell 脚本 :读取 user.txt 文件,每行包含 IP 地址、用户名和密码。然后,它将这些信息格式化并写入 /etc/ansible/hosts 文件。
  • Python 脚本 :功能和 Shell 脚本类似,读取 user.txt 文件,提取 IP 地址、用户名和密码,按照 Ansible 所需格式输出到 /etc/ansible/hosts 文件。

最后的运行成果:

可以选择其中一种方式来自动化生成 Ansible 主机清单文件,并直接使用它来管理 100 台服务器,这里我自己推荐使用Shell方便快捷。

相关推荐
zbee14 分钟前
向日葵linux端ubuntu24.04LTS安装解决方案
linux·服务器
FreakStudio20 分钟前
开源一款数据转换扩展板-FreakStudio多米诺系列
python·单片机·嵌入式·电子diy
久绊A21 分钟前
Ubuntu及其衍生系统安装Python
开发语言·python
奔跑吧邓邓子2 小时前
【Python爬虫(64)】从“听”开始:Python音频爬虫与语音数据处理全解析
开发语言·爬虫·python·音频·语音识别
陈译2 小时前
Zabbix进阶2--接入DeepSeek分析问题并提供解决方案
linux·运维·服务器·ai·zabbix
deephub3 小时前
用PyTorch从零构建 DeepSeek R1:模型架构和分步训练详解
人工智能·pytorch·python·深度学习·deepseek
致奋斗的我们3 小时前
HAProxy高级功能及配置
linux·数据库·mysql·青少年编程·负载均衡·web·haproxy
浪子西科3 小时前
【数据结构】(Python)第六章:图
开发语言·数据结构·python
起个破名想半天了3 小时前
Web自动化之Selenium添加网站Cookies实现免登录
python·selenium·cookie