Playbook 是一个不同于使用 Ansible 命令行执行方式的模式,其功能更强大灵活。Playbook 可定制配置,可按照指定的操作步骤有序执行,支持同步、异步方式。值得注意的是 Playbook 通过 YAML 格式来进行描述定义的。
案例拓扑
实战案例拓扑如下:
环境规划
网络环境规划如下:
角色 | NAT 外网 IP | NAT 外网 IP | 部署软件 |
---|---|---|---|
m01 | eth0:10.0.0.61 | eth1:172.16.1.61 | Ansible |
backup | eth0:10.0.0.41 | eth1:172.16.1.41 | rsync |
nfs | eth0:10.0.0.31 | eth1:172.16.1.31 | NFS、sersync |
web01 | eth0:10.0.0.7 | eth1:172.16.1.7 | httpd |
配置 Ansible 对应的主机
[root@m01 ~]# vim /etc/ansible/hosts
[web]
172.16.1.7
[nfs]
172.16.1.31
[backup]
172.16.1.41
注意:上面为 Ansible 主机清单列表。
检查对应主机组和规划的 IP 是否一致
[root@m01 ~]# ansible web --list-host
hosts (1):
172.16.1.7
[root@m01 ~]# ansible backup --list-host
hosts (1):
172.16.1.41
[root@m01 ~]# ansible nfs --list-host
hosts (1):
172.16.1.31
[root@m01 ~]# ansible all --list-host
hosts (3):
172.16.1.31
172.16.1.41
172.16.1.7
注意:上面命令用于检测主机清单列表是否生效。
建立对应目录站点
[root@m01 ~]# mkdir -p /etc/ansible/ansible_playbook/file
注意:上面命令用于建立存放 ansible-playbook 文件的目录。
编写基础模块的 Playbook
实现如下功能:
- 基础仓库准备;
- 安装 rsync 服务端;
- 安装 nfs-utils 服务端;
- 创建 www 用户指定 UID/GID;
- 准备 rsync 客户端密码文件。
建立基础环境的 YAML:
[root@m01 ansible_playbook]# cat base.yml
- hosts: all
remote_user: root
tasks:
- name: configure yum repos
yum_repository:
name: base
description: base yum repo
baseurl:
- http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
- http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
- http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck: no
- name: configure yum repos
yum_repository:
name: epel
description: epel yum repo
baseurl: http://mirrors.aliyun.com/epel/7/$basearch
gpgcheck: no
- name: Create www Group
group: name=www gid=666
- name: Create www User
user: name=www uid=666 group=666 shell=/sbin/nologin create_home=no
- name: create rsync client pass
copy: content='123456' dest=/etc/rsync.pass mode=0600
- name: Push backup scripts
copy: src=./files/clinet_push_rsync.sh dest=/server/scripts/
when: (ansible_hostname != "backup")
- name: Cron Tasks
cron: name=Rsync_Backup minute=00 hour=01 job='/bin/bash /server/scripts/clinet_push_rsync.sh &>/dev/null'
when: (ansible_hostname != "backup")
使用 ansible-playbook 检测语法并进行模拟执行:
[root@m01 ansible_playbook]# ansible-playbook --syntax-check base.yaml
playbook: base.yaml
[root@m01 ansible_playbook]# ansible-playbook -C base.yaml
编写应用模块 rsync 的剧本:
- 安装 rsync 服务端;
- 配置 rsync 服务端;
- 启动 rsync 服务端;
- 准备对应的数据存储仓库/backup、/data 并授权为 www;
- 准备虚拟用户和密码文件权限 600;
- 变更配置,重载服务。
准备对应配置文件存放至/etc/ansible/ansible_playbook/files/:
[root@m01 conf]# cat /etc/ansible/ansible_playbook/files/rsyncd.conf
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]
path = /backup
[data]
path = /data
编写 rsync 服务端安装的 YAML 语法:
[root@m01 ansible_playbook]# cat rsync.yml
- hosts: backup
remote_user: root
tasks:
- name: Install Rsync Server
yum: name=rsync state=present
- name: Config Rsync Server
copy: src=./files/{{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }}
with_items:
- { src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644" }
- { src: "rsync.passwd", dest: "rsync.passwd", mode: "0600" }
notify:
- Restart Rsync Server
tags: conf_rsync
- name: Create Directory
file: name={{ item }} state=directory owner=www group=www recurse=yes
with_items:
- /data
- /backup
- name: Server Rsync Server
service: name=rsyncd state=started enabled=yes
- name: Check Rsync Status
shell: netstat -lntp|grep rsync
register: Rsync_Status
- name: Out Rsync Status
debug: msg={{ Rsync_Status.stdout_lines }}
handlers:
- name: Restart Rsync Server
service: name=rsyncd state=restarted
编写应用模块 NFS 的剧本:
- 安装 NFS 服务端;
- 配置 NFS 服务端;
- 启动 NFS 服务端;
- 准备对应数据存储仓库/data 并授权为 www;
- 变更配置,重载服务。
准备 NFS 配置文件 exports:
[root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/files/exports
{{ share_dir }} {{ share_ip }}(rw,sync,all_squash,anonuid=666,anongid=666)
编写 NFS 安装与配置的 YAML:
[root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/nfs.yml
- hosts: nfs
remote_user: root
vars:
share_dir: /data
share_ip: 172.16.1.0/24
tasks:
- name: Install NFS-Server
yum: name=nfs-utils state=present
- name: Configure NFS-Server
template: src=./files/exports dest=/etc/exports
notify: Restart Nfs Server
- name: Create Directory
file: name={{ share_dir }} state=directory owner=www group=www recurse=yes
- name: Start NFS-Server
service: name=nfs state=started enabled=yes
- name: Check Nfs Server
shell: cat /var/lib/nfs/etab
register: NFS_Status
- name: Out Nfs Server
debug: msg={{ NFS_Status.stdout_lines }}
handlers:
- name: Restart Nfs Server
service: name=nfs state=restarted
编写应用模块 sersync 的剧本:
- 安装 sersync 服务端;
- 配置 sersync 服务端;
- 启动 sersync 服务端。
下载 sersync 软件包:
[root@m01 ansible_playbook]# ll /etc/ansible/ansible_playbook/file/
-rw-r--r-- 1 root root 727290 Aug 1 12:04 sersync.tar.gz
准备 sersync 实时同步的配置文件:
[root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/conf/confxml.xml.nfs
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="true"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data">
<remote ip="172.16.1.41" name="data"/>
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
编写 sersync 应用 YAML:
[root@m01 ansible_playbook]# cat sersync.yaml
- hosts: nfs
tasks:
- name: Installed Sersync
copy: src=./file/sersync.tar.gz dest=/server/tools/
- name: Tar xf Sersync
shell: cd /server/tools/ && tar xf sersync.tar.gz && mv GNU-Linux-x86 /usr/local/sersync
args:
creates: /usr/local/sersync
- name: Config Sersync
copy: src=./conf/confxml.xml.nfs dest=/usr/local/sersync/confxml.xml
- name: Service Start Sersync
shell: /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
编写 Web 应用模块的剧本:
[root@m01 ansible_playbook]# cat web.yml
- hosts: web
remote_user: root
vars:
remote_nfs_ip: 172.16.1.31
local_dir: /var/www/html/
http_port: 80
tasks:
- name: Installed Httpd Server
yum: name=httpd,php state=present
- name: Configure Httpd Server
template: src=./files/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
- name: Start Httpd Server
service: name=httpd state=started enabled=yes
- name: Mount Nfs Server
mount: src={{remote_nfs_ip}}:/data path={{ local_dir }} fstype=nfs opts=defaults state=mounted
- name: Push kaoshi.zip
unarchive: src=./files/kaoshi.zip dest={{ local_dir }}
handlers:
- name: Restart Httpd Server
service: name=httpd state=restarted
将所有编写好的 YAML 引入至一个文件中, 这样便于一次执行:
[root@m01 ansible_playbook]# cat main.yaml
- import_playbook: base.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
- import_playbook: sersync.yaml
- import_playbook: web.yaml
测试
- 测试 Web 是否能同步数据至 NFS 存储;
- NFS 是否实时同步至 rsync 的/data;
- 使用客户端测试能否推送数据至 rsync 的 backup。
条评论