簡介
Ansible
是一款開源的自動化工具,廣泛應用于配置管理、應用部署、任務自動化以及多節點管理等領域。它由 Michael DeHaan 于 2012 年創建,ansible 目前已經已經被紅帽官方收購,是自動化運維工具中大家認可度最高的,并且上手容易,學習簡單。是每位運維工程師必須掌握的技能之一。
官網文檔地址:Ansible Documentation — Ansible Community Documentation
特點:
-
無代理架構
不需要再被控節點上安裝任何專門的代理軟件。通過 SSH 與被控節點通信,實現任務的執行和數據的傳輸
? ? 優點:
-
簡化部署:無需在每個被控節點上安裝和維護代理程序,減少運維工作量。
-
提高安全性:減少了潛在的攻擊面,因為不需要開放額外的端口或運行額外的服務。
-
節省資源:避免了代理軟件占用系統資源的問題。
-
基于SSH
-
通過 SSH 協議與被控節點通信,能都在大多數 Unix 系統上無縫運行。且支持Windows 主機,使用 PowerShell 或 WinRM 進行管理
? ?優點:
-
廣泛兼容性:支持多種操作系統,無需額外配置。
-
安全性高:利用現有的 SSH 安全機制,確保數據傳輸的機密性和完整性。
-
聲明式任務
-
使用聲明式語言(YAML)定義任務,描述的是目標狀態而非具體的執行步驟。這是的配置更加直觀易讀,同時減少了錯誤和不一致性
? ? 優點:
-
可讀性強:YAML 格式簡潔明了,易于理解和編寫。
-
可重用性高:通過劇本(Playbook) 和角色(Roles)的復用,實現復雜任務的模塊化管理。
-
冪等性:確保多次運行相同的任務不會導致不一致的狀態。
各個板塊的概念
主機清單
Ansible 中用于管理目標主機的配置文件。它是一個文本文件,其中列出了要在其上執行 Ansible 任務的遠程主機。
可以包含 IP 地址、域名或主機別名等信息,且可以將主機分組以便更好地組織和管理。文件默認位于 /etc/ansible/hosts 文件中,但也可以使用 -i 參數指定其他位置的清單文件。
#這是一個示例:
[web-servers]
webserver1.example.com
webserver2.example.com[database-servers]
dbserver1.example.com
dbserver2.example.com
?連接插件
Ansible 中的組件,用建立與遠程主機的連接。Ansible 支持多種連接插件,包括 SSH、WinRM 等。連接插件的選擇取決于目標主機的操作系統和配置。
通過連接插件,Ansible 可以與目標主機進行通信,并在其上執行任務。在主機清單中,可以通過在主機名后連接插件名稱來指定連接插件。如果不指定,默認使用 SSH 連接插件。
這是一個示例
[web-servers]
webserver1.example.com ansible_connection=ssh
webserver2.example.com ansible_connection=ssh[database-servers]
dbserver1.example.com ansible_connection=ssh
dbserver2.example.com ansible_connection=winrm
模塊
是 Ansible 的核心組件,用于在遠程主機上執行任務。
Ansible 提供了豐富的內置模塊,涵蓋了各種任務,如文件操作、軟件包管理、服務管理、用戶管理等。還能通過在劇本中調用模塊,可以實現自動化任務的執行。以下是一些常見的模塊:
- ping:檢測遠程主機的連通性。
- command/shel:在目標主機上執行命令或命令字符串。
- copy:將文件從控制節點復制到遠程主機。
- file:創建、修改或刪除文件和目錄。
- template:使用 jinja2 模板生成文件,并將其復制到遠程主機。
- apt/yum:在基于 Debian/RedHat 的系統上安裝、升級或移除軟件包。
- service:啟動、停止、重新啟動或重載系統服務。
- user/group:創建、修改或刪除用戶和用戶組。
- lineinfile:在文中添加、修改或刪除一行文本。
- raw:在目標主機上執行原始命令,繞過模塊系統。
- wait_for:等待一定時間或直到某個條件為真。
- script:在在目標主機上執行本地腳本。
- git:克隆或更新 Git 代碼庫。
- debug:打印調試信息。
Playbook【劇本】
是 Ansible 的核心組件之一,是一種以 YAML 格式編寫的自動化任務描述文件。
每個 Playbook 由一個或多個 Play 組成。在每個 Play 下面,通過 tasks 關鍵字來定義一組任務。每個任務由一個或多個模塊組成,用于在遠程主機上實現自動化部署、配置和管理等操作。
# 這是一個示例
- name: Install and start Nginx # 描述 Playbook 或任務的簡短名稱hosts: web_servers # 指定要執行任務的目標主機或主機組become: yes # 可選參數,用于指定是否以管理員權限執行任務及執行任務的用戶。tasks:- name: Install Nginxapt:name: nginxstate: present- name: Start Nginx serviceservice:name: nginxstate: started
安裝操作
主機規劃
主機 | IP | 說明 |
---|---|---|
ansible-controller | 192.168.72.63 | 安裝ansible |
ansible-node1 | 192.168.72.64 | |
ansible-node2 | 192.168.72.65 |
環境準備【在對應的主機上執行命令】
配置主機名
# hostnamectl hostname ansible-controller# hostnamectl hostname ansible-node1# hostnamectl hostname ansible-node2
配置IP地址【改成自己主機所在的網段】
[root@ansible-controller ~]# nmcli c m ens160 ipv4.method manual ipv4.addresses 172.25.250.63/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@ansible-controller ~]# nmcli c up ens160[root@ansible-node1 ~]# nmcli c m ens160 ipv4.method manual ipv4.addresses 172.25.250.64/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@ansible-node1 ~]# nmcli c up ens160[root@ansible-node2 ~]# nmcli c m ens160 ipv4.method manual ipv4.addresses 172.25.250.65/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
[root@ansible-node2 ~]# nmcli c up ens160
關閉防火墻【三臺主機均要執行】
systemctl disable --now firewalld
關閉Selinux
setenforece 0sed -i "s/SELINUX=enforcing/SELINUX=permissive/" /etc/selinux/config
掛載倉庫
臨時掛載
mount /dev/sr0 /mnt
永久掛載
vim /etc/fstab
....
/dev/sr0 /mnt iso9660 defaults 0 0# 檢查掛載是否出錯
mount -a
安裝方法:
nsible 有三種安裝方式,源碼安裝、發行版安裝和 Python 安裝。
使用發行版安裝或 Python 安裝兩種方式時,Ansible 的安裝包有兩個,區別如下:
-
ansible-core
:一種極簡語言和運行時包,包含一組內置模塊和插件。 -
ansible
:一個更大的“包含電池”的軟件包,它添加了社區精選的 Ansible 集合選擇,用于自動化各種設備。
在用源碼或者 Python 安裝 Ansible 時,默認不會安裝
sshpass
軟件包,該軟件包用來給 Ansible 提供密碼驗證被控端,因此如果在執行 Ansible 的命令時需要輸入ssh
的密碼,則需要該軟件包,該軟件包通過dnf install -y sshpass
。
源碼安裝【它只能安裝 Ansible-core 且只需要在主節點上安裝即可】
[root@ansible-controller ~]# dnf install python3.12 python3.12-pip sshpass
[root@ansible-controller ~]# tar xf ansible-2.16.3.tar.gz
[root@ansible-controller ~]# cd ansible-2.16.3/
[root@ansible-controller ansible-2.16.3]# python3 -m pip install -r ./requirements.txt
[root@ansible-controller ansible-2.16.3]# python3 setup.py install
發行版安裝【只需要在主節點上安裝即可】
安裝epel源
[root@ansible-controller ~]# dnf install https://mirrors.aliyun.com/epel/epel-release-latest-9.noarch.rpm -y[root@ansible-controller ~]# ls /etc/yum.repos.d/
base.repo epel-cisco-openh264.repo epel.repo epel-testing.repo redhat.repo
安裝ansible
# 安裝包含常用模塊的 Ansible
[root@ansible-controller ~]# dnf install ansible -y
# 或
# 安裝最簡潔的 Ansible
[root@ansible-controller ~]# dnf install ansible-core -y
安裝驗證
[root@ansible-controller ~]# ansible --version
ansible [core 2.14.9]config file = /etc/ansible/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3.9/site-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/bin/ansiblepython version = 3.9.18 (main, Sep 7 2023, 00:00:00) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2)] (/usr/bin/python3)jinja version = 3.1.2libyaml = True
Python安裝
安裝Python
# 安裝 Python3 和 pip
[root@ansible-controller ~]# dnf install python3.12 python3.12-pip sshpass
安裝ansible
# 安裝 Ansible-core
[root@ansible-controller ~]# python3.12 -m pip install ansible-core==2.16.3# 安裝 Ansible
[root@ansible-controller ~]# python3.12 -m pip install ansible
Requirement already satisfied: ansible in /usr/lib/python3.9/site-packages (7.7.0)
Requirement already satisfied: ansible-core>=2.14.7 in /usr/lib/python3.9/site-packages (from ansible) (2.14.17)
Requirement already satisfied: PyYAML>=5.1 in /usr/lib64/python3.9/site-packages (from ansible-core>=2.14.7->ansible) (5.4.1)
Requirement already satisfied: cryptography in /usr/lib64/python3.9/site-packages (from ansible-core>=2.14.7->ansible) (36.0.1)
Requirement already satisfied: packaging in /usr/lib/python3.9/site-packages (from ansible-core>=2.14.7->ansible) (20.9)
Requirement already satisfied: resolvelib<0.9.0,>=0.5.3 in /usr/lib/python3.9/site-packages (from ansible-core>=2.14.7->ansible) (0.5.4)
Requirement already satisfied: cffi>=1.12 in /usr/lib64/python3.9/site-packages (from cryptography->ansible-core>=2.14.7->ansible) (1.14.5)
Requirement already satisfied: pyparsing>=2.0.2 in /usr/lib/python3.9/site-packages (from packaging->ansible-core>=2.14.7->ansible) (2.4.7)
Requirement already satisfied: pycparser in /usr/lib/python3.9/site-packages (from cffi>=1.12->cryptography->ansible-core>=2.14.7->ansible) (2.20)
Requirement already satisfied: ply==3.11 in /usr/lib/python3.9/site-packages (from pycparser->cffi>=1.12->cryptography->ansible-core>=2.14.7->ansible) (3.11)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
安裝驗證
# 查看版本
[root@ansible-controller ~]# ansible --version
ansible [core 2.14.17]config file = /etc/ansible/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3.9/site-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/bin/ansiblepython version = 3.9.19 (main, Aug 23 2024, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/usr/bin/python3)jinja version = 3.1.2libyaml = True
設置參數自動補全
[root@ansible-controller ~]# python3.12 -m pip install argcomplete
[root@ansible-controller ~]# activate-global-python-argcomplete --user
配置主機映射
[root@ansible-controller ~]# cat >> /etc/hosts <<EOF
192.168.72.63 ansible-controller
192.168.72.64 ansible-node1
192.168.72.65 ansible-node2
EOF
# 后面可以跟個簡短名,后續使用n1和n2
[root@ansible-controller .ssh]# vim /etc/hosts
192.168.23.63 ansible-controller
192.168.23.64 ansible-node1 n1
192.168.23.65 ansible-node2 n2
主機映射執行成功之后將文件復制到另外兩個被控節點
[root@ansible-controller ~]# scp /etc/hosts ansible-node1:/etc
[root@ansible-controller ~]# scp /etc/hosts ansible-node2:/etc
免密登錄
控制節點是運行 Ansible
的主機,負責發送任務并收集結果。被控節點是被 Ansible
管理的主機,無需安裝任何額外軟件,僅需確保 SSH
服務正常運行,并具備必要的訪問權限。
只需要在主控節點上創建密鑰對就好
[root@ansible-controller ~]# ssh-keygen
# -t可以指定加密算法
[root@ansible-controller ~]# ssh-keygen -t rsa
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:mnbdYPHKXn9SSKSSAgpSiZ20tnkrAJcbXmKM03P6c/Q root@ansible-controller
The key's randomart image is:
+---[RSA 3072]----+
| =oo |
|o+=o . . |
|+.%.o. . .. o |
|.* @. . oo. . |
|. * . . S.o... . |
| . o o + + + . .|
| . + = E + o . |
| . + . . . .. .|
| . .o |
+----[SHA256]-----+
拷貝公鑰
創建成功后,執行以下命令將公鑰拷貝到兩臺被控節點
# 查看隱藏目錄
[root@ansible-controller ~]# ls -a
[root@ansible-controller ~]# cd .ssh/
# .pub即為公鑰,使用rsa算法
[root@ansible-controller .ssh]# ls
id_rsa id_rsa.pub known_hosts known_hosts.old
# 復制本機公鑰到其它被控節點
[root@ansible-controller ~]# ssh-copy-id ansible-node1
# 或
[root@ansible-controller ~]# ssh-copy-id n1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@ansible-node1's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'ansible-node1'"
and check to make sure that only the key(s) you wanted were added.[root@ansible-controller ~]# ssh-copy-id ansible-node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@ansible-node2's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'ansible-node2'"
and check to make sure that only the key(s) you wanted were added.# 或
# 執行命令后,輸入正確密碼即可
[root@ansible-controller ~]# ssh-copy-id root@192.168.72.64
[root@ansible-controller ~]# ssh-copy-id root@192.168.72.65
配置好之后做測試連接
# 如果免密做成功則無需密碼即可登錄
[root@ansible-controller ~]# ssh root@192.168.72.64
# exit可退回原來的主機
[root@ansible-node1 ~]# exit
注意:
ssh-copy-id
命令格式有兩種:
ssh-copy-id
遠程用戶@遠程IP 或 僅IP
ssh-copy-id -i /root/.ssh/id_rsa.pub
遠程用戶@遠程IP 或 僅IP如果在生成密鑰時指定了密鑰的名稱,此處需要通過
ssh-copy-id -i
指定的名稱 遠程用戶@遠程IP 或 僅IP
快速使用
創建項目目錄
[root@ansible-controller ~]# mkdir ansible_ping
[root@ansible-controller ~]# cd ansible_ping/
創建清單文件
# 名稱任意,直接命名inventory也可以
[root@ansible-controller ansible_ping]# vim inventory.ini
[root@ansible-controller ansible_ping]# cat inventory.ini
[server]
192.168.72.64
n2
執行Ansible?
# 這里server是配置文件中括號內的名稱,-m指模塊 概念在前面有所提及
[root@ansible-controller ansible_ping]# ansible server -i inventory.ini -m ping
ansible-node1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
# 此時node2節點報錯,因為使用了縮寫名n2
192.168.23.65 | UNREACHABLE! => {"changed": false,"msg": "Failed to connect to the host via ssh: Host key verification failed.","unreachable": true
}
# 只需改為節點對應IP或者全稱即可(ansible-node2)
[root@ansible-controller ~]# cat inventory
[server]
192.168.23.64
192.168.23.65
[root@ansible-controller ~]# ansible server -i inventory -m ping
192.168.23.64 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
192.168.23.65 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
文檔就寫到這里,若有需要請查看附加文件,里面有具體的綜合示例