一、Ansible 的腳本 playbook 劇本
1.1playbooks的組成
(1)Tasks:任務(wù),即通過 task 調(diào)用 ansible 的模板將多個操作組織在一個 playbook 中運(yùn)行
(2)Variables:變量
(3)Templates:模板
(4)Handlers:處理器,當(dāng)changed狀態(tài)條件滿足時,(notify)觸發(fā)執(zhí)行的操作
(5)Roles:角色
二、劇本編寫實(shí)驗(yàn)
vim test1.yaml
--- #yaml文件以---開頭,以表明這是一個yaml文件,可省略
- name: first play #定義一個play的名稱,可省略
gather_facts: false #設(shè)置不進(jìn)行facts信息收集,這可以加快執(zhí)行速度,可省略
hosts: webservers #指定要執(zhí)行任務(wù)的被管理主機(jī)組,如多個主機(jī)組用冒號分隔
remote_user: root #指定被管理主機(jī)上執(zhí)行任務(wù)的用戶
tasks: #定義任務(wù)列表,任務(wù)列表中的各任務(wù)按次序逐個在hosts中指定的主機(jī)上執(zhí)行
- name: test connection #自定義任務(wù)名稱
ping: #使用 module: [options] 格式來定義一個任務(wù)
- name: disable selinux
command: '/sbin/setenforce 0' #command模塊和shell模塊無需使用key=value格式
ignore_errors: True #如執(zhí)行命令的返回值不為0,就會報錯,tasks停止,可使用ignore_errors忽略失敗的任務(wù)
- name: disable firewalld
service: name=firewalld state=stopped #使用 module: options 格式來定義任務(wù),option使用key=value格式
- name: install httpd
yum: name=httpd state=latest
- name: install configuration file for httpd
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf #這里需要一個事先準(zhǔn)備好的/opt/httpd.conf文件
notify: "restart httpd" #如以上操作后為changed的狀態(tài)時,會通過notify指定的名稱觸發(fā)對應(yīng)名稱的handlers操作
- name: start httpd service
service: enabled=true name=httpd state=started
handlers: #handlers中定義的就是任務(wù),此處handlers中的任務(wù)使用的是service模塊
- name: restart httpd #notify和handlers中任務(wù)的名稱必須一致
service: name=httpd state=restarted
##Ansible在執(zhí)行完某個任務(wù)之后并不會立即去執(zhí)行對應(yīng)的handler,而是在當(dāng)前play中所有普通任務(wù)都執(zhí)行完后再去執(zhí)行handler,這樣的好處是可以多次觸發(fā)notify,但最后只執(zhí)行一次對應(yīng)的handler,從而避免多次重啟。
運(yùn)行playbook
ansible-playbook test1.yaml
//補(bǔ)充參數(shù):
-k(–ask-pass):用來交互輸入ssh密碼
-K(-ask-become-pass):用來交互輸入sudo密碼
-u:指定用戶
ansible-playbook test1.yaml --syntax-check #檢查yaml文件的語法是否正確
ansible-playbook test1.yaml --list-task #檢查tasks任務(wù)
ansible-playbook test1.yaml --list-hosts #檢查生效的主機(jī)
ansible-playbook test1.yaml --start-at-task='install httpd' #指定從某個task開始運(yùn)行
2.1定義、引用變量
- name: second play
hosts: dbservers
remote_user: root
vars: #定義變量
- groupname: mysql #格式為 key: value
- username: nginx
tasks:
- name: create group
group: name={{groupname}} system=yes gid=306 #使用 {{key}} 引用變量的值
- name: create user
user: name={{username}} uid=306 group={{groupname}}
- name: copy file
copy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt #在setup模塊中可以獲取facts變量信息
ansible-playbook test1.yaml -e "username=nginx" #在命令行里定義變量
2.2使用遠(yuǎn)程主機(jī)sudo切換用戶
- hosts: dbservers
remote_user: zhangsan
become: yes #2.6版本以后的參數(shù),之前是sudo,意思為切換用戶運(yùn)行
become_user: root #指定sudo用戶為root
執(zhí)行playbook時:ansible-playbook test1.yml -k -K
2.3whenn條件判斷
- name: three play
hosts: all
remote_user: root
tasks:
- name: create file
file: path=/opt/abc.txt state=touch
when: ansible_default_ipv4.address=="192.168.243.103" #通過facts收集的信息過濾出匹配的主機(jī),when中的變量名字,不需要手動加{{}}
或
when: inventory_hostname == "<主機(jī)名>"
ansible-playbook test2.yaml
2.4迭代
- Ansible提供了很多種循環(huán)結(jié)構(gòu),一般都命名為with_items,作用等同于 loop 循環(huán)。
- name: play1
hosts: dbservers
gather_facts: false
tasks:
- name: create file
file:
path: "{{item}}"
state: touch
with_items: [ /opt/a, /opt/b, /opt/c, /opt/d ]
- name: play2
hosts: dbservers
gather_facts: false
vars:
test:
- /tmp/test1
- /tmp/test2
- /tmp/test3
- /tmp/test4
tasks:
- name: create directories
file:
path: "{{item}}"
state: directory
with_items: "{{test}}"
- name: play3
hosts: dbservers
gather_facts: false
tasks:
- name: add users
user: name={{item.name}} state=present groups={{item.groups}}
with_items:
- name: test1
groups: wheel
- name: test2
groups: root
或
with_items:
- {name: 'test1', groups: 'wheel'}
- {name: 'test2', groups: 'root'}
ansible-playbook test3.yaml
三、Templates 模板
- Jinja是基于Python的模板引擎。Template類是Jinja的一個重要組件,可以看作是一個編譯過的模板文件,用來產(chǎn)生目標(biāo)文本,傳遞Python的變量給模板去替換模板中的標(biāo)記。
1.先準(zhǔn)備一個以 .j2 為后綴的 template 模板文件,設(shè)置引用的變量
cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2
vim /opt/httpd.conf.j2
Listen {{http_port}} #42行,修改
ServerName {{server_name}} #95行,修改
DocumentRoot "{{root_dir}}" #119行,修改
2.修改主機(jī)清單文件,使用主機(jī)變量定義一個變量名相同,而值不同的變量
vim /etc/ansible/hosts
[webservers]
192.168.80.11 http_port=192.168.80.11:80 server_name=www.accp.com:80 root_dir=/etc/httpd/htdocs
[dbservers]
192.168.80.12 http_port=192.168.80.12:80 server_name=www.benet.com:80 root_dir=/etc/httpd/htdocs
3.編寫 playbook
vim apache.yaml
---
- hosts: all
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{package}} state=latest
- name: install configure file
template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #使用template模板
notify:
- restart httpd
- name: create root dir
file: path=/etc/httpd/htdocs state=directory
- name: start httpd server
service: name={{service}} enabled=true state=started
handlers:
- name: restart httpd
service: name={{service}} state=restarted
ansible-playbook apache.yaml
文章來源:http://www.zghlxwxcb.cn/news/detail-617807.html
四、Tags模板
- 可以在一個playbook中為某個或某些任務(wù)定義“標(biāo)簽”,在執(zhí)行此playbook時通過ansible-playbook命令使用–tags選項(xiàng)能實(shí)現(xiàn)僅運(yùn)行指定的tasks。
playbook還提供了一個特殊的tags為always。作用就是當(dāng)使用always作為tags的task時,無論執(zhí)行哪一個tags時,定義有always的tags都會執(zhí)行。
- name: serve play
remote_user: root
hosts: webservers
tasks:
- name: touch ddd,txt
file: path=/opt/ddd.txt state=touch
tags: #標(biāo)簽為ddd當(dāng)命令tags標(biāo)簽為ddd,或者沒有標(biāo)簽才執(zhí)行
- ddd
- name: touch 123.txt
file: path=/opt/123.txt state=touch
tags:
- always #alwats表示都執(zhí)行
- name: touch abc.txt
file: path=/opt/abc.txt state=touch
tags:
- aaa #同上
文章來源地址http://www.zghlxwxcb.cn/news/detail-617807.html
到了這里,關(guān)于【自動化運(yùn)維】playbook劇本的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!