Ansible最新版安装
pip方式安装
基础环境准备
(1)安装软件依赖
1 | yum install -y zlib zlib-dev zlib-devel sqlite-devel openssl-devel bzip2-devel libffi libffi-devel gcc gcc-c++ perl perl-core perl-CPAN perl-IPC-Cmd make tk-devel readline-devel libpcap-devel gdbm-devel xz-devel |
(2)下载并安装新版openssl
1 | wget https://github.com/openssl/openssl/releases/download/openssl-3.5.0/openssl-3.5.0.tar.gz |
(3)下载python3安装包
1 | curl -O https://mirrors.huaweicloud.com/python/3.13.3/Python-3.13.3.tgz |
安装Ansible
1 | python3 -m pip install ansible |
创建全局配置文件ansible.cfg
1 | mkdir -p /etc/ansible |
[root@124 ~]# ansible —version
ansible [core 2.18.4]
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/root/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /root/.local/lib/python3.13/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /root/.local/bin/ansible
python version = 3.13.3 (main, Apr 12 2025, 22:15:40) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] (/usr/bin/python3)
jinja version = 3.1.6
libyaml = True
容器方式安装
使用官方社区容器
community-ee-minimal
镜像:只包含 ansible-core
community-ee-base
镜像:包含nsible-core
、ansible.posix
、ansible.utils
、ansible.windows
的多个基础集合
本文中使用community-ee-base
镜像进行安装。
容器方式安装适用于被控主机python版本为3.13及以上的情况
(1)基本环境准备
1 | mkdir -p /data/code |
在安装ansible容器的主机上设置免密登录,并将密钥文件传至被控主机
1 | ssh-keygen |
(2)安装ghcr.io/ansible-community/community-ee-base
容器
- 方式一:直接使用docker命令:
1 | docker run -itd --name ansible --user "0" -v /root/.ssh:/root/.ssh -v /data/ansible:/etc/ansible -v /data/code:/data/code ghcr.io/ansible-community/community-ee-base |
- 方式二:使用docker compose安装
1 | #创建yml文件 |
(3)安装成功后,进入容器查看
1 | docker exec -it ansible bash |
查看版本
1 | ansible --version |
[root@123 runner]# ansible —version
ansible [core 2.18.3]
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/root/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/local/lib/python3.13/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.13.2 (main, Feb 4 2025, 00:00:00) [GCC 14.2.1 20250110 (Red Hat 14.2.1-7)] (/usr/bin/python3)
jinja version = 3.1.5
libyaml = True
执行ansible命令测试
1 | ansible all -m ping |
[root@123 code]# ansible all -m ping
192.168.1.125 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
192.168.1.124 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
构建自用Ansible镜像
(1)撰写DockerFile文件
1 | vi DockerFile |
(2)基本环境准备
1 | mkdir -p /data/code |
(3)在安装ansible容器的主机上设置免密登录,并将密钥文件传至被控主机
1 | ssh-keygen |
(4)安装容器
- 方式一:直接使用docker命令:
1 | docker run -itd --name ansible -v /root/.ssh:/root/.ssh -v /data/ansible:/etc/ansible -v /data/code:/data/code ansible:1.0 |
- 方式二:使用docker compose安装
1 | #创建yml文件 |
(5)安装成功后,进入容器查看
1 | docker exec -it ansible bash |
查看版本
1 | ansible --version |
[root@c7668aa8c849 ~]# ansible —version
ansible 2.9.27
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 14 2020, 14:45:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
执行ansible命令测试
1 | ansible all -m ping |
[root@c7668aa8c849 code]# ansible all -m ping
192.168.1.122 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
192.168.1.124 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
192.168.1.123 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}