目录
- ansible-playbook 基础介绍
- 1.
YAML
三板斧 - 2.
ansible playbook
安装apache 示例
- 1.
- 案例 全网备份 实时备份
- 环境规划
- 目录规划
- base.yaml
- rsync.yaml
- nfs.yaml
- sersync.yaml
- web.yaml
- mail.yaml
ansible-playbook 基础介绍
playbook
是由一个或多个模块组成的,使用多个不同的模块,完成一件事情。playbook
通过yaml
语法识别描述的状态文件。扩展名是yaml
1.YAML
三板斧
- 缩进
- YAML使用一个固定的缩进风格表示层级结构,每个缩进由两个空格组成, 不能使用tabs
- 冒号
- 以冒号结尾的除外,其他所有冒号后面所有必须有空格。
- 短横线
- 表示列表项,使用一个短横杠加一个空格。
- 多个项使用同样的缩进级别作为同一列表。
- 安装httpd服务->playbook
1.安装
2.配置
3.启动
2. ansible playbook
安装apache 示例
[root@m01 ansible_playbook]# vim webserver.yaml
- hosts: webtasks:- name: Install Httpd Serveryum: name=httpd,httpd-tools state=installed- name: Configgure Httpd Servercopy: src=./file/httpd.conf dest=/etc/httpd/conf/httpd.confnotify: Resart Httpd Server- name: Start Httpd Serverservice: name=httpd state=started enabled=yeshandlers:- name: Resart Httpd Serverservice: name=httpd state=restarted
案例 全网备份 实时备份
环境规划
角色 | 外网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 ansible_playbook]# pwd
/etc/ansible/ansible_playbook
[root@m01 ansible_playbook]# tree
.
├── base.yaml
├── conf
│ ├── confxml.xml
│ ├── exports
│ ├── resolv.conf
│ ├── rsyncd.conf
│ └── web.yaml
├── file
│ └── sersync2.5.4_64bit_binary_stable_final.tar.gz
├── mail.yaml
├── nfs.yaml
├── rsync.retry
├── rsync.yaml
├── scripts
│ ├── rsync_backup_md5.sh
│ └── rsync_check_backup.sh
└── sersync.yaml3 directories, 14 files
base.yaml
[root@m01 ansible_playbook]# vim base.yaml
- hosts: alltasks:- name: clear yum.repos.dfile: path=/etc/yum.repos.d/ state=absent - name: Create yum.repos.dfile: path=/etc/yum.repos.d/ state=directory - name: Install Base Reposget_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo - name: Install Epel Reposget_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/epel.repo- name: Dns Client copy: src=./conf/resolv.conf dest=/etc/rsolv.conf- name: Install Rsync Nfs-Utilsyum: name=rsync,nfs-utils state=installed- name: Create Group WWWgroup: name=www gid=666- name: Create User WWWuser: name=www uid=666 group=666 create_home=no shell=/sbin/nologin- name: Create Rsync_Client_Passcopy: content='1' dest=/etc/rsync.pass mode=600- name: Create Sripts Directoryfile: path=/server/scripts/ recurse=yes state=directory - name: Push Scriptscopy: src=./scripts/rsync_backup_md5.sh dest=/server/scripts/- name: Crontable Scriptscron: name="backup scripts" hour=01 minute=00 job="/usr/bin/bash /server/scripts/rsync_backup_md5.sh &>/dev/null"
rsync.yaml
[root@m01 ansible_playbook]# cat rsync.yaml
- hosts: backuptasks:- name: Installed Rsync Serveryum: name=rsync,mailx state=installed- name: configure Rsync Servercopy: src=/etc/ansible/ansible_playbook/conf/rsyncd.conf dest=/etc/rsyncd.confnotify: Restart Rsync Server- name: Create Virt Usercopy: content='rsync_backup:1' dest=/etc/rsync.password mode=600- name: Create Datefile: path=/data state=directory recurse=yes owner=www group=www mode=755- name: Create Backupfile: path=/backup state=directory recurse=yes owner=www group=www mode=755- name: Start RsyncServerservice: name=rsyncd state=started enabled=yes- name: Push Check Scriptscopy: src=./scripts/rsync_check_backup.sh dest=/server/scripts/- name: Crond Check Scriptscron: name="check scripts" hour=05 minute=00 job="/usr/bin/bash /server/scripts/rsync_check_backup.sh &>/dev/null"handlers:- name: Restart Rsync Serverservice: name=rsyncd state=restarted
nfs.yaml
[root@m01 ansible_playbook]# cat nfs.yaml
- hosts: nfstasks:- name: Installed Nfs Serveryum: name=nfs-utils state=installed- name: Configure Nfs Servercopy: src=./conf/exports dest=/etc/exportsnotify: Restart Nfs Server- name: Create Share Datafile: path=/data state=directory recurse=yes owner=www group=www mode=755- name: Start Nfs Serverservice: name=nfs-server state=started enabled=yeshandlers:- name: Restart Nfs Serverservice: name=nfs-server state=restarted
sersync.yaml
[root@m01 ansible_playbook]# cat sersync.yaml
- hosts: nfstasks:- name: Scp Sersynccopy: src=./file/sersync2.5.4_64bit_binary_stable_final.tar.gz dest=/usr/local/sersync.tar.gz- name: Zipshell: cd /usr/local && tar xf sersync.tar.gz && mv GNU-Linux-x86 sersyncargs:creates: /usr/local/sersync- name: configure Sersynccopy: src=./conf/confxml.xml dest=/usr/local/sersync/confxml.xmlnotify: kill old sersync and restart new sersync- name: Start Sersyncshell: pgrep sersync;[ $? -eq 0 ] || /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xmlhandlers:- name: kill old sersync and restart new sersyncshell: pgrep sersync | xargs kill -9;/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
web.yaml
[root@m01 ansible_playbook]# cat web.yaml
- hosts: webtasks:- name: Mount NFS Server Share Datemount: src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted- name: Install Httpd Phpyum: name=httpd,php state=installed- name: Configurl copycopy: src=./conf/httpd.conf dest=/etc/httpd/conf/httpd.confnotify: Restart Httpd- name: Unzip kaoshi.zipunarchive: src=./file/kaoshi.zip dest=/data/ creates=/data/index.html- name: Start Httpdservice: name=httpd state=started enabled=yeshandlers:- name: Restart Httpdservice: name=httpd state=restarted
mail.yaml
[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