網站統(tǒng)計查詢中國婚戀網站排名
playbook劇本
- PlayBook
- 1.playbooks 本身由以下各部分組成
- 2.示例:
- 3.運行playbook
- 補充參數:
- 4.定義、引用變量
- 5.指定遠程主機sudo切換用戶
- 6.when條件判斷
- 7.迭代
- 8.Templates 模塊
- 1.先準備一個以 .j2 為后綴的 template 模板文件,設置引用的變量
- 2.修改主機清單文件,使用主機變量定義一個變量名相同,而值不同的變量
- 3.編寫 playbook
- 9.tags 模塊
- 10.總結
PlayBook
1.playbooks 本身由以下各部分組成
- (1)Tasks:任務,即通過 task 調用 ansible 的模塊將多個操作組織在一個 playbook 中運行
- (2)Variables:變量
- (3)Templates:模板
- (4)Handlers:處理器,當changed狀態(tài)條件滿足時,(notify)觸發(fā)執(zhí)行的操作
- (5)Roles:角色
2.示例:
vim test1.yaml
--- #yaml文件以---開頭,以表明這是一個yaml文件,可省略
- name: first play #定義一個play的名稱,可省略gather_facts: false #設置不進行facts信息收集,這可以加快執(zhí)行速度,可省略hosts: webservers #指定要執(zhí)行任務的被管理主機組,如多個主機組用冒號分隔remote_user: root #指定被管理主機上執(zhí)行任務的用戶tasks: #定義任務列表,任務列表中的各任務按次序逐個在hosts中指定的主機上執(zhí)行- name: test connection #自定義任務名稱ping: #使用 module: [options] 格式來定義一個任務- name: disable selinuxcommand: '/sbin/setenforce 0' #command模塊和shell模塊無需使用key=value格式ignore_errors: True #如執(zhí)行命令的返回值不為0,就會報錯,tasks停止,可使用ignore_errors忽略失敗的任務- name: disable firewalldservice: name=firewalld state=stopped #使用 module: options 格式來定義任務,option使用key=value格式- name: install httpdyum: name=httpd state=latest- name: install configuration file for httpdcopy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf #這里需要一個事先準備好的/opt/httpd.conf文件notify: "restart httpd" #如以上操作后為changed的狀態(tài)時,會通過notify指定的名稱觸發(fā)對應名稱的handlers操作- name: start httpd serviceservice: enabled=true name=httpd state=startedhandlers: #handlers中定義的就是任務,此處handlers中的任務使用的是service模塊- name: restart httpd #notify和handlers中任務的名稱必須一致service: name=httpd state=restarted
==Ansible在執(zhí)行完某個任務之后并不會立即去執(zhí)行對應的handler,而是在當前play中所有普通任務都執(zhí)行完后再去執(zhí)行handler,這樣的好處是可以多次觸發(fā)notify,但最后只執(zhí)行一次對應的handler,從而避免多次重啟。
3.運行playbook
ansible-playbook test1.yaml
補充參數:
參數 | 含義 |
---|---|
-k(–ask-pass) | 用來交互輸入ssh密碼 |
-K(-ask-become-pass) | 用來交互輸入sudo密碼 |
-u | 指定用戶 |
ansible-playbook test1.yaml --syntax-check
#檢查yaml文件的語法是否正確
ansible-playbook test1.yaml --list-task
#檢查tasks任務
ansible-playbook test1.yaml --list-hosts
#檢查生效的主機
ansible-playbook test1.yaml --start-at-task='install httpd'
#指定從某個task開始運行
4.定義、引用變量
- name: second playhosts: dbserversremote_user: rootvars: #定義變量- groupname: mysql #格式為 key: value- username: nginxtasks:- name: create groupgroup: name={{groupname}} system=yes gid=306 #使用 {{key}} 引用變量的值- name: create useruser: name={{username}} uid=306 group={{groupname}} - name: copy filecopy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt #在setup模塊中可以獲取facts變量信息
ansible-playbook test1.yaml -e "username=nginx"
#在命令行里定義變量
5.指定遠程主機sudo切換用戶
---
- hosts: dbserversremote_user: zhangsan become: yes #2.6版本以后的參數,之前是sudo,意思為切換用戶運行become_user: root #指定sudo用戶為root
執(zhí)行playbook時:ansible-playbook test1.yml -k -K
6.when條件判斷
在Ansible中,提供的唯一一個通用的條件判斷是when指令,當when指令的值為true時,則該任務執(zhí)行,否則不執(zhí)行該任務。
//when一個比較常見的應用場景是實現跳過某個主機不執(zhí)行任務或者只有滿足條件的主機執(zhí)行任務
```bash
vim test2.yaml
---
- hosts: allremote_user: roottasks:- name: shutdown host command: /sbin/shutdown -r nowwhen: ansible_default_ipv4.address == "192.168.80.12" #when指令中的變量名不需要手動加上 {{}}
或 when: inventory_hostname == "<主機名>"
ansible-playbook test2.yaml
7.迭代
Ansible提供了很多種循環(huán)結構,一般都命名為with_items,作用等同于 loop 循環(huán)。
vim test3.yaml
---
- name: play1hosts: dbserversgather_facts: falsetasks: - name: create filefile:path: "{{item}}"state: touchwith_items: [ /opt/a, /opt/b, /opt/c, /opt/d ]- name: play2hosts: dbserversgather_facts: false vars:test:- /tmp/test1- /tmp/test2- /tmp/test3- /tmp/test4tasks: - name: create directoriesfile:path: "{{item}}"state: directorywith_items: "{{test}}"- name: play3hosts: dbserversgather_facts: falsetasks:- name: add usersuser: name={{item.name}} state=present groups={{item.groups}}with_items:- name: test1groups: wheel- name: test2groups: root
或with_items:- {name: 'test1', groups: 'wheel'}- {name: 'test2', groups: 'root'}
ansible-playbook test3.yaml
8.Templates 模塊
Jinja是基于Python的模板引擎。Template類是Jinja的一個重要組件,可以看作是一個編譯過的模板文件,用來產生目標文本,傳遞Python的變量給模板去替換模板中的標記。
1.先準備一個以 .j2 為后綴的 template 模板文件,設置引用的變量
cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2vim /opt/httpd.conf.j2
Listen {{http_port}} #42行,修改
ServerName {{server_name}} #95行,修改
DocumentRoot "{{root_dir}}" #119行,修改
2.修改主機清單文件,使用主機變量定義一個變量名相同,而值不同的變量
vim /etc/ansible/hosts
[webservers]
192.168.52.110 http_port=192.168.52.110:80 server_name=www.accp.com:80 root_dir=/etc/httpd/htdocs[dbservers]
192.168.52.100 http_port=192.168.52.100:80 server_name=www.benet.com:80 root_dir=/etc/httpd/htdocs
3.編寫 playbook
vim apache.yaml
---
- hosts: allremote_user: rootvars:- package: httpd- service: httpdtasks:- name: install httpd packageyum: name={{package}} state=latest- name: install configure filetemplate: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #使用template模板notify:- restart httpd- name: create root dirfile: path=/etc/httpd/htdocs state=directory- name: start httpd serverservice: name={{service}} enabled=true state=startedhandlers:- name: restart httpdservice: name={{service}} state=restarted
ansible-playbook apache.yaml
9.tags 模塊
可以在一個playbook中為某個或某些任務定義“標簽”,在執(zhí)行此playbook時通過ansible-playbook命令使用–tags選項能實現僅運行指定的tasks。
playbook還提供了一個特殊的tags為always。作用就是當使用always作為tags的task時,無論執(zhí)行哪一個tags時,定義有always的tags都會執(zhí)行。
vim webhosts.yaml
---
- hosts: webserversremote_user: roottasks:- name: Copy hosts filecopy: src=/etc/hosts dest=/opt/hoststags:- only #可自定義- name: touch filefile: path=/opt/testhost state=touchtags:- always #表示始終要運行的代碼ansible-playbook webhosts.yaml --tags="only"
vim dbhosts.yaml
---
- hosts: dbserversremote_user: roottasks:- name: Copy hosts filecopy: src=/etc/hosts dest=/opt/hoststags:- only- name: touch filefile: path=/opt/testhost state=touch
ansible-playbook dbhosts.yaml --tags="only"
//分別去兩臺被管理主機上去查看文件創(chuàng)建情況
10.總結
vim XXX.yaml
- name: #指定play名稱hosts: #指定主機組remote_user: #執(zhí)行用戶 gather_facts: true|false #是否收集遠程主機facts信息vars: #定義變量tasks: #定義task任務列表- name: #定義task任務名稱模塊: #定義任務使用的模塊和參數with_items: #定義循環(huán)列表when: #定義判斷條件(== != >= > <= <),true則執(zhí)行任務,否則不執(zhí)行任務ignore_errors: true #忽略任務失敗notify: #定義task任務changed狀態(tài)時觸發(fā)的任務名tags: #指定標簽,ansible-playbook --tags 僅執(zhí)行擁有指定 tags 標簽的任務(always標簽總會執(zhí)行)handlers: #定義notify觸發(fā)的任務列表
task任務模塊語法格式
橫向格式:
模塊名: 參數選項1=值 參數選項2={{變量名}} ...縱向格式:
模塊名:參數選項1: 值參數選項2: "{{變量名}}"...
with_items和變量的語法格式
橫向格式:
with_items: ["值1", "值2", "值3"]值為對象(鍵值對字段)時:
with_items:
- {key1: value1, key2: value2, ...}
- {key1: value3, key2: value4, ...}縱向格式:
with_items:
- 值1
- 值2
- 值3值為對象(鍵值對字段)時:
with_items:
- key1: value1key2: value2
- key1: value3key2: value4
template模板模塊
(1)先要準備一個xxx.j2模板文件,在文件中使用 {{變量名}} 引用主機變量 或者 vars 自定義的變量 及 facts 字段的值
(2)在playbook中的tasks中定義template模板配置 template: src=xxx.j2 dest=xxx