Ansible部署
一、部署環境及前置操作
1、測試環境
注:主機復用原測試環境,主機hostname根據需求調整
硬件環境:N100 x86主機 Proxmox系統
軟件環境:Ubuntu 22.04.3 LTS
軟件版本:redis-7.4.0.tar.gz
主機環境:主機IP 主機名 192.168.0.150 node1 #Ansible管理節點192.168.0.151 node2 #測試節點192.168.0.152 node3 #測試節點
2、主機IP及主機名調整
#配置靜態IP
vi /etc/netplan/00-installer-config.yaml
#添加以下內容,靜態IP根據個人需求調整
network:version: 2renderer: networkdethernets:ens18:addresses: [192.168.0.150/24] # 靜態 IP 和子網掩碼dhcp4: false # 關閉 DHCProutes:- to: default # 默認路由via: 192.168.0.1 # 網關地址nameservers:addresses: [192.168.0.1, 114.114.114.114] # DNS 服務器
#生效配置
netplan apply #主機復用原測試環境,主機hostname根據需求調整
#節點1:192.168.0.150 node1
hostnamectl set-hostname node1
#節點2:192.168.0.151 node2
hostnamectl set-hostname node2
#節點3:192.168.0.152 node3
hostnamectl set-hostname node3
3、調整hosts及時間同步
#添加host配置
cat >> /etc/hosts << EOF
192.168.0.150 node1
192.168.0.151 node2
192.168.0.152 node3
EOF
#node節點主機如果是使用其他主機克隆時,注意調整/etc/hosts中127.0.1.1配置,克隆主機默認為原始主機配置#調整系統時區配置
timedatectl set-timezone Asia/Shanghai#安裝時間同步工具chrony
apt install chrony -y#添加時間同步源/etc/chrony/chrony.conf
echo "server time1.aliyun.com iburst" >> /etc/chrony/chrony.conf#啟動服務
systemctl start chrony
systemctl enable chrony#檢查運行狀態
chronyc sources -v
4、配置免密登錄
#192.168.0.150主機執行
root@node1:/etc/ansible# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:40g2VL66TKcfb0X5l3alhHZscxP2mgOhL075cocK4iA root@node1
The key's randomart image is:
+---[RSA 3072]----+
| . |
| o . o |
| . . . =. o|
| . .. * *.+|
| + S = * *+|
| o = .+ o *oo|
| E . = =o + ..o.|
| . = = ++ + . |
| =...o+ . |
+----[SHA256]-----+
#首次配置免密需要手動輸入密碼
root@node1:/etc/ansible# ssh-copy-id test@192.168.0.151
root@node1:/etc/ansible# ssh-copy-id test@192.168.0.152
二、Ansible部署
1、Ansible安裝
Ansible安裝方式有2種,具體安裝方式如下:
1.1、使用apt安裝
在線安裝:
#聯網情況下
apt update
apt install ansible -y
離線安裝:
#離線環境
#在可聯網主機創建相同環境架構本地環境/虛擬機下載安裝包及依賴
mkdir -p /data/ansible_download/ #目錄根據實際進行調整
cd /data/ansible_download/
#下載軟件包及依賴
apt download ansible $(apt-rdepends ansible | grep -v "^ " | grep -v "^lib")
root@node1:/data/ansible_download# ll
total 17128
drwxr-xr-x 2 root root 4096 Jun 3 22:13 ./
drwxr-xr-x 3 root root 4096 Jun 3 22:13 ../
-rw-r--r-- 1 root root 17530700 Apr 29 2021 ansible_2.10.7+merged+base+2.10.8+dfsg-1_all.deb
#安裝,如果有其他deb安裝包,添加到命令
apt install ./ansible_2.10.7+merged+base+2.10.8+dfsg-1_all.deb -y
安裝驗證:
#安裝驗證
root@node1:/data/ansible_download# ansible --version
ansible 2.10.8config file = Noneconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3/dist-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 3.10.12 (main, Feb 4 2025, 14:57:36) [GCC 11.4.0]
1.2、使用pip安裝
在線安裝:
#聯網情況下
apt update
apt install python3-pip -y
pip3 install ansible
#默認源安裝較慢時
pip3 install ansible --index-url https://pypi.tuna.tsinghua.edu.cn/simple
離線安裝:
#離線環境
#在可聯網主機創建相同環境架構本地環境/虛擬機下載安裝包及依賴
mkdir -p /data/ansible_download/ #目錄根據實際進行調整
cd /data/ansible_download/
#下載軟件包
pip3 download ansible
#如果默認源下載較慢,可以使用
#國內常用鏡像源
#清華:https://pypi.tuna.tsinghua.edu.cn/simple
#阿里云:https://mirrors.aliyun.com/pypi/simple/
#騰訊云:https://mirrors.cloud.tencent.com/pypi/simple
#華為云:https://repo.huaweicloud.com/repository/pypi/simple
pip3 download ansible --index-url https://pypi.tuna.tsinghua.edu.cn/simple
#安裝
pip3 install --no-index --find-links=./ ansible
安裝驗證:
#安裝驗證
root@node1:/data/ansible_download# ansible --version
ansible [core 2.17.12]config file = Noneconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/local/lib/python3.10/dist-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/local/bin/ansiblepython version = 3.10.12 (main, Feb 4 2025, 14:57:36) [GCC 11.4.0] (/usr/bin/python3)jinja version = 3.0.3libyaml = True
2、Ansible配置文件
2.1、主配置文件
位置(按優先級從高到低):
-
當前目錄下的 ./ansible.cfg
-
用戶家目錄下的 ~/.ansible.cfg
-
系統全局的 /etc/ansible/ansible.cfg
創建方法:
# 創建默認配置文件
mkdir -p /etc/ansible/
mkdir -p /etc/ansible/{inventory,group_vars,host_vars,roles,files,templates}
touch /etc/ansible/ansible.cfg#生成默認配置(配置較多,使用常用配置項即可)
ansible-config init --disabled > /etc/ansible/ansible.cfg
常用配置內容:
[defaults]
# 安全設置
# 禁用SSH主機密鑰檢查(測試環境用)
host_key_checking = False# 禁用棄用警告
deprecation_warnings = False# 性能優化
# 并發進程數
forks = 50
# 智能收集facts
gathering = smart
# 緩存facts加速后續執行
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
# 緩存24小時
fact_caching_timeout = 86400# 路徑設置
# 默認庫存文件
inventory = /etc/ansible/hosts
# 角色搜索路徑
roles_path = /etc/ansible/roles
# 日志記錄
log_path = /var/log/ansible.log[privilege_escalation]
# 默認啟用權限提升,根據需求配置
become = True
# 使用sudo
become_method = sudo
# 提升為root
become_user = root
# 不提示sudo密碼
become_ask_pass = False[ssh_connection]
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
# 啟用管道加速
pipelining = True
2.2、庫存文件 (Inventory File)
默認位置:/etc/ansible/hosts
創建方法:
#以自定義為例
mkdir -p /etc/ansible/
touch /etc/ansible/hosts
配置內容:
# 簡單主機定義
[ubuntu_servers]
192.168.0.151
192.168.0.152 ansible_port=22 # 自定義SSH端口
2.3、主機變量文件
位置:通常在庫存文件所在目錄的 host_vars/ 和 group_vars/ 子目錄中
創建方法:
mkdir -p /etc/ansible/{host_vars,group_vars}
示例:
# /etc/ansible/group_vars/all.yml
---
ansible_python_interpreter: /usr/bin/python3
timezone: UTC# /etc/ansible/host_vars/web1.example.com.yml
---
http_port: 8080
max_clients: 200
2.4、Ansible Vault 密碼文件(可選)
用于加密敏感數據:
#全局存儲
echo "my_vault_password" > /etc/ansible/vault_pass.txt
chmod 600 /etc/ansible/vault_pass.txt#用戶級存儲
#在用戶目錄下創建
mkdir -p ~/.ansible/
echo "your_vault_password" > ~/.ansible/vault_pass
chmod 600 ~/.ansible/vault_pass
2.5、配置文件優先級
Ansible 按以下順序查找配置文件:
-
ANSIBLE_CONFIG 環境變量指定的文件
-
當前目錄下的 ansible.cfg
-
用戶家目錄下的 ~/.ansible.cfg
-
/etc/ansible/ansible.cfg
推薦目錄結構如下:
ansible_project/
├── ansible.cfg # 項目級配置
├── inventory/ # 庫存目錄
│ ├── hosts # 主庫存文件
│ ├── host_vars/ # 主機變量
│ └── group_vars/ # 組變量
├── roles/ # 自定義角色
├── playbooks/ # playbook 文件
└── files/ # 文件資源
驗證配置文件:
#測試配置文件目錄結構如下
root@node1:/etc/ansible# tree /etc/ansible
/etc/ansible
├── ansible.cfg
├── files
├── group_vars
│?? └── all.yml
├── hosts
├── host_vars
├── inventory
├── roles
└── templates
root@node1:/etc/ansible# ansible --version
ansible [core 2.17.12]config file = /etc/ansible/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/local/lib/python3.10/dist-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/local/bin/ansiblepython version = 3.10.12 (main, Feb 4 2025, 14:57:36) [GCC 11.4.0] (/usr/bin/python3)jinja version = 3.0.3libyaml = True
2.6、測試
#免密配置為test用戶,直接執行ansible ubuntu_servers -m command -u test -a 'df -h'因配置開啟權限提升,會出現執行失敗情況,現象如下:
root@node1:/etc/ansible# ansible ubuntu_servers -m command -u test -a 'ls /tmp'
192.168.0.151 | FAILED | rc=-1 >>
Missing sudo password
192.168.0.152 | FAILED | rc=-1 >>
Missing sudo password#處理方式1:可以通過-K參數手動輸入密碼
root@node1:/etc/ansible# ansible ubuntu_servers -m command -u test -a 'df -h' -K
BECOME password:
192.168.0.151 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
tmpfs 197M 1.1M 196M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 15G 7.0G 7.0G 51% /
tmpfs 982M 0 982M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 2.0G 252M 1.6G 14% /boot
tmpfs 197M 4.0K 197M 1% /run/user/1000
192.168.0.152 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
tmpfs 197M 1.1M 196M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 15G 6.5G 7.5G 47% /
tmpfs 982M 0 982M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 2.0G 252M 1.6G 14% /boot
tmpfs 197M 4.0K 197M 1% /run/user/1000#方式2:關閉權限提升配置,編輯ansible.cfg配置,將become=true,改為become=false
root@node1:/etc/ansible# ansible ubuntu_servers -m command -u test -a 'df -h'
192.168.0.151 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
tmpfs 197M 1.1M 196M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 15G 7.0G 7.0G 51% /
tmpfs 982M 0 982M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 2.0G 252M 1.6G 14% /boot
tmpfs 197M 4.0K 197M 1% /run/user/1000
192.168.0.152 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
tmpfs 197M 1.1M 196M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 15G 6.5G 7.5G 47% /
tmpfs 982M 0 982M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 2.0G 252M 1.6G 14% /boot
tmpfs 197M 4.0K 197M 1% /run/user/1000