- 基礎(chǔ)篇與進(jìn)階篇主要是為了基于
AnsibleAPI二次開發(fā) 任務(wù)中心 做墊鋪,并非官方使用文檔,可能會(huì)有所遺漏,如有不足之處,歡迎拍磚指正,謝謝。- 基于
AnsibleAPI二次開發(fā)任務(wù)中心已經(jīng)開發(fā)完成,并且已經(jīng)投入生產(chǎn)使用,后續(xù)會(huì)把詳細(xì)的設(shè)計(jì)思路與具體架構(gòu)分享給大家,敬請(qǐng)期待。- 為了方便大部分同學(xué)更好的了解Ansible的高級(jí)功能與特性,僅以此篇文章指引大家循序漸進(jìn)的系統(tǒng)學(xué)習(xí)Ansible,將Ansible的高級(jí)功能以及特性發(fā)揮到淋漓盡致。
ansible 簡(jiǎn)介:
Anasible 是基于Python2-Paramiko 模塊開發(fā)的自動(dòng)化維護(hù)工具,實(shí)現(xiàn)了批量系統(tǒng)配置、部署、運(yùn)行等功能。Ansible是基于模塊工作的,本身不具備批量部署的功能,如果想要實(shí)現(xiàn)批量自動(dòng)化部署,是Ansible自身的各種模塊的集合。
可以與 Ansible 同臺(tái)競(jìng)技的運(yùn)維工具( pupet、cfengine、chef、func、fabric、saltstack )
-
Ansible 發(fā)展史
- ansible 作者:
Michael DeHaan同時(shí)他也是Cobbler 與 Func作者。 - 2012-03-09 發(fā)布 0.0.1 版本。
- 2015-10-17 被
Red Hat收購。 - GitHub
- ansible 作者:
-
ansible 特性
- Python 開發(fā)
- 模塊化: 調(diào)用特定的模塊(如: Paramiko、PyYAML、jinja2 等), 完成特定的任務(wù)。
- 自定義模塊
- 簡(jiǎn)單部署
- 支持編排任務(wù)(PlayBook)
- 冥等性: 任務(wù)重復(fù)執(zhí)行等于只執(zhí)行一次, 不會(huì)重復(fù)執(zhí)行多次相同命令。
- 支持多語言編寫模塊
- YAML 格式編排任務(wù),支持豐富的數(shù)據(jù)結(jié)構(gòu).
Ansible 架構(gòu)與執(zhí)行流程


