Ansible 三

playbook是由一個或多個模塊組成的,使用多個不一樣的模塊,完成一件事情。php

yum
copy
service
安裝一個服務,配置,並啓動。
1.找誰來拍。
2.大概的任務。
3.具體怎麼作。linux


安裝httpd服務->playbook
1.安裝
2.配置
3.啓動web

 

[root@m01 ~]# cat httpd_install.yaml
#這是一個ansible的playbookshell

- hosts: web
tasks:bash

- name: Install Httpd Server
yum: name=httpd,httpd-tools state=installedssh

- 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 推送ui

 

 

 

1.環境規劃
角色 外網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 httpdurl


1.全網備份
2.實時備份

0.目錄規劃
[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.基礎環境:全部機器統一的配置
1.須要關閉firewalld以及selinux、epel倉庫、ssh端口、優化基礎配置
2.須要安裝rsync和nfs-utils
3.準備www用戶
4.須要準備/etc/rsync.pass密碼文件
5.須要準備全網備份腳本

基礎的playbook劇本
[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"


##########
2.應用環境:Rsync
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

3.應用環境:NFS
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

4.應用環境:Sersync
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

5.應用環境:WEB

一、掛載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

6.包含include
[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

7.快照還原 1.推送公鑰 2.使用ping模塊測試 3.執行ansible-playbook測試 4.測試全網備份,測試是否能發郵件,並校驗結果 5.測試nfs的掛載 6.測試實時同步 ######################################################################擴展: 1.安裝httpd,php服務 [root@web01 ~]# yum install httpd php -y 2.配置httpd服務 -----上qq羣下載httpd.conf文件便可-------- 3.上傳代碼 [root@web01 conf]# cd /data/ -------上傳kaoshi.zip文件------- 4.解壓代碼 [root@web01 data]# unzip kaoshi.zip 5.啓動httpd服務 [root@web01 conf]# systemctl start httpd

相關文章
相關標籤/搜索