這篇文章給大家介紹下PlayBook,我們叫它劇本,它是以一種固定的格式,將多個(gè)ad-hoc放入yml文件中。在Ansible中,劇本文件是yml結(jié)尾的,在SaltStack中劇本文件是sls結(jié)尾的,但是兩者語法都是使用的yaml語法。
PlayBook與ad-hoc區(qū)別
1、PlayBook功能比ad-hoc全,是對(duì)ad-hoc的一種編排
2、PlayBook能很好的控制先后執(zhí)行順序,以及依賴關(guān)系
3、PlayBook語法展現(xiàn)更加直觀
4、PlayBook可以持久使用,ad-hoc無法持久使用
YAML語法
語法 | 描述 |
縮進(jìn) | YAML使用固定的縮進(jìn)風(fēng)格表示層級(jí)結(jié)構(gòu),每個(gè)縮進(jìn)由兩個(gè)空格組成,不能使用TAB |
冒號(hào) | 以冒號(hào)結(jié)尾的除外,其他所有冒號(hào)后面所有空格 |
短橫線 | 表示列表項(xiàng),使用一個(gè)短橫線加一個(gè)空格,多個(gè)項(xiàng)使用同樣的縮進(jìn)級(jí)別作為同一列表 |
PlayBook部署實(shí)戰(zhàn)
1、部署httpd
1、安裝httpd服務(wù)
2、啟動(dòng)httpd服務(wù)并加入開機(jī)自啟動(dòng)
3、編寫網(wǎng)站頁面并啟動(dòng)
4、開啟防火墻端口
5、不同的主機(jī)配置不同的網(wǎng)站
#創(chuàng)建劇本存放目錄
[root@Ansible ~]# mkdir -p ansible/httpd
#編輯主機(jī)列表
[root@Ansible ~]# cat /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
#編寫劇本
[root@Ansible ~]# cat ansible/httpd/httpd.yml
- hosts: web_group
tasks:
- name: Install httpd Server #安裝httpd
yum:
name: httpd
state: present
- name: Start httpd Server #開啟httpd服務(wù)
systemd:
name: httpd
state: started
enabled: yes
- name: Start Firewalld Server #開啟防火墻
systemd:
name: firewalld
state: started
enabled: yes
- name: Config Firewalld Server #配置防火墻服務(wù)
firewalld:
service: http
immediate: yes
permanent: yes
state: enabled
- hosts: web01
tasks:
- name: Config Httpd Server #增加Web01頁面
copy:
content: Web01
dest: /var/www/html/index.html
- hosts: web02
tasks:
- name: Config Httpd Server #增加Web02頁面
copy:
content: Web02
dest: /var/www/html/index.html
[root@Ansible ~]# ansible-playbook --syntax-check ansible/httpd/httpd.yml #檢查語法
playbook: ansible/httpd/httpd.yml
[root@Ansible ~]# ansible-playbook ansible/httpd/httpd.yml #執(zhí)行劇本
#瀏覽器訪問10.0.0.7和10.0.0.8即可
?
?2、Backup備份服務(wù)器和客戶端的部署
#創(chuàng)建rsync劇本存放目錄
[root@Ansible ~]# mkdir ansible/rsyncd
#編輯主機(jī)列表
[root@Ansible ~]# 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
#準(zhǔn)備rsync配置文件
[root@Ansible ~]# cat ansible/rsyncd/rsyncd.conf #最好是與劇本放到同一目錄
uid = rsync
gid = rsync
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]
path = /backup
#編寫劇本
[root@Ansible ~]# cat ansible/rsyncd/rsyncd.yml
- hosts: all
tasks:
- name: Install Rsyncd Server
yum:
name: rsync
state: present
- name: Create www Group
group:
name: www
gid: 666
- name: Create www User
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: false
- hosts: backup_group
tasks:
- name: Scp Rsync Config
copy:
src: /root/ansible/rsyncd/rsyncd.conf
dest: /etc/rsyncd.conf
owner: root
group: root
mode: 0644
- name: Create backup Directory
file:
path: /backup
state: directory
mode: 0755
owner: www
group: www
recurse: yes
- name: Start Rsyncd Server
systemd:
name: rsyncd
state: started
#檢查劇本
[root@Ansible ~]# ansible-playbook --syntax-check ansible/rsyncd/rsyncd.yml
playbook: ansible/rsyncd/rsyncd.yml
#運(yùn)行劇本
[root@Ansible ~]# ansible-playbook ansible/rsyncd/rsyncd.yml
完成后還可以嘗試給客戶端推送數(shù)據(jù),加入crontab做備份等等操作。?
3、NFS服務(wù)部署
#1、添加目標(biāo)服務(wù)器到主機(jī)列表并做ssh免密鑰
[root@Ansible ~]# cat /etc/ansible/hosts
[nfs_group]
nfs ansible_ssh_host=10.0.0.31
[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_all:children]
nfs_group
web_group
[root@Ansible ~]# ssh-copy-id -i .ssh/id_rsa.pub root@10.0.0.31
#2、創(chuàng)建nfs的目錄
[root@Ansible ~]# mkdir ansible/nfs/
#3、準(zhǔn)備nfs配置文件添加到管理機(jī)中
[root@Ansible ~]# cat ansible/nfs/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
#4、編寫nfs劇本
[root@Ansible ~]# cat ansible/nfs/nfs.yml
- hosts: nfs_all
tasks:
- name: Install nfs-utils
yum:
name: nfs-utils
state: present
- name: Create www Group
group:
name: www
gid: 666
- name: Create www user
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: false
- hosts: nfs_group
tasks:
- name: Scp NFS server exports
copy:
src: exports
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
- name: Start NFS server
systemd:
name: nfs-server
state: started
enabled: yes
- hosts: web_group
tasks:
- name: Mount NFS Server
mount:
path: /opt
src: 10.0.0.31:/data
fstype: nfs
opts: defaults
state: mounted
#5、檢查語法
[root@Ansible ~]# ansible-playbook --syntax-check /root/ansible/nfs/nfs.yml
playbook: /root/ansible/nfs/nfs.yml
#6、執(zhí)行劇本
[root@Ansible ~]# ansible-playbook ansible/nfs/nfs.yml
#7、查看web01、web02掛載情況
[root@Web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
10.0.0.31:/data 19G 2.0G 17G 11% /opt
[root@Web02 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
10.0.0.31:/data 19G 2.0G 17G 11% /opt
4、Nginx服務(wù)部署
#1、添加目標(biāo)服務(wù)器至主機(jī)列表并做免密鑰
[root@Ansible ~]# cat /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
[root@Ansible ~]# ssh-copy-id -i .ssh/id_rsa.pub root@10.0.0.7
[root@Ansible ~]# ssh-copy-id -i .ssh/id_rsa.pub root@10.0.0.8
#2、創(chuàng)建劇本存放目錄
[root@Ansible ~]# mkdir ansible/nginx
#3、準(zhǔn)備nginx配置文件與代碼文件
[root@Web01 ~]# scp /etc/nginx/nginx.conf /etc/nginx/conf.d/* 10.0.0.61:/root/ansible/nginx
[root@Web01 ~]# tar zcvf code.tar.gz /code
[root@Web01 ~]# scp code.tar.gz 10.0.0.61:/root/ansible/nginx
#4、寫劇本
[root@Ansible ~]# cat ansible/nginx/nginx.yml
- hosts: web_group
tasks:
- name: nginx.repo
copy:
src: nginx.repo
dest: /etc/yum.repos.d/nginx.repo
- name: install nginx
yum:
name: nginx
state: present
- name: start and enable nginx
systemd:
name: nginx
state: started
enabled: yes
- name: copy nginx.conf to nginx
copy:
src: nginx.conf
dest: /etc/nginx
- name: copy 'wecenter.conf' to nginx
copy:
src: wecenter.conf
dest: /etc/nginx/conf.d/wecenter.conf
- name: copy 'wordpress.conf' to nginx
copy:
src: wordpress.conf
dest: /etc/nginx/conf.d/wordpress.conf
- name: remove nginx defualt.conf
file:
path: /etc/nginx/conf.d/defualt.conf
state: absent
- name: tar xf code.tar.gz
unarchive:
src: code.tar.gz
dest: /
creates: /code
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
#5、檢查劇本語法
[root@Ansible ~]# ansible-playbook --syntax-check ansible/nginx/nginx.yml
playbook: ansible/nginx/nginx.yml
#6、執(zhí)行劇本
[root@Ansible ~]# ansible-playbook ansible/nginx/nginx.yml
5、PHP服務(wù)部署
#1、將目標(biāo)主機(jī)添加至主機(jī)列表
[root@Ansible ~]# cat /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
#2、創(chuàng)建劇本存放目錄
[root@Ansible ~]# mkdir ansible/php
#3、準(zhǔn)備必要文件:php71.tar.gz、php.ini、www.conf
[root@Ansible ~]# cd ansible/php/
[root@Ansible php]# rz -E
rz waiting to receive.
[root@Web01 ~]# scp /etc/php.ini /etc/php-fpm.d/www.conf 10.0.0.61:/root/ansible/php
root@10.0.0.61's password:
php.ini 100% 61KB 16.5MB/s 00:00
www.conf 100% 18KB 2.4MB/s 00:00
#4、寫劇本
[root@Ansible php]# cat php.yml
- hosts: web_group
tasks:
- name: tar xf php to web_group
unarchive:
src: php71.tar.gz
dest: /root
- name: localinstall rpm
yum:
name:
- /root/autoconf-2.69-11.el7.noarch.rpm
- /root/automake-1.13.4-3.el7.noarch.rpm
- /root/libevent-2.0.21-4.el7.x86_64.rpm
- /root/libjpeg-turbo-1.2.90-8.el7.x86_64.rpm
- /root/libmcrypt-2.5.8-13.el7.x86_64.rpm
- /root/libmemcached-1.0.16-5.el7.x86_64.rpm
- /root/libtool-ltdl-2.4.2-22.el7_3.x86_64.rpm
- /root/libX11-1.6.7-3.el7_9.x86_64.rpm
- /root/libX11-common-1.6.7-3.el7_9.noarch.rpm
- /root/libXau-1.0.8-2.1.el7.x86_64.rpm
- /root/libxcb-1.13-1.el7.x86_64.rpm
- /root/libXpm-3.5.12-1.el7.x86_64.rpm
- /root/libxslt-1.1.28-6.el7.x86_64.rpm
- /root/mod_php71w-7.1.33-1.w7.x86_64.rpm
- /root/pcre-devel-8.32-17.el7.x86_64.rpm
- /root/perl-Data-Dumper-2.145-3.el7.x86_64.rpm
- /root/perl-Test-Harness-3.28-3.el7.noarch.rpm
- /root/perl-Thread-Queue-3.02-2.el7.noarch.rpm
- /root/php71w-cli-7.1.33-1.w7.x86_64.rpm
- /root/php71w-common-7.1.33-1.w7.x86_64.rpm
- /root/php71w-devel-7.1.33-1.w7.x86_64.rpm
- /root/php71w-embedded-7.1.33-1.w7.x86_64.rpm
- /root/php71w-fpm-7.1.33-1.w7.x86_64.rpm
- /root/php71w-gd-7.1.33-1.w7.x86_64.rpm
- /root/php71w-mbstring-7.1.33-1.w7.x86_64.rpm
- /root/php71w-mcrypt-7.1.33-1.w7.x86_64.rpm
- /root/php71w-mysqlnd-7.1.33-1.w7.x86_64.rpm
- /root/php71w-opcache-7.1.33-1.w7.x86_64.rpm
- /root/php71w-pdo-7.1.33-1.w7.x86_64.rpm
- /root/php71w-pear-1.10.4-1.w7.noarch.rpm
- /root/php71w-pecl-igbinary-2.0.5-1.w7.x86_64.rpm
- /root/php71w-pecl-memcached-3.0.4-1.w7.x86_64.rpm
- /root/php71w-pecl-mongodb-1.5.3-1.w7.x86_64.rpm
- /root/php71w-pecl-redis-3.1.6-1.w7.x86_64.rpm
- /root/php71w-process-7.1.33-1.w7.x86_64.rpm
- /root/php71w-xml-7.1.33-1.w7.x86_64.rpm
state: present
- name: create group
group:
name: www
gid: 666
- name: create user
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: false
- name: copy php.ini to web_group
copy:
src: php.ini
dest: /etc/php.ini
- name: copy www.conf to web_group
copy:
src: www.conf
dest: /etc/php-fpm.d/www.conf
- name: start and enable php
systemd:
name: php-fpm
state: started
enabled: yes
#5、劇本語法檢查
[root@Ansible php]# ansible-playbook --syntax-check php.yml
playbook: php.yml
#6、執(zhí)行劇本
[root@Ansible php]# ansible-playbook php.yml
6、Mariadb服務(wù)部署
#1、添加服務(wù)器到我們的主機(jī)列表并做免密鑰
[root@Ansible ~]# cat /etc/ansible/hosts
[mysql_group]
mysql ansible_ssh_host=10.0.0.51
[root@Ansible ~]# ssh-copy-id -i .ssh/id_rsa.pub root@10.0.0.51
#2、創(chuàng)建劇本目錄
[root@Ansible ~]# mkdir ansible/mysql
#3、準(zhǔn)備好數(shù)據(jù)庫
[root@MySQL ~]# mysqldump -uroot -pkoten.vip -A > all.sql
[root@MySQL ~]# scp all.sql 10.0.0.61:/root/ansible/mysql
#4、寫劇本
[root@Ansible ~]# cat ansible/mysql/mysql.yml
- hosts: mysql_group
tasks:
- name: Install mariadb
yum:
name:
- mariadb-server
- MySQL-python
state: present
- name: Start httpd Server
systemd:
name: mariadb
state: started
enabled: yes
- name: Copy all.sql to Mysql
copy:
src: all.sql
dest: /root/all.sql
- name: import all.sql
mysql_db:
login_host: localhost
login_port: 3306
login_user: root
name: all
state: import
target: /root/all.sql
- name: Restart MariaDB Server
systemd:
name: mariadb
state: restarted
#5、檢查
[root@Ansible ~]# ansible-playbook --syntax-check ansible/mysql/mysql.yml
playbook: ansible/mysql/mysql.yml
#6、執(zhí)行劇本
[root@Ansible ~]# ansible-playbook ansible/mysql/mysql.yml
?我是koten,10年運(yùn)維經(jīng)驗(yàn),持續(xù)分享運(yùn)維干貨,感謝大家的閱讀和關(guān)注!文章來源:http://www.zghlxwxcb.cn/news/detail-463058.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-463058.html
到了這里,關(guān)于【運(yùn)維知識(shí)進(jìn)階篇】Ansible自動(dòng)化運(yùn)維-PlayBook詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!