ansible 主要組成部分:
- Users: 管理 Ansible Playbook 和 Ansible 引擎。
- Ansible playbook: Ansible 引擎和CMDB做交互。
- Public or Private cloud: 便于所有模塊 和 API 以及云的交互。
- Inventory: Ansible 執(zhí)行主機(jī)的列表清單,默認(rèn)讀取配置為/etc/ansible/hosts。
- API: 提供端到端交互的API接口。
- Modules: Ansible 執(zhí)行命令的功能模塊, 一般為 Ansible 內(nèi)置模塊, 也可以自定義第三方模塊。
- Plugins: 用于補(bǔ)充模塊的功能,可以自行編寫插件。
Ansible 安裝
# Centos
yum -y install ansible
# Ubuntu
apt install -y ansible
# 源碼安裝
git clone https://github.com/ansible/ansible
# 使用 pip 命令安裝
pip install ansible
Ansible 配置說明:
/etc/ansible/ansible.cfg 主配置文件, 配置ansible的工作特性.
/etc/ansible/hosts 主機(jī)列表清單.
/etc/ansible/roles/ 存放(roles)角色的目錄.
/usr/local/bin/ansible 二進(jìn)制執(zhí)行文件, ansible 主程序.
/usr/local/bin/ansilbe-doc 配置文檔, 模塊功能查看工具.
/usr/local/bin/ansible-galaxy 用于上傳/下載 roles 模塊到官方平臺(tái)的工具.
/usr/local/bin/ansible-playbook 自動(dòng)化任務(wù)、編排劇本工具/usr/bin/ansible-pull 遠(yuǎn)程執(zhí)行命令的工具.
/usr/local/bin/ansible-vault 文件(如: playbook 文件) 加密工具.
/usr/local/bin/ansible-console 基于 界面的用戶交互執(zhí)行工具.
/etc/ansible/hosts
創(chuàng)建SSH秘鑰
ssh-keygen -t rsa -C "deniss.wang"拷貝公鑰到其他被服務(wù)器
ssh-copy-id -i ubuntu@ubuntu20-bj01
hosts
[codo-cluster]
demo.opendevops.cn ansible_ssh_user=root
www.opendevops.cn ansible_ssh_user=root
[k3s-cluster]
ubuntu20-bj01 ansible_user=ubuntu
ubuntu20-bj02 ansible_user=ubuntu
ubuntu20-bj03 ansible_user=ubuntu
ubuntu20-sh04 ansible_user=ubuntu
-m 指定模塊執(zhí)行。 如:ping、yum、copy、file等,此處使用模塊ping測(cè)試。
-k 使用密碼方式,默認(rèn)是使用SSH-KEY登錄。
基本示例:
# ansible 通過 單主機(jī)進(jìn)行操作 ( -k 為用戶密碼方式, 默認(rèn)為 ssh-key )
ansible 10.0.8.2 -m ping -k
# ansible 通過 ':' 組合進(jìn)行操作
ansible "10.0.8.2:10.0.8.3" -m ping -k
# ansible 通過 通配符加主機(jī) 進(jìn)行操作
ansible 10.0.8.* -m ping -k
# ansible 通過 hosts 組名稱 進(jìn)行操作
ansible codo -m ping -k
# ansible 通過 ':' 組合組進(jìn)行操作
ansible 'codo-cluster:k3s-cluster' -m ping -k
# ansible 通過 通配符 進(jìn)行操作
ansible '*-cluster' -m ping -k
# ansible 通過 ':&' 邏輯與 (兩個(gè)組中都包含的主機(jī))
ansible 'codo-cluster:&k3s-cluster' -m ping -k
# ansible 通過 ':!' 邏輯非 (codo-cluster 但不在 k3s-cluster的主機(jī))
ansible 'codo-cluster:!k3s-cluster' -m ping -k
# ansible 也支持多邏輯的組合
ansible 'webservers:dbserver:&appserver:!ftpservers' -m ping -k
# ansible 也支持正則表達(dá)式
ansible '~(codo|k3s)-cluster' -m ping -k
# ansible 通過 all 對(duì) hosts 清單下所有主機(jī)進(jìn)行操作
ansible all -m ping -k
# ansible 通過 通配符 對(duì) hosts 清單下所有主機(jī)進(jìn)行操作
ansible '*' -m ping -k
執(zhí)行結(jié)果:
# ansible k3s -m 'ping'
ubuntu20-bj03 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ubuntu20-bj02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ubuntu20-bj01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ubuntu20-sh04 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
/etc/ansible/ansible.cfg
# defaults 為默認(rèn)配置
[defaults]
# 主機(jī)清單的路徑, 默認(rèn)為如下
# inventory = /etc/ansible/hosts
# 模塊存放的路徑
# library = /usr/share/my_modules/
# utils 模塊存放路徑
# module_utils = /usr/share/my_module_utils/
# 遠(yuǎn)程主機(jī)腳本臨時(shí)存放目錄
# remote_tmp = ~/.ansible/tmp
# 管理節(jié)點(diǎn)腳本臨時(shí)存放目錄
# local_tmp = ~/.ansible/tmp
# 插件的配置文件路徑
# plugin_filters_cfg = /etc/ansible/plugin_filters.yml
# 執(zhí)行并發(fā)數(shù)
# forks = 5
# 異步任務(wù)查詢間隔 單位秒
# poll_interval = 15
# sudo 指定用戶
# sudo_user = root
# 運(yùn)行 ansible 是否提示輸入sudo密碼
# ask_sudo_pass = True
# 運(yùn)行 ansible 是否提示輸入密碼 同 -k
# ask_pass = True
# 遠(yuǎn)程傳輸模式
# transport = smart
# SSH 默認(rèn)端口
# remote_port = 22
# 模塊運(yùn)行默認(rèn)語言環(huán)境
# module_lang = C
# roles 存放路徑
# roles_path = /etc/ansible/roles
# 不檢查 /root/.ssh/known_hosts 文件 建議取消
# host_key_checking = False
# ansible 操作日志路徑 建議打開
# log_path = /var/log/ansible.log
ansible執(zhí)行過程
load配置文件 /etc/ansible/ansible.cfg
Load 模塊配置文件
通過 Ansible 將調(diào)用的模塊或PlayBook生成對(duì)應(yīng)的臨時(shí) py文件, 并將該臨時(shí)文件傳輸至遠(yuǎn)程服務(wù)器的對(duì)的執(zhí)行用戶目錄下
$HOME/.ansible/tmp/ansible-tmp-2123/xxxxxxx.py>文件.對(duì)生成的文件添加可執(zhí)行權(quán)限.
執(zhí)行生成文件,并返回對(duì)應(yīng)的結(jié)果.
-
刪除生成文件,退出.
執(zhí)行返回狀態(tài):
- 綠色:執(zhí)行成功,無更改操作。如 ping模塊
- 黃色:執(zhí)行成功,更新過主機(jī)的操作。如執(zhí)行shell模塊執(zhí)行ifconfig命令。
- 紅色:執(zhí)行失敗返回結(jié)果。 如FAILED、UNREACHABLE狀態(tài)。
Ansible-Doc
顯示幫助信息
使用參數(shù):
-l --list 顯示可用模塊
-s --snippet 顯示指定模塊的 playbook 階段
Demo
# Demo
ansible-doc -l
# ...略過
ansible-doc ping
# 模塊ping顯示文檔
ansible-doc -s ping
Ansible
ansible <host-pattern> [-m module_name] [-a args]
-
host-pattern: 主機(jī)ip、主機(jī)名、主機(jī)組。 -
module_name: 模塊的名稱。默認(rèn)為-m command。 -
args: 模塊的參數(shù), 需要加上-a進(jìn)行指定模塊的參數(shù)。如: `ansible all -a ‘hostname’ -
-v、-vv、-vvv: 顯示詳細(xì)的命令輸出日志, v 越多越詳細(xì)。如:ansible all -m ping -vvv -
--list: 顯示主機(jī)的列表。 如:ansible all --list -
-k / --ask-pass: 提示輸入ssh連接密碼, 默認(rèn)為 ssh-key 認(rèn)證。如:ansible all -m ping -k -
-K / --ask-become-pass: 提示輸入 sudo 的密碼。 -
-C / --check: 檢查命令操作, 并不會(huì)執(zhí)行。如:ansible all -m ping -C -
-T / --timeout: 執(zhí)行命令的超時(shí)時(shí)間, 默認(rèn)為 10s。如:ansible all -m ping -T=2 -
-u / --user: 執(zhí)行遠(yuǎn)程操作的用戶. 如:ansible all -m ping -u=root -
-b / --become: 代替舊版的sudo切換。
ansible 常用模塊
截止 2021-10-12 ansible 模塊為 6006 個(gè). 不可謂不強(qiáng)大!人生苦短,我用Python!
command 模塊
在遠(yuǎn)程主機(jī)上執(zhí)行命令, 支持條件判斷. ansible 默認(rèn)模塊, 可忽略 -m 參數(shù)直接操作.
注意: command 模塊 不支持 $VARNAME < > | ; & 等符號(hào).
Demo
# 停止docker服務(wù)
ansible k3s-cluster -m command -a 'systemctl stop docker'
# 查看所有docker鏡像
ansible k3s-cluster -a 'docker ps -a'
# 如果 /opt/ansible 不存在 就不執(zhí)行 df -h 操作, 如果 /opt/ansible 存在, 就執(zhí)行 df -h 操作.
ansible k3s-cluster -a 'removes=/mnt/ansible df -h'
# 如果 /opt/ansible 不存在 就執(zhí)行 df -h 操作, 如果存在 /opt/ansible 就不執(zhí)行 df -h 操作.
ansible k3s-cluster -a 'creates=/mnt/ansible df -h'
# 切換目錄, 等同于 cd /mtn && ls -lt 操作
ansible k3s-cluster -a 'chdir=/mnt ls -lt'
shell 模塊
shell 模塊: shell 模塊支持 command 所有的操作, 而且支持 $VARNAME < > | ; & 等符號(hào)操作.
Demo:
# 查看docker進(jìn)程
ansible k3s-cluster -m shell -a 'ps -ef|grep docker'
scripts 模塊
script 模塊: 執(zhí)行腳本. 只需要調(diào)用 ansible 的宿主機(jī)存放的腳本文件就可以在選擇主機(jī)上面執(zhí)行腳本.
Demo
# shell
cat /tmp/deniss.sh
#!/bin/bash
echo "測(cè)試 shell"
ansible k3s-cluster -m script -a '/tmp/deniss.sh'
# python
cat /tmp/deniss.py
#!/usr/bin/python
import sys
print ('Deniss_Wang' )
print (sys.version)
ansible k3s-cluster -m script -a '/tmp/deniss.py'
# 其他腳本也是可以的,只需要配置好環(huán)境語言解釋即可。
copy 模塊
copy 模塊: 復(fù)制ansible宿主機(jī)文件到目標(biāo)主機(jī).
Demo
# src="" 宿主機(jī)路徑 dest="" 目標(biāo)主機(jī)路徑 backup=yes 如果目標(biāo)主機(jī)文件存在, 會(huì)備份, 再覆蓋.
ansible k3s-cluster -m copy -a ‘src=/tmp/deniss.py dest=/tmp/deniss.py backup=yes
# mode="" 修改權(quán)限, owner="" 修改用戶, group="" 修改用戶組
ansible k3s-cluster -m copy -a 'src=/root/deniss.py dest=/root/deniss.py mode=0644 owner=deniss group=deniss'
# content="" 將內(nèi)容寫入到目標(biāo)文件中
ansible k3s-cluster -m copy -a 'content="hello\nworld\n" dest=/tmp/deniss.txt'
Fetch 模塊
fetch 模塊: 將目標(biāo)遠(yuǎn)程主機(jī)的文件, 下載到本地, 下載成功會(huì)存放在以 IP/NAME的目錄中, 包含原文件的整體路徑.
注意:只能下載單個(gè)文件, 不支持目錄, 想下載完整路徑,可以壓縮后在下載。
Demo
# src="" 目標(biāo)遠(yuǎn)程主機(jī)的文件路徑 dest="" 本地目錄
ansible k3s-cluster -m fetch -a 'src=/var/log/syslog dest=/tmp/'
file 模塊
file 模塊: 操作遠(yuǎn)程目標(biāo)主機(jī)的文件. 如: touch、 absent 等.
Demo
# mode="" 修改權(quán)限 owner="" 修改用戶 group="" 修改用戶組 recurse=yes 遞歸授權(quán)
ansible k3s-cluster -m file -a 'name=/tmp/deniss.txt owner=ubuntu group=ubuntu mode=0755 recurse=yes'
# dest、name、path: 指定遠(yuǎn)程主機(jī)的文件路徑,state: 文件操作類型,默認(rèn)為 absent,touch: 創(chuàng)建空文件.
ansible k3s-cluster -m file -a 'name=/tmp/deniss.txt state=touch'
# directory: 創(chuàng)建文件夾, absent: 遞歸刪除文件夾/文件,link: 創(chuàng)建軟連接.
ansible k3s-cluster -m file -a 'src=/tmp/deniss.txt dest=/tmp/deniss.link state=link'
Cron 模塊
cron 模塊: 為遠(yuǎn)程主機(jī)添加定時(shí)任務(wù)
-
day: 表示 天. 支持 ( 1-31, *, */2 ) 寫法 -
hour: 表示 小時(shí). 支持 ( 0-23, *, */2 ) 寫法 -
minute: 表示 分鐘. 支持 ( 0-59, *, */2 ) 寫法 -
month: 表示 月. 支持 ( 1-12, *, */2 ) 寫法 -
weekday: 表示 星期. 支持 ( 0-6, Sunday-Saturday, * )寫法 -
job: 表示 計(jì)劃任務(wù)的內(nèi)容. -
name: 表示 計(jì)劃任務(wù)名稱. 相同的計(jì)劃任務(wù)名稱會(huì)覆蓋.
Demo
# day: 表示 天. 支持 ( 1-31, *, */2 ) 寫法
# hour: 表示 小時(shí). 支持 ( 0-23, *, */2 ) 寫法
# minute: 表示 分鐘. 支持 ( 0-59, *, */2 ) 寫法
# month: 表示 月. 支持 ( 1-12, *, */2 ) 寫法
# weekday: 表示 星期. 支持 ( 0-6, Sunday-Saturday, * )寫法
# job: 表示 計(jì)劃任務(wù)的內(nèi)容.
# name: 表示 計(jì)劃任務(wù)名稱. 相同的計(jì)劃任務(wù)名稱會(huì)覆蓋.
ansible k3s-cluster -m cron -a 'weekday=1-5 job="echodate>> /tmp/1.txt" name=echocron'
# disabled= (true/false、yes/no)注釋掉計(jì)劃任務(wù) 關(guān)閉、啟動(dòng)計(jì)劃任務(wù) 必須指定job和name.
ansible k3s-cluster -m cron -a 'disabled=true job="echodate>> /root/1.txt" name=echocron'
# state=absent 刪除計(jì)劃任務(wù)。
ansible k3s-cluster -m cron -a 'name=echocron state=absent'
Yum 模塊
yum 模塊: 利用 yum 操作軟件包, 如 安裝、查詢、卸載等.
Demo
# name: 軟件包的名稱, 或者rpm包, 遠(yuǎn)程服務(wù)器必須存在 rpm 包. 安裝多個(gè)軟件使用 , 號(hào)隔開. 如 name=nginx,php,mysql
# state="present/installed/absent/removed"
# present、installed: 安裝軟件.
# absent、removed: 卸載/刪除軟件.
# update_cache=yes: 更新 yum 緩存后 在安裝軟件 disable_gpg_check=yes: 禁用 gpg 檢查.
ansible k3s-cluster -m yum -a 'name=mysql state=present'
ansible k3s-cluster -m yum -a 'name=/tmp/nginx-xx.x.x-x.x.x86_64.rpm'
ansible k3s-cluster -m yum -a 'name=nginx update_cache=yes disable_gpg_check=yes'
# list="updates/installed/available/repos" 指定獲取狀態(tài)
# 狀態(tài)釋義: installed: 已安裝的軟件 updates: 可以升級(jí)的軟件 available: 可以安裝的軟件 repos: yum 源
ansible k3s-cluster -m yum -a 'list=installed'
Service 模塊
service: 軟件服務(wù)管理模塊. 啟動(dòng)、關(guān)閉、重啟 等操作.
# name="",安裝名字
# state="started/stopped/restarted/reloaded" 啟動(dòng)、停止、重啟、重載
# enable="yes/no、true/false" 設(shè)置是否開機(jī)自啟
ansible k3s-cluster -m service -a 'name=nginx state=started enabled=yes'
User 模塊
user: 管理系統(tǒng)用戶的模塊
Demo
# name"" 用戶名
# shell="" 指定用戶的shell類型
# system="yes/no" 指定是否為 系統(tǒng)用戶
# home="" 指定用戶額外的home目錄, 默認(rèn)/home/user .
# groups="" 用戶額外的 groups 組.
# uid="" 指定用戶的UID.
# comment="" 用戶描述
ansible k3s-cluster -m user -a 'name=deniss shell=/sbin/nologin system=yes home=/tmp/deniss groups=root uid=777 comment="deniss user"'
# state="present/absent"
# present: 創(chuàng)建用戶 (默認(rèn)為present) absent: 刪除用戶
ansible k3s-cluster -m user -a 'name=deniss state=absent remove=yes'
ansible k3s-cluster -m user -a 'name=nginx state=absent remove=yes'
Group 模塊
group: 管理系統(tǒng)用戶組的模塊.
Demo
# name"" 用戶名
# system="yes/no" 指定是否為 系統(tǒng)用戶
# home="" 指定用戶額外的home目錄, 默認(rèn)/home/user .
# gid="" 指定GID.
# state="present/absent"
# present: 創(chuàng)建用戶組 (默認(rèn)為present) absent: 刪除用戶組
# 創(chuàng)建
ansible all -m group -a 'name=deniss system=yes gid=777'
# 刪除
ansible all -m group -a 'name=deniss state=absent'
ansible-galaxy
官網(wǎng) https://galaxy.ansible.com/
ansible-galaxy 工具用于下載對(duì)應(yīng)的roles
# list 查看本地的 roles 角色。
ansible-galaxy list geerlingguy.nginx
# install 下載 roles 角色存放到$HOME/.ansible/roles/目錄下。
ansible-galaxy install geerlingguy.nginx
# remove 刪除已下載的 roles 角色,也可以在存放目錄總刪除。
ansible-galaxy remove geerlingguy.nginx
附上幾張已經(jīng)開發(fā)完成的任務(wù)中心截圖,歡迎拍磚!




在基于DevOps思想對(duì)自動(dòng)化運(yùn)維改革的大道上,一直砥礪前行,從未停歇。
道阻且長(zhǎng),行則將至,行而不輟,未來可期。
歡迎搜索 k8stech 關(guān)注公眾號(hào),定時(shí)更新運(yùn)維開發(fā)、SRE、云原生等文章。