一、playbook的相關(guān)知識
1.playbook簡介
playbook是 一個不同于使用Ansible命令行執(zhí)行方式的模式,其功能更強大靈活。簡單來說,playbook是一個非常簡單的配置管理和多主機部署系統(tǒng),不同于任何已經(jīng)存在的模式,可作為一個適合部署復(fù)雜應(yīng)用程序的基礎(chǔ)。Playbook可以定制配置,可以按照指定的操作步驟有序執(zhí)行,支持同步和異步方式。我們完成一個任務(wù),例如安裝部署一個httpd服務(wù),我們需要多個模塊(一個模塊也可以稱之為task)提供功能來完成。而playbook就是組織多個task的容器,他的實質(zhì)就是一個文件,有著特定的組織格式,它采用的語法格式是YAML(Yet Another Markup Language)。
2.playbook的?各部分組成
(1)Tasks:任務(wù),即通過 task 調(diào)用 ansible 的模板將多個操作組織在一個 playbook 中運行
(2)Variables:變量
(3)Templates:模板
(4)Handlers:處理器,當(dāng)changed狀態(tài)條件滿足時,(notify)觸發(fā)執(zhí)行的操作
(5)Roles:角色
二、基礎(chǔ)的playbook劇本編寫
playbook中運用的模塊就是ansible中的模塊,就像docker-compose一樣將docker操作容器的指令歸納為一個yaml文件,開啟運行yaml中的指令模塊就能按照預(yù)設(shè)計的方向去完成。?
1.playbook編寫 apache的yum部署劇本?
劇本編寫實現(xiàn)的需求:對Ansible管理的所有的webservers組的成員,yum安裝最新版本的apache服務(wù)軟件,并進行相應(yīng)環(huán)境的調(diào)整,確保webservers的apache服務(wù)能夠正常運行并設(shè)置開機自啟?
cd /etc/ansible #在ansible的所在目錄中創(chuàng)建該項目的目錄
mkdir apache
vim apache.yaml
---
- name: apache yum apply
gather_facts: false
hosts: webservers
remote_user: root
tasks:
- name: test connection
ping:
- name: stop firewalld
service: name=firewalld state=stopped
- name: stop selinux
command: '/usr/sbin/setenforce 0'
ignore_errors: true
- name: yum install apache service
yum: name=httpd state=latest
- name: start apache service
service: name=httpd state=started enabled=yes
?
?運行劇本:
//運行playbook
ansible-playbook apache.yaml
//補充參數(shù):
-k(–ask-pass):用來交互輸入ssh密碼
-K(-ask-become-pass):用來交互輸入sudo密碼
-u:指定用戶
ansible-playbook apache.yaml --syntax-check 檢查yaml文件的語法是否正確
ansible-playbook apache.yaml --list-task 檢查tasks任務(wù)
ansible-playbook apache.yaml --list-hosts 檢查生效的主機
ansible-playbook apache.yaml --start-at-task='install httpd' 指定從某個task開始運行
?
?
?2.playbook編寫nginx 的yum安裝并且能修改其監(jiān)聽端口的劇本
需求:通過yum安裝nginx服務(wù),并且能夠控制被管理的主機的服務(wù)的開啟,按照預(yù)設(shè)的配置在運行時的端口。
在編寫劇本前,需要準(zhǔn)備相應(yīng)的兩個文件,一個為nginx的yum源。一個為相對應(yīng)的主配置文件,在主配置文件中修改其端口,在將該配置移至被管理主機中,作為運行啟動時的默認(rèn)配置??
mkdir /etc/ansible/nginx
vim nginx.yaml
---
- name: nginx script
gather_facts: false
hosts: webservers
remote_user: root
tasks:
- name: test connection
ping:
- name: stop firewalld
service: name=firewalld state=stopped enabled=no
- name: stop selinux
command: '/usr/sbin/setenforce 0'
ignore_errors: true
- name: prepare nginx repo
copy: src=/etc/ansible/nginx/nginx.repo dest=/etc/yum.repos.d/nginx.repo
- name: install nginx
yum: name=nginx state=latest
- name: change port
copy: src=/opt/default.conf dest=/etc/nginx/conf.d/default.conf
notify: "restart nginx"
- name: start nginx
service: name=nginx state=started enabled=yes
handlers:
- name: restart nginx
service: name=nginx state=restarted
?
?運行結(jié)果:
?
?
?三、playbook的定義、引用變量
基礎(chǔ)變量的定義與引用
在yaml文件中,我們可以在初始配置的模塊中用var去定義變量的存在,變量的格式為key:value,以此來確定該變量在劇本中的存在?
vim test.yaml
---
- name: this is a play for testing variables
hosts: dbservers
remote_user: root
vars:
filename: cx.txt
tasks:
- name: touch a learn file
file: path=/opt/{{filename}} state=touch
ansible-playbook test.yaml
?
?
?四、playbook中的when條件判斷和變量循環(huán)使用
1.when條件判斷
#選用filter=ansible_default_ipv4中的address作為when條件進行測試
ansible all -m setup -a 'filter=ansible_default_ipv4'
?測試劇本編寫:
vim test2.yaml
---
- name: this is when test playbook
hosts: all
remote_user: root
tasks:
- name: test when
debug: msg='判斷位置'
when: ansible_default_ipv4.address == "192.168.190.20"
ansible-playbook test2.yaml
?除此之外 when條件還可以通過?!=(不等于條件來進行判斷)
vim test3.yaml
---
- name: this is when test playbook
hosts: all
remote_user: root
tasks:
- name: test when
debug: msg='判斷位置'
when: ansible_default_ipv4.address != "192.168.190.20"
ansible-playbook test3.yaml
2.變量循環(huán)
1)with_item 單循環(huán)輸出
vim test4.yaml
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_items: [a, b, c, d]
ansible-playbook test4.yaml
?當(dāng)列表為兩個時。with_item的輸出方式:
vim test4.yaml
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_items:
- [a, b, c, d]
- [1 ,2, 3, 4]
ansible-playbook test4.yaml
?2)with_together?同一列表位置數(shù)據(jù)組合輸出的循環(huán)
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_together:
- [a, b, c, d]
- [1 ,2, 3, 4]
- [A, B, C]
??
3) with_nested 列表數(shù)據(jù)循環(huán)匹配的循環(huán)(根據(jù)列表個數(shù)定義有多少層的循環(huán))?
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_nested:
- [a, b, c, d]
- [1 ,2, 3, 4]
~
4)with_list? 每組列表一起循環(huán)的輸出
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_list:
- [a, b, c, d]
- [1 ,2, 3, 4]
~
~
?四種迭代循環(huán)方式的總結(jié)
?whith_items: ?{{item}}會把所有的列表展開進行遍歷輸出,with_flattened也可以替代with_items
?with_list: ? ?{{item}}會把每個列表當(dāng)作一個整體輸出。如果每個列表中只有一個值,則效果與with items一致。loop也可以替代ith
?with_together: {{item}}引用時會把每個列表相同位置的值對齊合并后輸出
with nested:{ {item}}引用時會把每個列表的值兩兩組合循環(huán)輸出
五、Tags
可以在一個playbook中為某個或某些任務(wù)定義“標(biāo)簽”,在執(zhí)行此playbook時通過ansible-playbook命令使用--tags選項能實現(xiàn)僅運行指定的tasks。
playbook還提供了一個特殊的tags為always。作用就是當(dāng)使用always作為tags的task時,無論執(zhí)行哪一個tags時,定義有always的tags都會執(zhí)行。
?1.單標(biāo)簽的使用
vim test1.yaml
---
- name: this is a play for testing variables
hosts: dbservers
remote_user: root
vars:
filename: abc.txt
tasks:
- name: position 1
debug:
msg: 'ls /opt'
tags:
- only
- name: position 2
debug:
msg: 'ls /mnt'
ansible-playbook test1.yaml --tags="only"
??
2.通用標(biāo)簽always的運用?
---
- name: this is a play for testing variables
hosts: dbservers
remote_user: root
vars:
filename: abc.txt
tasks:
- name: position 1
debug:
msg: '測試標(biāo)簽1'
tags:
- one
- name: position 2
debug:
msg: '測試通用標(biāo)簽always'
tags:
- always
- name: position 3
debug:
msg: '測試標(biāo)簽3'
tags:
- one
??
七、?roles?
- Roles又稱為角色,playbook被稱為劇本。Roles角色是自1.2版本之后引入的新特性,用于層次性、結(jié)構(gòu)化的組織劇本
- ?
- roles能夠根據(jù)層次型結(jié)構(gòu)自動裝載變量文件、任務(wù)集、以及觸發(fā)的動作等,要使用roles只需要在劇本中使用include命令引入即可
- 簡單的來說,roles就是分別將變量、文件、任務(wù)、模板以及處理器放置于不同的單獨的目錄,并且可以便捷的通過include引入
- 角色一般用于基于主機構(gòu)建的服務(wù)的場景中,但是也可以是用于構(gòu)建守護進程等場景中,主要是使用在代碼復(fù)用度較高的場景?
???●files
用來存放由 copy 模塊或 script 模塊調(diào)用的文件。
●templates
用來存放 jinjia2 模板,template 模塊會自動在此目錄中尋找 jinjia2 模板文件。
●tasks
此目錄應(yīng)當(dāng)包含一個 main.yml 文件,用于定義此角色的任務(wù)列表,此文件可以使用 include 包含其它的位于此目錄的 task 文件。
●handlers
此目錄應(yīng)當(dāng)包含一個 main.yml 文件,用于定義此角色中觸發(fā)條件時執(zhí)行的動作。
●vars
此目錄應(yīng)當(dāng)包含一個 main.yml 文件,用于定義此角色用到的變量。
●defaults
此目錄應(yīng)當(dāng)包含一個 main.yml 文件,用于為當(dāng)前角色設(shè)定默認(rèn)變量。文章來源:http://www.zghlxwxcb.cn/news/detail-624989.html
●meta
此目錄應(yīng)當(dāng)包含一個 main.yml 文件,用于定義此角色的特殊設(shè)定及其依賴關(guān)系。文章來源地址http://www.zghlxwxcb.cn/news/detail-624989.html
集中式lamp的簡單role編寫過程:?
//在一個 playbook 中使用 roles 的步驟:
(1)創(chuàng)建以 roles 命名的目錄
mkdir /etc/ansible/roles/ -p #yum裝完默認(rèn)就有
(2)創(chuàng)建全局變量目錄(可選)
mkdir /etc/ansible/group_vars/ -p
touch /etc/ansible/group_vars/all #文件名自己定義,引用的時候注意
(3)在 roles 目錄中分別創(chuàng)建以各角色名稱命名的目錄,如 httpd、mysql
mkdir /etc/ansible/roles/httpd
mkdir /etc/ansible/roles/mysql
(4)在每個角色命名的目錄中分別創(chuàng)建files、handlers、tasks、templates、meta、defaults和vars目錄,用不到的目錄可以創(chuàng)建為空目錄,也可以不創(chuàng)建
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta}
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta}
(5)在每個角色的 handlers、tasks、meta、defaults、vars 目錄下創(chuàng)建 main.yml 文件,千萬不能自定義文件名
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
(6)修改 site.yml 文件,針對不同主機去調(diào)用不同的角色
vim /etc/ansible/site.yml
---
- hosts: webservers
remote_user: root
roles:
- httpd
- hosts: dbservers
remote_user: root
roles:
- mysql
(7)運行 ansible-playbook
cd /etc/ansible
ansible-playbook site.yml
示例:
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml
------編寫httpd模塊------
寫一個簡單的tasks/main.yml
vim /etc/ansible/roles/httpd/tasks/main.yml
- name: install apache
yum: name={{pkg}} state=latest
- name: start apache
service: enabled=true name={{svc}} state=started
//定義變量:可以定義在全局變量中,也可以定義在roles角色變量中,一般定義在角色變量中
vim /etc/ansible/roles/httpd/vars/main.yml
pkg: httpd
svc: httpd
-------編寫mysql模塊-------
vim /etc/ansible/roles/mysql/tasks/main.yml
- name: install mysql
yum: name={{pkg}} state=latest
- name: start mysql
service: enabled=true name={{svc}} state=started
vim /etc/ansible/roles/mysql/vars/main.yml
pkg:
- mariadb
- mariadb-server
svc: mariadb
-------編寫php模塊-----
vim /etc/ansible/roles/php/tasks/main.yml
- name: install php
yum: name={{pkg}} state=latest
- name: start php-fpm
service: enabled=true name={{svc}} state=started
vim /etc/ansible/roles/php/vars/main.yml
pkg:
- php
- php-fpm
svc: php-fpm
-----編寫roles示例-----
vim /etc/ansible/site.yml
---
- hosts: webservers
remote_user: root
roles:
- httpd
- mysql
- php
cd /etc/ansible
ansible-playbook site.yml
到了這里,關(guān)于Ansible之playbook劇本編寫的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!