playbook
一、什麼是PlayBook
PlayBook即"劇本","兵書"之意
# PlayBook的組成
play: 定義的是主機的角色(主角還是配角)
task: 定義的是具體執行的任務(角色的台詞和動作)
playbook: 由一個或多個play(角色)組成,一個play(角色)可以包含多個task(台詞,動作)。
#簡單了解為: 使用不同的子產品完成一件事情
在
Ansible
中"劇本檔案"是以yml結尾的檔案。
在
SaltStack
中"劇本檔案"是以sls結尾的檔案。
但是文法,使用的都是
文法
yaml
二、PlayBook與ad-hoc
特點 | PlayBook | ad-hoc |
---|---|---|
完整性 | √ | ✘ |
持久性 | √ | ✘ |
執行效率 | 低 | 高 |
變量 | 支援 | 不支援 |
耦合度 | 低 | 高 |
1.PlayBook功能比ad-hoc更全,是對ad-hoc的一種編排.
2.PlayBook能很好的控制先後執行順序, 以及依賴關系.
3.PlayBook文法展現更加的直覺.
4.playbook可以持久使用,ad-hoc無法持久使用.
三、YAML文法
文法 | 描述 |
---|---|
縮進 | YAML使用固定的縮進風格表示層級結構,每個縮進由兩個空格組成, 不能使用TAB |
冒号 | 以冒号結尾的除外,其他所有冒号後面所有必須有空格 |
短橫線 | 表示清單項,使用一個短橫杠加一個空格,多個項使用同樣的縮進級别作為同一清單 |
yum:
name: vsftpd
state: present
yum:
name:
- httpd
- nginx
- php-fpm
state: present
四、PlayBook—《孫子兵法》編寫
host:對哪些主機進行操作(演員)
remote_user:使用什麼使用者執行(通行證)
tasks:具體執行任務(台詞和動作)
[[email protected] ~]# cat foo.yml
---
- hosts: all
remote_user: root
vars:
file_name: zls.txt
tasks:
- name: Create New File
file: name=/tmp/{{ file_name }} state=touch
五、PlayBook部署httpd
#編寫httpd劇
#建立目錄劇本存放目錄
[[email protected] ~]# mkdir httpd
#編輯Inventory
[[email protected] ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
#需求一:編寫安裝httpd劇本
[[email protected] ~]# vim /root/httpd/httpd.yml
---
- hosts: web_group
tasks:
- name: Install httpd Server
yum:
name: httpd
state: present
#檢查文法
[[email protected] ~]# ansible-playbook --syntax-check httpd/httpd.yml
playbook: httpd/httpd.yml
#測試安裝
[[email protected] ~]# ansible-playbook -C httpd/httpd.yml
PLAY [web_group] ***************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]
TASK [Install httpd Server] ****************************************************************************************************************************************************************************************************************************************************
changed: [web02]
changed: [web01]
PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
web01 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#需求二:安裝完httpd服務并啟動加入開機自啟
[[email protected] ~]# vim /root/httpd/httpd.yml
---
- hosts: web_group
#安裝httpd
tasks:
- name: Install httpd Server
yum:
name: httpd
state: present
#啟動httpd
- name: Start Httpd Server
systemd:
name: httpd
state: started
enabled: yes
#測試安裝和啟動
[[email protected] ~]# ansible-playbook -C httpd/httpd.yml
PLAY [web_group] ***************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]
TASK [Install httpd Server] ****************************************************************************************************************************************************************************************************************************************************
changed: [web02]
changed: [web01]
TASK [Start Httpd Server] ******************************************************************************************************************************************************************************************************************************************************
changed: [web02]
changed: [web01]
PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
web01 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#需求三:編寫網站頁面并啟動
---
- hosts: web_group
#安裝httpd
tasks:
- name: Install httpd Server
yum:
name: httpd
state: present
#配置網站
- name: Config Httpd Server
copy:
content: zls_web_page
dest: /var/www/html/index.html
#啟動httpd
- name: Start Httpd Server
systemd:
name: httpd
state: started
enabled: yes
#執行
[[email protected] httpd]# ansible-playbook /root/httpd/httpd.yml
#需求四:開啟防火牆端口
---
- hosts: web_group
#安裝httpd
tasks:
- name: Install httpd Server
yum:
name: httpd
state: present
#配置網站
- name: Config Httpd Server
copy:
content: zls_web_page
dest: /var/www/html/index.html
#啟動httpd
- name: Start Httpd Server
systemd:
name: httpd
state: started
enabled: yes
#啟動防火牆
- name: Start Firewalld Server
systemd:
name: firewalld
state: started
enabled: yes
#開啟防火牆的80端口
- name: Config Firewalld Server
firewalld:
service: http
immediate: yes
permanent: yes
state: enabled
#浏覽器測試通路網站:
http://10.0.0.7
http://10.0.0.8
目前來說,想要根據不同主機配置不同的網站,我們可以使用多個play的方式,但是在生産環境中,我們需要寫循環,來滿足我們的需求,多個play了解即可
#需求五:不同的主機配置不同的網站
---
- hosts: web_group
#安裝httpd
tasks:
- name: Install httpd Server
yum:
name: httpd
state: present
#啟動httpd
- name: Start Httpd Server
systemd:
name: httpd
state: started
enabled: yes
#啟動防火牆
- name: Start Firewalld Server
systemd:
name: firewalld
state: started
enabled: yes
#開啟防火牆的80端口
- name: Config Firewalld Server
firewalld:
service: http
immediate: yes
permanent: yes
state: enabled
#單獨配置web01頁面
- hosts: web01
tasks:
- name: Config Httpd Server
copy:
content: zls_web01_page
dest: /var/www/html/index.html
#單獨配置web02頁面
- hosts: web02
tasks:
- name: Config Httpd Server
copy:
content: zls_web02_page
dest: /var/www/html/index.html
[[email protected] httpd]# ansible-playbook /root/httpd/httpd.yml
六、PlayBook實戰
實戰一:《孫子兵法-九變篇》之"未雨綢缪,嚴陣以待"
*1.演員表*
主機名 | wanIP | lanIP | 服務 | 角色 |
---|---|---|---|---|
m01 | 10.0.0.61 | 172.16.1.61 | Ansible | 控制端(導演) |
backup | 10.0.0.41 | 172.16.1.41 | rsync服務端 | 被控端(男一) |
web01 | 10.0.0.7 | 172.16.1.7 | rsync用戶端 | 被控端(女二) |
web02 | 10.0.0.8 | 172.16.1.8 | rsync用戶端 | 被控端(女二) |
*2.戰前準備*
燈光,音響,攝像準備…
yum源,使用者,配置檔案…
#建立rsync劇本存放目錄
[[email protected] ~]# mkdir rsyncd
#編輯Inventory
[[email protected] ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
[backup_group]
backup ansible_ssh_host=10.0.0.41
#準備rsync配置檔案
[[email protected] rsyncd]# vim /root/rsyncd/rsyncd.j2
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup
*3.編寫劇本*
#編寫劇本
[[email protected] ~]# vim /root/rsyncd/rsyncd.yml
- hosts: all
tasks:
#安裝rsync
- name: Install Rsyncd Server
yum:
name: rsync
state: present
#建立www組
- name: Create www Group
group:
name: www
gid: 666
#建立www使用者
- name: Create www User
user:
name: www
group: www
uid: 666
create_home: false
shell: /sbin/nologin
- hosts: backup_group
tasks:
#推送rsync配置檔案
- name: Scp Rsync Config
copy:
src: ./rsyncd.j2
dest: /etc/rsyncd.conf
owner: root
group: root
mode: 0644
#建立密碼檔案并授權
- name: Create Passwd File
copy:
content: 'rsync_backup:123'
dest: /etc/rsync.passwd
owner: root
group: root
mode: 0600
#建立/backup目錄
- name: Create backup Directory
file:
path: /backup
state: directory
mode: 0755
owner: www
group: www
recurse: yes
#啟動rsync服務
- name: Start Rsyncd Server
systemd:
name: rsyncd
state: started
#檢測文法
[[email protected] ~]# ansible-playbook --syntax-check /root/rsyncd/rsyncd.yml
playbook: /root/rsyncd/rsyncd.yml
#測試
[[email protected] ~]# ansible-playbook -C /root/rsyncd/rsyncd.yml
PLAY [all] ***********************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [backup]
ok: [web02]
ok: [web01]
TASK [Install Rsyncd Server] *****************************************************************************************************************************************************************************************************************
changed: [backup]
changed: [web02]
changed: [web01]
TASK [Scp Rsync Config] **********************************************************************************************************************************************************************************************************************
changed: [web02]
changed: [web01]
changed: [backup]
TASK [Create www Group] **********************************************************************************************************************************************************************************************************************
changed: [backup]
changed: [web01]
changed: [web02]
TASK [Create www User] ***********************************************************************************************************************************************************************************************************************
changed: [web02]
changed: [web01]
changed: [backup]
TASK [Create backup Directory] ***************************************************************************************************************************************************************************************************************
changed: [web01]
changed: [backup]
changed: [web02]
TASK [Start Rsyncd Server] *******************************************************************************************************************************************************************************************************************
changed: [web01]
changed: [web02]
changed: [backup]
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
backup : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web01 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
擴充需求:
1.給用戶端推送腳本
2.加入crontab做備份
實戰二:《孫子兵法-虛實篇》之"空城計"
部署服務,
NFS
服務端,敞開大門提供挂載點給
NFS
和
web01
web02

1.演員表
主機名 | wanIP | lanIP | 服務 | 角色 |
---|---|---|---|---|
m01 | 10.0.0.61 | 172.16.1.61 | Ansible | 控制端(導演) |
nfs | 10.0.0.31 | 172.16.1.31 | nfs服務端 | 被控端(男一) |
web01 | 10.0.0.7 | 172.16.1.7 | nfs用戶端 | 被控端(女二) |
web02 | 10.0.0.8 | 172.16.1.8 | nfs用戶端 | 被控端(女二) |
*2.戰前準備*
#編輯Ansible Inventory
[[email protected] ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
[backup_group]
backup ansible_ssh_host=10.0.0.41
[nfs_group]
nfs ansible_ssh_host=10.0.0.31
[nfs_all:children]
web_group
nfs_group
#建立項目存放目錄
[[email protected] ~]# mkdir nfs
#準備nfs配置檔案
[[email protected] ~]# cat /root/nfs/nfs.j2
/data 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
*3.編寫劇本*
[[email protected] ~]# vim /root/nfs/nfs.yml
- hosts: nfs_all
tasks:
#安裝nfs
- name: Install nfs-utils
yum:
name: nfs-utils
state: present
#建立www組
- name: Create www Group
group:
name: www
gid: 666
#建立www使用者
- name: Create www User
user:
name: www
group: www
uid: 666
create_home: false
shell: /sbin/nologin
- hosts: nfs
tasks:
#推送配置檔案
- name: Scp NFS Server
copy:
src: ./nfs.j2
dest: /etc/exports
owner: root
group: root
mode: 0644
#建立挂載目錄并授權
- name: Create data Directory
file:
path: /data
state: directory
owner: www
group: www
mode: 0755
recurse: yes
#啟動nfs-server
- name: Start NFS Server
systemd:
name: nfs-server
state: started
enabled: yes
#web01和web02挂載目錄
- hosts: web_group
tasks:
- name: Mount NFS Server
mount:
path: /opt
src: 10.0.0.31:/data
fstype: nfs
opts: defaults
state: mounted
#檢查文法
[[email protected] ~]# ansible-playbook --syntax-check /root/nfs/nfs.yml
playbook: /root/nfs/nfs.yml
#執行
[[email protected] ~]# ansible-playbook /root/nfs/nfs.yml
4、檢查
實戰三:《孫子兵法-始計篇》之"兵者,詭道也"
使用實作一套
playbook
架構
LAMP
部署需求:
1.使用yum安裝httpd、php、php-mysql、php-pdo、mariadb
2.啟動httpd、mariadb服務
3.下載下傳wordpress代碼
4.部署到httpd站點目錄
*1.演員表*
主機名 | wanIP | lanIP | 服務 | 角色 |
---|---|---|---|---|
m01 | 10.0.0.61 | 172.16.1.61 | Ansible | 控制端(導演) |
web01 | 10.0.0.7 | 172.16.1.7 | nfs用戶端 | 被控端(男一) |
web02 | 10.0.0.8 | 172.16.1.8 | nfs用戶端 | 被控端(男一) |
*2.戰前準備*
因為這隻是一個練習,是以将apache mariadb php全部都寫在一個yml檔案中,并且放在一個目錄下
注意:在生産中,我們需要每一個服務單獨拎出來,解耦。
#建立項目目錄
[[email protected] ~]# cd lamp/
#編輯Inventory
[[email protected] lamp]# cat /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
[backup_group]
backup ansible_ssh_host=10.0.0.41
[backup_all:children]
web_group
backup_group
[nfs_group]
nfs ansible_ssh_host=10.0.0.31
[nfs_all:children]
web_group
nfs_group
*3.編寫劇本*
[[email protected] ~]# vim /root/lamp/lamp.yml
- hosts: web_group
tasks:
#安裝指定服務
- name: Install httpd mariadb php Server
yum:
name: "{{ packages }}"
vars:
packages:
- httpd
- mariadb-server
- php
- php-mysql
- php-pdo
#啟動httpd服務
- name: Start httpd Server
systemd:
name: httpd
state: started
enabled: yes
#啟動mariadb服務
- name: Start httpd Server
systemd:
name: mariadb
state: started
enabled: yes
#下載下傳wordpress
- name: Get Wordpress Package
get_url:
url: "http://test.driverzeng.com/Nginx_Code/wordpress-5.0.3-zh_CN.tar.gz"
dest: /var/www/html
#解壓wordpress
- name: Unarchive Wordpress Package
unarchive:
src: /var/www/html/wordpress-5.0.3-zh_CN.tar.gz
dest: /var/www/html
copy: no
#檢查文法
[[email protected] lamp]# ansible-playbook --syntax-check /root/lamp/lamp.yml
playbook: /root/lamp/lamp.yml
#執行
[[email protected] lamp]# ansible-playbook /root/lamp/lamp.yml
4、檢查通路
#浏覽器通路:
http://10.0.0.7/wordpress/wp-admin/setup-config.php
http://10.0.0.8/wordpress/wp-admin/setup-config.php