做網(wǎng)站實(shí)名認(rèn)證總是失敗怎么回事我們seo
目錄
- 一、Ansible 的腳本 playbook 劇本
- 1.1playbooks的組成
- 二、劇本編寫實(shí)驗(yàn)
- 2.1定義、引用變量
- 2.2使用遠(yuǎn)程主機(jī)sudo切換用戶
- 2.3whenn條件判斷
- 2.4迭代
- 三、Templates 模板
- 四、Tags模板
一、Ansible 的腳本 playbook 劇本
1.1playbooks的組成
(1)Tasks:任務(wù),即通過 task 調(diào)用 ansible 的模板將多個(gè)操作組織在一個(gè) playbook 中運(yùn)行
(2)Variables:變量
(3)Templates:模板
(4)Handlers:處理器,當(dāng)changed狀態(tài)條件滿足時(shí),(notify)觸發(fā)執(zhí)行的操作
(5)Roles:角色
二、劇本編寫實(shí)驗(yàn)
vim test1.yaml
--- #yaml文件以---開頭,以表明這是一個(gè)yaml文件,可省略
- name: first play #定義一個(gè)play的名稱,可省略gather_facts: false #設(shè)置不進(jìn)行facts信息收集,這可以加快執(zhí)行速度,可省略hosts: webservers #指定要執(zhí)行任務(wù)的被管理主機(jī)組,如多個(gè)主機(jī)組用冒號分隔remote_user: root #指定被管理主機(jī)上執(zhí)行任務(wù)的用戶tasks: #定義任務(wù)列表,任務(wù)列表中的各任務(wù)按次序逐個(gè)在hosts中指定的主機(jī)上執(zhí)行- name: test connection #自定義任務(wù)名稱ping: #使用 module: [options] 格式來定義一個(gè)任務(wù)- name: disable selinuxcommand: '/sbin/setenforce 0' #command模塊和shell模塊無需使用key=value格式ignore_errors: True #如執(zhí)行命令的返回值不為0,就會報(bào)錯(cuò),tasks停止,可使用ignore_errors忽略失敗的任務(wù)- name: disable firewalldservice: name=firewalld state=stopped #使用 module: options 格式來定義任務(wù),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 #這里需要一個(gè)事先準(zhǔn)備好的/opt/httpd.conf文件notify: "restart httpd" #如以上操作后為changed的狀態(tài)時(shí),會通過notify指定的名稱觸發(fā)對應(yīng)名稱的handlers操作- name: start httpd serviceservice: enabled=true name=httpd state=startedhandlers: #handlers中定義的就是任務(wù),此處handlers中的任務(wù)使用的是service模塊- name: restart httpd #notify和handlers中任務(wù)的名稱必須一致service: name=httpd state=restarted
##Ansible在執(zhí)行完某個(gè)任務(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' #指定從某個(gè)task開始運(yùn)行
2.1定義、引用變量
- 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" #在命令行里定義變量
2.2使用遠(yuǎn)程主機(jī)sudo切換用戶
- hosts: dbserversremote_user: zhangsan become: yes #2.6版本以后的參數(shù),之前是sudo,意思為切換用戶運(yùn)行become_user: root #指定sudo用戶為root
執(zhí)行playbook時(shí):ansible-playbook test1.yml -k -K
2.3whenn條件判斷
- name: three playhosts: allremote_user: roottasks:- name: create filefile: path=/opt/abc.txt state=touchwhen: ansible_default_ipv4.address=="192.168.243.103" #通過facts收集的信息過濾出匹配的主機(jī),when中的變量名字,不需要手動(dòng)加{{}}或 when: inventory_hostname == "<主機(jī)名>"ansible-playbook test2.yaml
2.4迭代
- Ansible提供了很多種循環(huán)結(jié)構(gòu),一般都命名為with_items,作用等同于 loop 循環(huán)。
- 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
三、Templates 模板
- Jinja是基于Python的模板引擎。Template類是Jinja的一個(gè)重要組件,可以看作是一個(gè)編譯過的模板文件,用來產(chǎn)生目標(biāo)文本,傳遞Python的變量給模板去替換模板中的標(biāo)記。
1.先準(zhǔn)備一個(gè)以 .j2 為后綴的 template 模板文件,設(shè)置引用的變量
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.修改主機(jī)清單文件,使用主機(jī)變量定義一個(gè)變量名相同,而值不同的變量
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: 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=restartedansible-playbook apache.yaml
四、Tags模板
- 可以在一個(gè)playbook中為某個(gè)或某些任務(wù)定義“標(biāo)簽”,在執(zhí)行此playbook時(shí)通過ansible-playbook命令使用–tags選項(xiàng)能實(shí)現(xiàn)僅運(yùn)行指定的tasks。
playbook還提供了一個(gè)特殊的tags為always。作用就是當(dāng)使用always作為tags的task時(shí),無論執(zhí)行哪一個(gè)tags時(shí),定義有always的tags都會執(zhí)行。
- name: serve playremote_user: roothosts: webserverstasks:- name: touch ddd,txtfile: path=/opt/ddd.txt state=touchtags: #標(biāo)簽為ddd當(dāng)命令tags標(biāo)簽為ddd,或者沒有標(biāo)簽才執(zhí)行- ddd- name: touch 123.txtfile: path=/opt/123.txt state=touchtags:- always #alwats表示都執(zhí)行- name: touch abc.txtfile: path=/opt/abc.txt state=touchtags:- aaa #同上