Ansible最新版安装

pip 方式安装

基础环境准备

（1）安装软件依赖

Copy
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

Copy
wget https://github.com/openssl/openssl/releases/download/openssl-3.5.0/openssl-3.5.0.tar.gz
tar -zxvf openssl-3.5.0.tar.gz
cd openssl-3.5.0
#编译安装
./config --prefix=/usr/local/openssl-3.5.0 --libdir=lib --openssldir=/etc/ssl
make -j1 depend && make -j8 && make install_sw

（3）下载 python3 安装包

Copy
curl -O https://mirrors.huaweicloud.com/python/3.13.3/Python-3.13.3.tgz
tar -zxvf Python-3.13.3.tgz
cd Python-3.13.3
./configure --with-openssl=/usr/local/openssl-3.5.0 --with-openssl-rpath=auto --prefix=/usr/local/python3.13
make -j8 && make altinstall
#设置软链接
ln -s /usr/local/python3.13/bin/python3.13 /usr/bin/python3
ln -s /usr/local/python3.13/bin/pip3.13 /usr/bin/pip3

##验证版本
python3 --version
pip3 --version

安装 Ansible

Copy
python3 -m pip install ansible
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
#查看版本
ansible --version

创建全局配置文件ansible.cfg

Copy
mkdir -p /etc/ansible
vi /etc/ansible/ansible.cfg
[defaults]
#主机清单存在文件
inventory=/etc/ansible/hosts
#使用的 Python解释器路径
interpreter_python = /usr/bin/python3
#查看配置文件生效
ansible --version

[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）基本环境准备

Copy
mkdir -p /data/code
mkdir -p /data/ansible
#创建全局配置文件
vi /data/ansible/ansible.cfg
[defaults]
#主机清单存在文件
inventory=/etc/ansible/hosts
interpreter_python = /usr/bin/python3

#创建配置文件
vi /data/code/ansible.cfg
[defaults]
#主机清单存在文件
inventory=/data/code/hosts
interpreter_python = /usr/bin/python3

#创建主机清单文件
vi /data/code/hosts
[webservers]
192.168.1.124
192.168.1.125

在安装 ansible 容器的主机上设置免密登录，并将密钥文件传至被控主机

Copy
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub root@受控主机ip

（2）安装ghcr.io/ansible-community/community-ee-base容器

方式一：直接使用 docker 命令：

Copy
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 安装

Copy
#创建yml文件
vi ansible.yml

services:
ansible:
image: ghcr.io/ansible-community/community-ee-base
container_name: ansible
restart: always
tty: true
stdin_open: true
volumes:
- /root/.ssh:/root/.ssh
- /data/ansible:/etc/ansible
- /data/code:/data/code
user: "0"
networks:
ansible-net:
ipv4_address: 172.20.17.11

networks:
ansible-net:
driver: bridge
ipam:
config:
- subnet: 172.20.17.0/24

#安装
docker compose -f ansible.yml up -d

（3）安装成功后，进入容器查看

Copy
docker exec -it ansible bash

查看版本

Copy
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 命令测试

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

Copy
vi DockerFile

FROM centos:7
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && yum install epel-release -y && yum install ansible -y

#创建ansible镜像
docker build -f DockerFile -t ansible:1.0 --load .

（2）基本环境准备

Copy
mkdir -p /data/code
mkdir -p /data/ansible
#创建全局配置文件
vi /data/ansible/ansible.cfg
[defaults]
#主机清单存在文件
inventory=/etc/ansible/hosts
interpreter_python = /usr/bin/python

#创建配置文件
vi /data/code/ansible.cfg
[defaults]
#主机清单存在文件
inventory=/data/code/hosts
interpreter_python = /usr/bin/python

#创建主机清单文件
vi /data/code/hosts
[webservers]
192.168.1.122
192.168.1.123
192.168.1.124

（3）在安装 ansible 容器的主机上设置免密登录，并将密钥文件传至被控主机

Copy
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub root@受控主机ip

（4）安装容器

方式一：直接使用 docker 命令：

Copy
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 安装

Copy
#创建yml文件
vi ansible.yml

services:
ansible:
image: ansible:1.0
container_name: ansible
restart: always
tty: true
stdin_open: true
volumes:
- /root/.ssh:/root/.ssh
- /data/ansible:/etc/ansible
- /data/code:/data/code
user: "0"
networks:
ansible-net:
ipv4_address: 172.20.17.11

networks:
ansible-net:
driver: bridge
ipam:
config:
- subnet: 172.20.17.0/24

#安装
docker compose -f ansible.yml up -d

（5）安装成功后，进入容器查看

Copy
docker exec -it ansible bash

查看版本

Copy
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 命令测试

Copy
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"
}