1 引言
基于Python開(kāi)發(fā),集合了眾多運(yùn)維工具(puppet职辅、func鱼喉、fabric)的優(yōu)點(diǎn)旋圆,實(shí)現(xiàn)了通過(guò)各種模塊批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能卦羡。
默認(rèn)通過(guò)SSH連接受控機(jī)執(zhí)行任務(wù)瓶颠,無(wú)需代理瑟慈。多并發(fā)过吻。
2 安裝
管理機(jī)應(yīng)安裝Python 2(2.7)或Python 3(3.5或更高版本)廓推,另外专缠,管理機(jī)不支持Windows控制節(jié)點(diǎn)墩弯。
通過(guò)Pip安裝:
pip install ansible
源碼安裝:
git clone github.com/ansible/ansible.git
cd ./ansible
source ./hacking/env-setup
- 下載安裝程序(適合生產(chǎn)離線場(chǎng)景):
https://mirrors.huaweicloud.com/ansible/
tar xzvf ansible_v2.9.9_install.tar.gz
sh ansible_v2.9.0_install.sh
安裝完畢引矩,添加測(cè)試受控機(jī)進(jìn)行簡(jiǎn)單的測(cè)試区端。
vi /etc/ansible/hosts 添加以下內(nèi)容
[iweb]
10.0.0.111
10.0.0.112
[root]# ansible all -m ping -k
SSH password:
10.0.0.112 | FAILED! => {
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
10.0.0.111 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
管理機(jī)生成SSH密鑰,然后將SSH公鑰復(fù)制到所有客戶機(jī)(112報(bào)錯(cuò)就是因?yàn)闊o(wú)ssh公鑰)。
ssh-keygen -t rsa
[root]# ll /root/.ssh/
-rw------- 1 root root 1675 Nov 3 18:06 id_rsa
-rw-r--r-- 1 root root 401 Nov 3 18:06 id_rsa.pub
-rw-r--r-- 1 root root 348 Feb 3 2021 known_hosts
[root]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.112
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@10.0.0.112'"
and check to make sure that only the key(s) you wanted were added.
加完公鑰,再次測(cè)試,發(fā)現(xiàn)兩臺(tái)受控機(jī)均成功返回機(jī)器名窗宦。
[root]# ansible all -a "uname -a"
10.0.0.111 | CHANGED | rc=0 >>
Linux dbjf-ebus-mnt 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
10.0.0.112 | CHANGED | rc=0 >>
Linux dbjf-istp-lvs1 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
批量授權(quán)方案
hosts
[nginx]
172.0.0.1
[jboss]
10.10.0.1
initial-ssh.sh
#!/bin/bash
user=''
password=''
for host in $(cat hosts|grep -v '\[')
do
sshpass -p ${password} ssh-copy-id -o StrictHostKeyChecking=no root@${host}
done
3 升級(jí)中常用的模塊
- command:在遠(yuǎn)程主機(jī)中執(zhí)行命令,不經(jīng)過(guò)遠(yuǎn)程主機(jī)的shell處理擂啥。不支持重定向、管道符阶祭。如遠(yuǎn)程節(jié)點(diǎn)是windows筝野,則使用win_command模塊焕议。
- shell:在遠(yuǎn)程主機(jī)中執(zhí)行命令時(shí)宽堆,會(huì)經(jīng)過(guò)遠(yuǎn)程主機(jī)上的/bin/sh程序處理籽慢。
- script:腳本只存在于ansible主機(jī)本地箱亿,無(wú)需手動(dòng)拷貝到遠(yuǎn)程主機(jī)后再執(zhí)行届惋。
3.1 調(diào)用本地腳本在受控機(jī)執(zhí)行
[root]# ansible iweb -m script -a "./back-all.sh"
10.0.0.111 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 10.0.0.111 closed.\r\n",
"stderr_lines": [
"Shared connection to 10.0.0.111 closed."
],
"stdout": "",
"stdout_lines": []
}
3.2直接調(diào)用受控機(jī)的腳本執(zhí)行
10.0.0.111 | CHANGED | rc=0 >>
3.3 遠(yuǎn)程復(fù)制過(guò)程中利用copy自帶的backup參數(shù)備份
- name: configure httpd
copy: src=/etc/ansible/files/httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart httpd
3.4 分發(fā)文件到受控機(jī)可以使用copy或rsync模塊歌粥。
[root]# ansible web_jboss_gmjj -m copy -a "src=/data/down/php-5.4.16-42.el7.x86_64.rpm dest=/upgrade/ owner=root group=root mode=755"
10.0.0.0 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "7d9868a58c2e2aa6a434c4fb8819c0eebedd391d",
"dest": "/upgrade/php-5.4.16-42.el7.x86_64.rpm",
"gid": 0,
"group": "root",
"md5sum": "0334e09f8c56b261eb7f762bcc027f89",
"mode": "0755",
"owner": "root",
"size": 1419816,
"src": "/root/.ansible/tmp/ansible-tmp-1636009694.72-34754-140290725324375/source",
"state": "file",
"uid": 0
}
3.5 checksum文件對(duì)比校驗(yàn)迄损。
[root@dbjf-istp-lvs2 down]# sha1sum php-5.4.16-42.el7.x86_64.rpm
7d9868a58c2e2aa6a434c4fb8819c0eebedd391d php-5.4.16-42.el7.x86_64.rpm
[root@dbjf-istp-lvs2 down]# ansible web_jboss_gmjj -m stat -a "path=/upgrade/php-5.4.16-42.el7.x86_64.rpm"
10.0.0.111 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"stat": {
"atime": 1636009555.3919408,
"charset": "binary",
"checksum": "7d9868a58c2e2aa6a434c4fb8819c0eebedd391d",
"ctime": 1636009555.3969407,
"isuid": false,
"mimetype": "application/x-rpm",
"mode": "0755",
"mtime": 1636009555.065942,
"nlink": 1,
"path": "/upgrade/php-5.4.16-42.el7.x86_64.rpm",
"pw_name": "root",
"readable": true,
"size": 1419816,
}
3.6 下載文件到指定目錄:
ansible all -m get_url -a "url=http://www.mp4.com dest=/tmp/mp4 mode=0440 force=yes"
3.7 解壓ansible管理機(jī)上的壓縮文件到遠(yuǎn)程主機(jī):
ansible all -m unarchive -a "src=/tmp/install/zabbix-3.0.4.tar.gz dest=/tmp/ mode=0755 copy=yes"
3.8 解壓遠(yuǎn)程主機(jī)上的文件到目錄:
ansible all -m unarchive -a "src=/tmp/install/zabbix-3.0.4.tar.gz dest=/tmp/ mode=0755 copy=no"
4 playbook
將多個(gè)play組織在一個(gè)playbook中,即可以讓它們聯(lián)合起來(lái)按事先編排的機(jī)制完成某一任務(wù)辞嗡。
Playbook核心元素
- Hosts 執(zhí)行的遠(yuǎn)程主機(jī)列表
- Tasks捆等,playbook的核心,定義順序執(zhí)行的動(dòng)作 action续室,調(diào)用ansbile module栋烤。
- Varniables 內(nèi)置變量或自定義變量在playbook中調(diào)用
- Templates 模板,即使用模板語(yǔ)法的文件挺狰,比如配置文件等
- Handlers 和notity結(jié)合使用明郭,由特定條件觸發(fā)的操作,滿足條件方才執(zhí)行她渴,否則不執(zhí)行
Handlers will only be fired for tasks which report a changed state.
只有當(dāng) task 執(zhí)行狀態(tài)顯示是 changed 時(shí)达址,handler 動(dòng)作才會(huì)被觸發(fā)
handlers是按照在handlers中定義個(gè)順序執(zhí)行的,而不是安裝notify的順序執(zhí)行的趁耗。
- tags 標(biāo)簽沉唠,指定某條任務(wù)執(zhí)行,用于選擇運(yùn)行playbook中的部分代碼苛败。
[root@dbjf-istp-lvs2 ansible-work]# vi playbooks/web.yml
- hosts: webservers
vars:
worker_processes: 4
num_cpus: 4
max_open_files: 65535
root: /upgrade
remote_user: root
tasks:
- name: make sure ntpd is running
service: name=ntpd state=started
Tips:
當(dāng)tasks 參數(shù)比較多時(shí)满葛,為了美觀和不易出錯(cuò)径簿,建議用 yml的字典傳參:
tasks:
- name: make sure ntpd is running
service:
name: ntpd
state: started
playbook語(yǔ)法:
ansible-playbook --** file.yml
-i hosts
-i hosts -l group1
-C
--list-tasks
--list-hosts #列出目標(biāo)主機(jī)
--list-tags
--syntax-check
-f 10 #并行處理
--syntax-check #語(yǔ)法檢驗(yàn)
yml語(yǔ)法一覽:
- 大小寫敏感
- 使用縮進(jìn)表示層級(jí)關(guān)系.不允許使用Tab鍵,只允許使用空格嘀韧。
- 縮進(jìn)的空格數(shù)目不重要篇亭,只要相同層級(jí)的元素左側(cè)對(duì)齊即可!
- '#'表示注釋,從這個(gè)字符一直到行尾
- 開(kāi)始行是
---
. 這是 YAML 格式的一部分, 表明一個(gè)文件的開(kāi)始 - 列表中的所有成員都開(kāi)始于相同的縮進(jìn)級(jí)別, 并且使用一個(gè)
"- "
作為開(kāi)頭(一個(gè)橫杠和一個(gè)空格) - 一個(gè)字典是由一個(gè)簡(jiǎn)單的 鍵: 值 的形式組成(這個(gè)冒號(hào)后面必須是一個(gè)空格)
- Ansible 使用
“{{ var }}”
來(lái)引用變量锄贷,foo: "{{ variable }}"
译蒂。
關(guān)閉facts
不收集系統(tǒng)信息可以加快執(zhí)行效率。
- hosts: all
gather_facts: no
變量的注冊(cè)和調(diào)試
---
- hosts: all
tasks:
- name: ls /tmp
shell: ls -l /tmp
register: result
ignore_errors: True
- name: echo result when rc==5
shell: echo "{{result}}"
when: result.rc == 5
- name: debug show stdout
debug:
msg: "{{result.stdout}}"