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方便快捷。

相关推荐
sanggou1 小时前
Linux批量执行工具脚本使用指南:一键运行多个release-dev.sh脚本
linux·bash
亿牛云爬虫专家3 小时前
Kubernetes下的分布式采集系统设计与实战:趋势监测失效引发的架构进化
分布式·python·架构·kubernetes·爬虫代理·监测·采集
牧以南歌〆5 小时前
在Ubuntu主机中修改ARM Linux开发板的根文件系统
linux·arm开发·驱动开发·ubuntu
夜月yeyue6 小时前
设计模式分析
linux·c++·stm32·单片机·嵌入式硬件
kfepiza7 小时前
Debian的`/etc/network/interfaces`的`allow-hotplug`和`auto`对比讲解 笔记250704
linux·服务器·网络·笔记·debian
蹦蹦跳跳真可爱5897 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
nananaij7 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
雷羿 LexChien7 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt
cuijiecheng20188 小时前
Ubuntu下布署mediasoup-demo
linux·运维·ubuntu
敲键盘的小夜猫8 小时前
LLM复杂记忆存储-多会话隔离案例实战
人工智能·python·langchain