在Linux下安装Ansible
Reverse Lv4

安装工具

不管在宿主机环境还是 Docker 容器环境下,都一样,直接用包管理器安装

1
2
apt update
apt -y install ansible

or

1
2
yum update
yum -y install ansible

创建配置

自己找个路径创建配置文件就行,比如我的/apps/data/workspace/ansible

1
2
_home="/apps/data/workspace/ansible"
mkdir -p "$_home" && cd "$_home"

使用 ansible-config 在当前目录下初始化创建一个 ansible.cfg 配置,然后自行按需修改

1
ansible-config init --disabled -t all > ansible.cfg

修改配置

一般我只会改这些东西,稍微写个,look 一下这些参数的作用.

如果需要更多功能,需自行探索 ansible 的配置,目前我提供的这些都是自己用的上的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[defaults]
forks = 10
ansible_ssh_common_args = -o PreferredAuthentications=publickey,password -o PubkeyAuthentication=yes
remote_port = 22
inventory = /apps/data/workspace/ansible/hosts.ini
host_key_checking = False
stdout_callback = minimal
timeout = 10
remote_user = root
log_path = /apps/data/workspace/ansible/ansible.log.log
private_key_file = ~/.ssh/id_rsa

[ssh_connection]
pipelining = True
scp_if_ssh = True
Section Option Value Description
[defaults] forks 10 设置并行执行任务的最大进程数,允许同时处理 10 台主机以提高效率。
[defaults] ansible_ssh_common_args -o PreferredAuthentications=publickey,password -o PubkeyAuthentication=yes 指定 SSH 认证优先顺序为公钥认证,失败后尝试密码认证,并启用公钥认证。
[defaults] remote_port 22 指定默认 SSH 端口为 22,可通过 inventory 文件覆盖。
[defaults] inventory /apps/data/workspace/ansible/hosts.ini 指定默认 inventory 文件路径,定义 Ansible 管理的主机列表。
[defaults] host_key_checking False 禁用 SSH 主机密钥检查,简化初次连接(降低安全性,建议受控环境使用)。
[defaults] stdout_callback minimal 设置输出格式为简洁模式,仅显示关键信息,适合脚本化或减少日志冗余。
[defaults] timeout 10 设置 SSH 连接超时时间为 10 秒,适合网络稳定的环境。
[defaults] remote_user root 设置默认登录用户为 root,需确保远程主机允许 root 登录。
[defaults] log_path /apps/data/workspace/ansible/ansible.log.log 指定日志文件路径,记录 Ansible 运行日志,便于调试。
[defaults] private_key_file ~/.ssh/id_rsa 指定默认 SSH 私钥路径,确保文件存在且权限正确。
[ssh_connection] pipelining True 启用 SSH pipelining,通过单一连接发送多条命令,显著提高执行速度。需禁用远程主机的 requiretty。
[ssh_connection] scp_if_ssh True 启用 SCP 协议进行文件传输,通常比 SFTP 更快。

主机列表清单

那个/apps/data/workspace/ansible/hosts.ini文件内容大概这样写

其实你配置文件里面指定了密码和用户名的话,这里ansible_ssh_useransible_ssh_pass其实就可以不填写了 : )

1
2
3
4
5
6
7
8
9
10
11
[temp1]
10.10.11.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"
10.10.12.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"
10.10.13.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"
10.10.14.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"
10.10.15.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"
10.10.16.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"
10.10.17.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"
10.10.18.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"
10.10.19.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"
10.10.20.3 ansible_ssh_user="登录用户" ansible_ssh_pass="登录密码"

如何使用

直接使用 -i 指定主机清单,使用 --config 指定配置文件路径

注意注意注意!!!
如果你的 ansible 不支持–config 参数,那么你直接设置环境变量: export ANSIBLE_CONFIG=/apps/data/workspace/ansible/ansible.cfg

然后命令里面的–config 去掉,再执行就可以了

1
ansible -i /apps/data/workspace/ansible/hosts.ini --config /apps/data/workspace/ansible/ansible.cfg all -m ping
1
ansible -i /apps/data/workspace/ansible/hosts.ini  all -m ping

使用剧本

直接再工作目录下创建一个task.yaml然后自己 AI 或者自己写要实现的功能

比如我让 AI 给我写了一个 copy 脚本到目标机器执行并且得到结果的 demo 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
---
- name: Copy and execute script on remote hosts
hosts: "{{ target_group }}"
tasks:
- name: Copy script to remote host (force overwrite)
copy:
src: /apps/data/workspace/ansible/scripts/default.sh
dest: /tmp/script.sh
mode: "0755"
force: yes
register: copy_result

- name: Execute the script on remote host
command: /tmp/script.sh
register: script_result
when: copy_result is not failed

- name: Fetch script execution output
debug:
msg: "{{ script_result.stdout_lines }}"
when: script_result.stdout is defined

- name: Clean up script on remote host
file:
path: /tmp/script.sh
state: absent
when: script_result is not failed

命令使用示例,大概长这样,看一下你大概就知道怎么用了,这样的话只维护剧本就行了

1
ansible-playbook -i ./hosts.ini -e target_group=test1 ./task.yaml