playbook是由一个或多个模块组成的,使用多个不同的模块,完成一件事情。
安装一个服务,配置,并启动,编写剧本时遵循的规则类似于:
1.找谁来拍。
2.大概的任务。
3.具体怎么做。
例:安装httpd服务->playbook
1.安装
2.配置
3.启动
[root@m01 ~]# cat httpd_install.yaml
- hosts: web
tasks:
- name: Install Httpd Server
yum: name=httpd,httpd-tools state=installed
- name: Configure Httpd Server
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: Start Httpd Server
service: name=httpd state=started enabled=yes
2.修改本地拷贝好的httpd.conf文件
3.执行ansible-playbook httpd_install.yaml 推送
角色 | 外网IP(NAT) | 内网IP(LAN) | 部署软件 |
---|---|---|---|
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 |
[root@m01 ~]# mkdir /etc/ansible/ansible_playbook/{file,conf,scripts} -p
[root@m01 ~]# tree /etc/ansible/ansible_playbook/
/etc/ansible/ansible_playbook/
├── conf
└── file
└── scripts
1.需要关闭firewalld以及selinux、epel仓库、ssh端口、优化基础配置
2.需要安装rsync和nfs-utils
3.准备www用户
4.需要准备/etc/rsync.pass密码文件
5.需要准备全网备份脚本
[root@m01 ansible_playbook]# cat base.yaml
- hosts: all
tasks:
- name: Install Epel Repos
get_url: url=http://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel.repo
- name: Dns Client
copy: src=./conf/resolv.conf dest=/etc/resolv.conf
- name: Install Rsync Nfs-Utils
yum: name=rsync,nfs-utils state=installed
- name: Create Group WWW
group: name=www gid=666
- name: Create User WWW
user: name=www uid=666 group=666 create_home=no shell=/sbin/nologin
- name: Create Rsync_Client_Pass
copy: content='1' dest=/etc/rsync.pass mode=600
- name: Create Scripts Directory
file: path=/server/scripts recurse=yes state=directory
- name: Push File Scripts
copy: src=./scripts/rsync_backup_md5.sh dest=/server/scripts/
- name: Crontable Scripts
cron: name="backup scripts" hour=01 minute=00 job="/bin/bash /server/scripts/rsync_backup_md5.sh &>/dev/null"
1.安装rsync
2.配置rsync(配置变更,一定要进行重载操作)
3.创建虚拟用户,权限调整
4.创建目录/data/ /backup
5.启动rsync
6.配置邮箱->邮箱的发件人->校验的脚本
[root@m01 ansible_playbook]# cat rsync.yaml
- hosts: backup
tasks:
- name: Installed Rsync Server
yum: name=rsync,mailx state=installed
- name: configure Rsync Server
copy: src=./conf/rsyncd.conf dest=/etc/rsyncd.conf
notify: Restart Rsync Server
- name: Create Virt User
copy: content='rsync_backup:1' dest=/etc/rsync.password mode=600
- name: Create Data
file: path=/data state=directory recurse=yes owner=www group=www mode=755
- name: Create Backup
file: path=/backup state=directory recurse=yes owner=www group=www mode=755
- name: Start RsyncServer
service: name=rsyncd state=started enabled=yes
- name: Push Check Scripts
copy: src=./scripts/rsync_check_backup.sh dest=/server/scripts/
- name: Crond Check Scripts
cron: name="check scripts" hour=05 minute=00 job="/bin/bash /server/scripts/rsync_check_backup.sh &>/dev/null"
handlers:
- name: Restart Rsync Server
service: name=rsyncd state=restarted
1.安装nfs-utils
2.配置nfs(当修改了配置,触发重载操作)
3.创建目录,授权
4.启动
[root@m01 ansible_playbook]# cat nfs.yaml
- hosts: nfs
tasks:
- name: Installed Nfs Server
yum: name=nfs-utils state=installed
- name: Configure Nfs Server
copy: src=./conf/exports dest=/etc/exports
notify: Restart Nfs Server
- name: Create Share Data
file: path=/data state=directory recurse=yes owner=www group=www mode=755
- name: Start Nfs Server
service: name=nfs-server state=started enabled=yes
handlers:
- name: Restart Nfs Server
service: name=nfs-server state=restarted
1.下载sersync
2.解压,改名,配置
3.启动
[root@m01 ansible_playbook]# cat sersync.yaml
- hosts: nfs
tasks:
- name: Scp Sersync
copy: src=./file/sersync2.5.4_64bit_binary_stable_final.tar.gz dest=/usr/local/sersync.tar.gz
- name: Zip
shell: cd /usr/local && tar xf sersync.tar.gz && mv GNU-Linux-x86 sersync
args:
creates: /usr/local/sersync
- name: configure Sersync
copy: src=./conf/confxml.xml dest=/usr/local/sersync/
- name: Start Sersync
shell: /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
1、挂载nfs共享的目录
[root@m01 ansible_playbook]# cat web.yaml
- hosts: web
tasks:
- name: Mount NFS Server Share Data
mount: src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted
[root@m01 ansible_playbook]# cat mail.yaml
- import_playbook: base.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
- import_playbook: sersync.yaml
- import_playbook: web.yaml