目錄
一、ansible劇本playbook的組成
二、palybook的基礎(chǔ)應(yīng)用:
實(shí)操1:通過palybooks完成nginx的安裝
第一種:通過yum安裝nginx
第二種:通過編譯安裝nginx
實(shí)操2:playbook定義、引用變量???????
實(shí)操3:通過playbook完成普通賬戶權(quán)限提權(quán)為root用戶
實(shí)操4:通過when條件判斷指定主機(jī)完成相應(yīng)的任務(wù)
實(shí)操5:通過with_items循環(huán)實(shí)現(xiàn)在遠(yuǎn)程主機(jī)上關(guān)閉服務(wù)、創(chuàng)建文件和目錄
實(shí)操6:基于templates模塊,完成nginx服務(wù)的配置模板
實(shí)操7:基于tags標(biāo)簽,指定完成特定的任務(wù)模塊
合并:綜合多模塊
三、關(guān)于playbook的知識(shí)點(diǎn)總結(jié)
一、ansible劇本playbook的組成
(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:角色
二、palybook的基礎(chǔ)應(yīng)用:
實(shí)操1:通過palybooks完成nginx的安裝
第一種:通過yum安裝nginx
完成playbook劇本的編寫,并完成語法檢查
#定義劇本名稱為nginx安裝
- name: install nginx
#表示捕收劑facts信息
gather_facts: false
#指定指定此play的遠(yuǎn)程主機(jī)組
hosts: webservers
#指定運(yùn)行此play的用戶
remote_user: root
#指定當(dāng)前play的任務(wù)列表
tasks:
#關(guān)閉防火墻和selinux
- name: disabled firewalld
service: name=firewalld state=stopped enabled=no
- name: disable selinux
command: '/sbin/setenforce 0'
ignore_errors: yes
- name: disabled selinux forever
replace: path=/etc/selinux/config regexp=enforcing replace=disabled after=loaded
#將nginx的repo文件傳輸給遠(yuǎn)程主機(jī)
- name: copy nginx repo
copy: src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo
#通過yum安裝nginx
- name: install nginx by yum
yum: name=nginx state=latest
#安裝nginx
- name: start nginx service
systemd: name=nginx state=started enabled=yes
?
驗(yàn)證
?
第二種:通過編譯安裝nginx
---
- name: second play for install httpd
gather_facts: false
hosts: webservers:dbservers
remote_user: root
tasks:
#關(guān)閉防火墻
- name: disabled firewalld
service: name=firewalld state=stopped enabled=no
#關(guān)閉 selinux
- name: disable selinux
command: '/sbin/setenforce 0'
ignore_errors: yes
- name: disabled selinux forever
replace: path=/etc/selinux/config regexp=enforcing replace=disabled after=loaded
#關(guān)閉和刪除yum安裝的nginx
- name: disabled nginx
service: name=nginx state=stopped enabled=no
ignore_errors: yes
- name: remove nginx
yum: name=nginx state=absent
#準(zhǔn)備本地yum倉庫
yum: name=nginx state=absent
#準(zhǔn)備本地yum倉庫
- name: archive yum online repo
archive: path=/etc/yum.repos.d/*.repo dest=/etc/yum.repos.d/yum_bak.repo.tar.gz format=gz remove=yes
- name: copy local yum repo
copy: src=nginx/local.repo dest=/etc/yum.repos.d/local.repo
- name: mount cdrom
mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
#安裝nginx的依賴環(huán)境包
- name: install pkgs
yum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++,make state=latest
#創(chuàng)建nginx用戶
- name: create user nginx
user: name=nginx create_home=no shell=/sbin/nologin
#解壓軟件包并安裝
- name: unarchive nginx package
unarchive: copy=yes src=nginx/nginx-1.24.0.tar.gz dest=/opt/
- name: install nginx with source
shell: chdir=/opt/nginx-1.24.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
#創(chuàng)建軟連接
- name: create nginx link
file: src=/usr/local/nginx/sbin/nginx path=/usr/sbin/nginx state=link
#創(chuàng)建nginx的service文件
- name: create nginx service file
copy: src=nginx/nginx.service dest=/lib/systemd/system/nginx.service
- name: start nginx
service: name=nginx state=started enabled=yes
實(shí)操2:playbook定義、引用變量???????
---
- name: stop firewalld nfs crond
gather_facts: true
hosts: webservers:dbservers
remote_user: root
#定義變量名為servername值為firewalld
vars:
- servername: firewalld
tasks:
- name: stop firewalld
#調(diào)用vars定義的變量
systemd: name={{servername}} state=stopped enabled=no
- name: copy ipv4 info
#通過調(diào)用收集的facts信息做變量值
copy: content={{ansible_default_ipv4.address}} dest=/opt/ip.txt
縱向?qū)懛?/p>
[root@localhost playbook]#ansible-playbook demo3.yaml -e "servername=crond"
?
實(shí)操3:通過playbook完成普通賬戶權(quán)限提權(quán)為root用戶
---
- name: stop firewalld nfs crond
gather_facts: true
hosts: dbservers
remote_user: xueyin
become: yes
become_user: root
#定義變量名為servername值為firewalld
vars:
- servername: firewalld
tasks:
- name: stop firewalld
#調(diào)用vars定義的變量
systemd:
name: "{{servername}}"
state: stopped
enabled: no
- name: copy ipv4 info
#通過調(diào)用收集的facts信息做變量值
copy:
content: "{{ansible_default_ipv4.address}}"
dest: /opt/ip.txt
[root@localhost ~]#vim /etc/sudoers
不僅在playbook文件中設(shè)置sudo提權(quán) 還需要在對應(yīng)的主機(jī)上實(shí)現(xiàn)sudo提權(quán)
[root@localhost playbook]#ansible-playbook demo3.yaml -k -K
-k 指定ssh的登錄密碼
-K 大寫需要輸入用戶的登錄密碼
?
實(shí)操4:通過when條件判斷指定主機(jī)完成相應(yīng)的任務(wù)
when一個(gè)比較常見的應(yīng)用場景是實(shí)現(xiàn)跳過某個(gè)主機(jī)不執(zhí)行任務(wù)或者只有滿足條件的主機(jī)執(zhí)行任務(wù)
vim test2.yaml
---
- hosts: all
remote_user: root
tasks:
- name: shutdown host
command: /sbin/shutdown -r now
when: ansible_default_ipv4.address == "192.168.80.12" #when指令中的變量名不需要手動(dòng)加上 {{}}
或
when: inventory_hostname == "<主機(jī)名>"
ansible-playbook test2.yaml
實(shí)操5:通過with_items循環(huán)實(shí)現(xiàn)在遠(yuǎn)程主機(jī)上關(guān)閉服務(wù)、創(chuàng)建文件和目錄
no1:關(guān)閉firewalld、nfs、crond
---
- name: stop firewalld nfs crond
gather_facts: true
hosts: webservers:dbservers
remote_user: root
#定義變量名為servername,值分別為firewalld,nfs,crond
vars:
- servername: [firewalld, nfs, crond]
tasks:
- name: stop firewalld nfs crond
#通過with_items實(shí)現(xiàn)循環(huán)關(guān)閉服務(wù)
with_items: "{{servername}}"
systemd: name={{item}} state=stopped enabled=no
?no2:批量創(chuàng)建多個(gè)用戶,并加入指定的組
---
- name: create user
gather_facts: false
hosts: dbservers
remote_user: root
vars:
myusers:
- {myname: xyc, mygroup: ky33}
- {myname: wwy, mygroup: ky34}
- {myname: wx, mygroup: ky35}
tasks:
- name: create groups
with_items: [ky33, ky34, ky35]
group: name={{item}}
- name: create users and join into group
with_items: "{{myusers}}"
user: name={{item.myname}} groups={{item.mygroup}} state=present
文章來源:http://www.zghlxwxcb.cn/news/detail-828487.html
實(shí)操6:基于templates模塊,完成nginx服務(wù)的配置模板
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.j2
vim /opt/httpd.conf.j2
Listen {{http_port}} #42行,修改
ServerName {{server_name}} #95行,修改
DocumentRoot "{{root_dir}}" #119行,修改
實(shí)操7:基于tags標(biāo)簽,指定完成特定的任務(wù)模塊
???????可以在一個(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都會(huì)執(zhí)行。文章來源地址http://www.zghlxwxcb.cn/news/detail-828487.html
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都會(huì)執(zhí)行。
vim webhosts.yaml
---
- hosts: webservers
remote_user: root
tasks:
- name: Copy hosts file
copy: src=/etc/hosts dest=/opt/hosts
tags:
- only #可自定義
- name: touch file
file: path=/opt/testhost state=touch
tags:
- always #表示始終要運(yùn)行的代碼
ansible-playbook webhosts.yaml --tags="only"
vim dbhosts.yaml
---
- hosts: dbservers
remote_user: root
tasks:
- name: Copy hosts file
copy: src=/etc/hosts dest=/opt/hosts
tags:
- only
- name: touch file
file: path=/opt/testhost state=touch
ansible-playbook dbhosts.yaml --tags="only"
//分別去兩臺(tái)被管理主機(jī)上去查看文件創(chuàng)建情況
合并:綜合多模塊
---
- name: serventh play for install nginx with source
gather_facts: yes
hosts: webservers
remote_user: root
vars:
- root_dir: /var/www/html
tasks:
#關(guān)閉防火墻和selinux
- name: disabled firewalld
service: name=firewalld state=stopped enabled=no
- name: disable selinux
command: '/sbin/setenforce 0'
ignore_errors: yes
- name: disabled selinux forever
replace: path=/etc/selinux/config regexp=enforcing replace=disabled after=loaded
#安裝依賴包
- name: mount cdrom
mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
- name: install pkgs
with_items: [pcre-devel, zlib-devel, openssl-devel, gcc, gcc-c++, make]
yum: name={{item}} state=latest
#創(chuàng)建運(yùn)行用戶
- name: create nginx user
user: name=nginx create_home=no shell=/sbin/nologin
#解壓軟件包并安裝
- name: unarchive nginx package
unarchive: copy=yes src=/etc/ansible/playbook/nginx-1.24.0.tar.gz dest=/opt/
- name: install nginx with source
shell: chdir=/opt/nginx-1.24.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
#創(chuàng)建軟鏈接并啟動(dòng)服務(wù)
- name: create link file for nginx
file: state=link src=/usr/local/nginx/sbin/nginx path=/usr/local/sbin/nginx
- name: create nginx service file
copy: src=nginx.service dest=/lib/systemd/system/nginx.service
- name: start nginx
service: name=nginx state=started enabled=yes
#創(chuàng)建網(wǎng)頁根目錄
- name: create root dir
file: path={{root_dir}} state=directory
#創(chuàng)建網(wǎng)頁文件
- name: create index.html for accp.com
copy: content="<h1>this is accp test web page</h1>" dest={{root_dir}}/index.html
when: ansible_default_ipv4.address == "192.168.20.17"
- name: create index.html for benet.com
copy: content="<h1>this is benet test web page</h1>" dest={{root_dir}}/index.html
when: ansible_default_ipv4.address == "192.168.20.12"
#生成配置文件
- name: copy nginx config file with template
template: src=/etc/ansible/playbook/nginx.conf.j2 dest=/usr/local/nginx/conf/nginx.conf
notify: "reload nginx"
handlers:
- name: reload nginx
service: name=nginx state=reloaded
三、關(guān)于playbook的知識(shí)點(diǎn)總結(jié)
inventory主機(jī)清單管理
主機(jī)清單配置文件 /etc/ansible/hosts
格式:
[組名]
主機(jī)IP[:ssh端口] #默認(rèn)是 22 端口
主機(jī)名[:ssh端口]
主機(jī)IP[0:255] #設(shè)置連續(xù)的主機(jī)列表范圍
主機(jī)名[a:z] ansible_port= ansible_user= ansible_password= #設(shè)置主機(jī)變量
[組名:vars] #設(shè)置組變量
ansible_port=
ansible_user=
ansible_password=
[大組名:children] #設(shè)置嵌套組
組名1
組名2
組名3
playbook劇本
vim XXX.yaml XXX.yml
---
- name: #設(shè)置play的名稱
hosts: #指定執(zhí)行此play的遠(yuǎn)程主機(jī)組
remote_user: #指定執(zhí)行此play的用戶
#become: #設(shè)置是否sudo切換
#become_user: #指定sudo切換的用戶
gather_facts: #設(shè)置是否收集facts信息 yes|no|true|false
vars: #設(shè)置自定義變量
tasks: #指定此play的任務(wù)列表
- name: #定義任務(wù)的名稱
模塊名: 模塊參數(shù) #定義任務(wù)要使用的模塊和參數(shù)(鍵值對格式)
- name:
模塊名: 模塊參數(shù)
ignore_errors: true #忽略此任務(wù)的失敗
- name:
模塊名: 模塊參數(shù)
notify: '任務(wù)名' #定義此任務(wù)執(zhí)行結(jié)果為changed狀態(tài)時(shí)要觸發(fā)的handlers任務(wù)
- name:
模塊名: 模塊參數(shù)
when: #定義條件表達(dá)式(== != > >= < <=),條件成立時(shí)執(zhí)行此task任務(wù),否則不執(zhí)行此任務(wù)
- name:
模塊名: 模塊參數(shù)={{item}}
with_items: #定義循環(huán)列表
- name:
模塊名: 模塊參數(shù)
tags: #定義任務(wù)的標(biāo)簽,ansible-playbook --tags="標(biāo)簽" 可實(shí)現(xiàn)僅執(zhí)行擁有指定標(biāo)簽的任務(wù)(擁有always標(biāo)簽的任務(wù)總是執(zhí)行)
- 標(biāo)簽1
- 標(biāo)簽2
....
handlers:
- name: 任務(wù)名
模塊名: 模塊參數(shù)
ansible-playbook XXX.yaml -u -k -K
--syntax-check
--list-task
--list-hosts
--start-at-task="任務(wù)名稱"
--tags="標(biāo)簽"
tasks任務(wù)的模塊語法格式:
縱向格式:
模塊名:
參數(shù)1: 值
參數(shù)2: "{{變量名}}"
....
橫向格式:
模塊名: 參數(shù)1=值 參數(shù)2={{變量名}} ....
with_items 和 vars 的語法格式
縱向格式:
- name:
with_items:
- 值1
- 值2
....
模塊名: 參數(shù)={{item}}
vars:
變量名:
- 值1
- 值2
....
tasks:
- name:
模塊名: 參數(shù)={{item}}
with_items: "{{變量名}}"
#值為對象類型(鍵值對字段)時(shí):
with_items:
- key1: 值1
key2: 值2
- key1: 值3
key2: 值4
....
vars:
變量名:
- key1: 值1
key2: 值2
- key1: 值3
key2: 值4
....
橫向格式:
#值為純量類型時(shí):
with_items: [值1, 值2, ....]
vars:
變量名: [值1, 值2, ....]
#值為對象類型(鍵值對字段)時(shí):
with_items:
- {key1: 值1, key2: 值2}
- {key1: 值3, key2: 值4}
....
template配置模板模塊
1)先準(zhǔn)備一個(gè) XXX.j2 配置模板文件,在模板文件中使用 {{變量名}} 格式引用主機(jī)變量、組變量、facts信息字段變量或vars字段自定義的變量的值
2)編輯 playbook 文件,在文件中的 tasks 任務(wù)中定義 template 模塊配置(template: src=XXX.j2文件路徑 dest=遠(yuǎn)程主機(jī)文件路徑)
roles角色的作用?
可以把playbook劇本里的各個(gè)play看作為一個(gè)角色,將各個(gè)角色的tasks任務(wù)、vars變量、template模板和copy、script模塊使用的相關(guān)文件等內(nèi)容放置在指定角色的目錄里統(tǒng)一管理,在需要的時(shí)候可在playbook中使用roles角色直接調(diào)用即可。也就是說roles角色可以在playbook中實(shí)現(xiàn)代碼的復(fù)用。
roles/ #角色總目錄,其每個(gè)子目錄就是一個(gè)角色目錄
nginx/ #相當(dāng)于playbook中的每一個(gè)play主題,目錄名就是角色名
files/ #存放copy、script模塊調(diào)用的文件
templates/ #存放template模塊調(diào)用的 XXX.j2 模板文件
tasks/main.yml #定義此角色的tasks普通任務(wù)列表
handlers/main.yml #定義此角色通過notify觸發(fā)時(shí)執(zhí)行的handlers處理器任務(wù)列表
vars/main.yml #定義此角色用的自定義變量
defaults/main.yml #定義此角色用的默認(rèn)變量(一般不用)
meta/main.yml #定義此角色的元數(shù)據(jù)信息和依賴關(guān)系
mysql/
....
php/
....
vim XXX.yaml
- name:
hosts:
remote_user:
roles:
- nginx
- mysql
- php
ansible-playbook XXX.yaml
到了這里,關(guān)于【ansible】自動(dòng)化運(yùn)維ansible之playbook劇本編寫與運(yùn)行的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!