ansible安装介绍

安装

yum 源安装

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
28
29
30
31
32
33
34
35
36
# yum 安装ansible
$ yum -y install epel-release
$ yum -y install ansible
...
Dependencies resolved.
======================================================================================================================================================================================
Package Architecture Version Repository Size
======================================================================================================================================================================================
Installing:
ansible noarch 1:7.7.0-1.el9 epel 34 M
Installing dependencies:
ansible-core aarch64 1:2.14.18-2.el9 appstream 2.6 M
git-core aarch64 2.47.3-1.el9 appstream 4.8 M
less aarch64 590-6.el9 baseos 161 k
python3-cffi aarch64 1.14.5-5.el9 baseos 257 k
python3-cryptography aarch64 36.0.1-5.el9 baseos 1.2 M
python3-packaging noarch 20.9-5.el9 appstream 77 k
python3-ply noarch 3.11-14.el9 baseos 106 k
python3-pycparser noarch 2.20-6.el9 baseos 135 k
python3-pyparsing noarch 2.4.7-9.el9 baseos 150 k
python3-pyyaml aarch64 5.4.1-6.el9 baseos 199 k
python3-resolvelib noarch 0.5.4-5.el9 appstream 34 k
python3-setuptools noarch 53.0.0-15.el9 baseos 936 k
sshpass aarch64 1.09-4.el9 appstream 27 k
...
# 确认安装
$ ansible --version
ansible [core 2.14.18]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.9/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.9.23 (main, Aug 19 2025, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-11)] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True

另外的安装方式:

ansible使用

1
2
3
4
5
6
7
8
9
10
# 指定哪些主机,通过什么模块(shell/copy/ping等), 执行什么命令
$ ansible $host_pattern -m $module_name -a $args
-m MODULE_NAME, --module-name MODULE_NAME
-a MODULE_ARGS, --args MODULE_ARGS


# 可以提前做好各个机器的免密,也可以在执行的过程中,手动指定用户名和密钥文件
# 比如: -u root --private-key=~/.ssh/id_rsa
-u REMOTE_USER, --user REMOTE_USER
--private-key PRIVATE_KEY_FILE, --key-file PRIVATE_KEY_FILE

主机清单文件

本案例以/etc/ansible/hosts 为例

  • 默认配置文件:/etc/ansible/hosts
  • 通过命令指定:-i INVENTORY, --inventory INVENTORY, --inventory-file INVENTORY /opt/hosts
  • 通过env指定:ANSIBLE_INVENTORY=/opt/hosts

demo:

1
2
3
4
5
6
7
8
[prod]
192.168.139.61

[dev]
dev-[00:100].zmq100.cn
dev.zmq100.cn
192.168.0.3
192.168.0.4

ansible简单测试

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# ping测试
$ ansible prod -m ping
192.168.139.61 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}

# 执行shell
$ ansible prod -m shell -a 'curl -s https://www.httpbin.org/status/418'
192.168.139.61 | CHANGED | rc=0 >>

-=[ teapot ]=-

_...._
.' _ _ `.
| ."` ^ `". _,
\_;`"---"`|//
| ;/
\_ _/
`"""`

# 拷贝文件
$ ansible prod -m copy -a 'src=./src.txt dest=/root backup=yes'
192.168.139.61 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/root/src.txt",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1764766526.6254685-1030-4635454059232/source",
"state": "file",
"uid": 0
}

ansible-playbook测试

查看GitHub社区

https://github.com/ansible/ansible-examples

  • 查看支持的模块: ansible-doc -l
    • -l, --list List available plugins.
  • 查看模块用法:ansible-doc -v ping
    • -v, --verbose Causes Ansible to print more debug messages.

yaml清单

1
2
3
4
5
6
7
8
9
10
- name: copy_test
hosts: all
tasks:
- name: copy_file
copy:
src: /root/src.txt
dest: /root
owner: root
group: root
mode: '0644'

执行测试

1
2
3
4
5
6
7
8
9
10
11
$ ansible-playbook -C a.yaml
-C, --check don't make any changes;

$ ansible-playbook a.yaml
PLAY [copy_test] **********************************
TASK [Gathering Facts] ****************************
ok: [192.168.139.61]
TASK [copy_file] **********************************
ok: [192.168.139.61]
PLAY RECAP ****************************************
192.168.139.61 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0