ansible-playbook
playbook是由一個或多個模塊組成的,使用多個不同的模塊,完成一件事情,
ansible軟體特點
可以實作批量管理
可以實作批量部署
ad-hoc(批量執行命令)---針對臨時性的操作
ansible clsn -m command -a "hostname" <- 批量執行命令舉例
撰寫劇本-腳本(playbook)---針對重復性的操作
ansible核心功能
pyYAML-----用于ansible撰寫劇本所使用的語言格式(saltstack---python)
rsync-ini語法 sersync-xml語法 ansible-pyYAML語法
paramiko---遠程連接與資料傳輸
Jinja2-----用于撰寫ansible的模板資訊
劇本撰寫規則說明
YAML三板斧
縮進
YAML使用一個固定的縮進風格表示層級結構,每個縮進由兩個空格組成, 不能使用tabs
冒號
以冒號結尾的除外,其他所有冒號后面所有必須有空格,
短橫線
表示串列項,使用一個短橫杠加一個空格,
多個項使用同樣的縮進級別作為同一串列,
劇本書寫格式
- hosts: 172.16.1.7 處理指定服務器 (空格)hosts:(空格)172.16.1.7
task: 劇本所要干的事情; (空格)(空格)task:
- name: (兩個空格)-(空格)name:
command: echo hello clsn linux (四個空格)command:(空格)
劇本格式示例
[root@m01 ansible-playbook]# vim rsync.yaml
- hosts: 172.16.1.41
tasks:
- name: Install Rsync
yum: name=rsync state=installed
劇本檢查方法
ansible-playbook --syntax-check 01.yml
--- 進行劇本配置資訊語法檢查
ansible-playbook -C 01.yml
--- 模擬劇本執行(彩排)
語法檢查
ansible-playbook --syntax-check
[root@m01 ansible-playbook]# ansible-playbook --syntax-check 01.yml
playbook: 01.yml
模擬劇本執行
ansible-playbook -C
[root@m01 ansible-playbook]# ansible-playbook -C 01.yml
PLAY [all] ****************************************************************
TASK [Gathering Facts] ****************************************************
ok: [172.16.1.41]
ok: [172.16.1.8]
此處省略………..
PLAY RECAP ****************************************************************
172.16.1.31 : ok=2 changed=0 unreachable=0 failed=0
172.16.1.41 : ok=2 changed=0 unreachable=0 failed=0
172.16.1.8 : ok=2 changed=0 unreachable=0 failed=0
劇本示例
劇本撰寫內容擴展:劇本任務撰寫多個任務
- hosts: all
tasks:
- name: restart-network
cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
- name: sync time
cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"
劇本撰寫內容擴展:劇本任務撰寫多個主機
- hosts: 172.16.1.7
tasks:
- name: restart-network
cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
- name: sync time
cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"
- hosts: 172.16.1.31
tasks:
- name: show ip addr to file
shell: echo $(hostname -i) >> /tmp/ip.txt
劇本撰寫方式
多主機單任務撰寫方式
多主機多任務撰寫方式
不同主機多任務撰寫方式
Ansible專案案例
環境規劃
全網備份
實時備份
| 角色 | 外網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
需提前準備好的檔案
rsync組態檔
準備對應的組態檔存放至/etc/ansible/ansible_playbook/conf/
[root@m01 conf]# cat /etc/ansible/ansible_playbook/conf/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.password
log file = /var/log/rsyncd.log
#####################################
[backup]
path = /backup
[data]
path = /data
nfs組態檔
準備nfs組態檔exports
[root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/conf/nfs_exports
/data/ 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
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組態檔
準備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>
基礎環境部署
基礎環境:所有機器統一的配置
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: 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=https://www.cnblogs.com/wenrulaogou/p/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"
應用環境: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=https://www.cnblogs.com/wenrulaogou/p/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=https://www.cnblogs.com/wenrulaogou/p/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"
應用環境: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=https://www.cnblogs.com/wenrulaogou/p/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
應用環境:Sersync
1.下載sersync
2.解壓,改名,配置
3.啟動
[root@m01 ansible_playbook]# cat sersync.yaml
- hosts: nfs
tasks:
- name: Scp Sersync
copy: src=https://www.cnblogs.com/wenrulaogou/p/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
應用環境:WEB
掛載nfs共享的目錄
[root@m01 ansible_playbook]# cat web.yaml
- hosts: web
tasks:
- name: Mount NFS Server Share Data
mount: src=https://www.cnblogs.com/wenrulaogou/p/172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted
包含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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/145665.html
標籤:Linux
上一篇:loadrunner·11 controller 出現 runtime Error錯誤!!!跪求大神幫忙!!
下一篇:Vim 基本的使用
