banner
云野阁

云野阁

闲云野鹤,八方逍遥

Ansible最新版本安裝

pip 方式安裝#

基礎環境準備#

(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

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 安裝包

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#

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

創建全局配置文件ansible.cfg

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 鏡像:包含ansible-coreansible.posixansible.utilsansible.windows的多個基礎集合

本文中使用community-ee-base 鏡像進行安裝。

容器方式安裝適用於被控主機 python 版本為 3.13 及以上的情況

(1)基本環境準備

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 容器的主機上設置免密登錄,並將密鑰文件傳至被控主機

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

(2)安裝ghcr.io/ansible-community/community-ee-base容器

  • 方式一:直接使用 docker 命令:
 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 安裝
#創建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)安裝成功後,進入容器查看

docker exec -it ansible bash

查看版本

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 命令測試

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

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)基本環境準備

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 容器的主機上設置免密登錄,並將密鑰文件傳至被控主機

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

(4)安裝容器

  • 方式一:直接使用 docker 命令:
 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 安裝
#創建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)安裝成功後,進入容器查看

docker exec -it ansible bash

查看版本

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 命令測試

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

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。