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 文件。

核心是 ansible_ssh_useransible_ssh_pass,它允许 Ansible 直接使用用户名和密码进行 SSH 连接,从而跳过手动配置 SSH 免密(key-based authentication)

默认情况下,Ansible 连接远程主机时使用 基于 SSH 密钥(key-based authentication),也就是通过 ssh-keygen 生成密钥并手动拷贝到远程主机 ~/.ssh/authorized_keys 文件中来实现免密登录。

但如果在 hosts 文件中指定了 ansible_ssh_pass,Ansible 就会改用密码认证方式,而不会尝试使用 SSH 密钥。这就相当于你手动输入密码进行 SSH 连接一样,因此你不需要提前配置免密。

方法 1:使用 Shell 脚本

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

Shell 脚本

#!/bin/bash

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

# 清空 hosts 文件
> "$output_file"

# 写入 Ansible 组名
echo "[All-server]" >> "$output_file"

# 逐行读取 user.txt 并转换格式
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 文件已生成:$output_file"

使用说明:

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

  2. 赋予执行权限:

    chmod +x generate_hosts.sh
    
  3. 执行脚本:

    ./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 脚本

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

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

# 先清空 hosts 文件并写入 Ansible 组名
with open(output_file, 'w') as f:
    f.write("[All-server]\n")

# 读取 user.txt 并格式化内容
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("{ip} ansible_ssh_port=22 ansible_ssh_user={user} ansible_ssh_pass='{password}'\n".format(
                ip=ip, user=user, password=password
            ))

print("Ansible hosts 文件已生成: {}".format(output_file))

使用说明:

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

  2. 赋予执行权限:

    chmod +x generate_hosts.py
    
  3. 执行脚本:

    ./generate_hosts.py
    

脚本工作原理:

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

最后的运行成果:

image

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

posted @   xiao智  阅读(284)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示