- ansible playbook劇本介紹:
- playbook 是ansible用于配置,部署和管理被節點的劇本
- 由一個或多個模塊組成,完成統一的目的,實現自動化操作
- 劇本編寫需遵循yaml語法
- yaml的三要素:
- 縮進:兩個字符,默認的tab鍵是四個字符,所以要使用tab鍵,需要修改/root/.vimrc文件。添加:set tabstop=2
- 冒號:冒號后面需要空格,除非以冒號結尾
- 短橫杠:列表項,后面跟空格
- yaml的三要素:
- playbook的語法結構:
- ansible-playbook 選項 文件路徑
- 選項:-C 模擬預運行
- --list-hosts:列出清單
- --list-tasks:列出任務
- --list-tags:列出標簽
- --syntax-check:語法檢查
- 選項:-C 模擬預運行
- ansible-playbook 選項 文件路徑
- Ansible playbook使用場景:
- 執行一些簡單的任務可以使用ad-hoc命令,過于復雜時就需要使用playbook劇本
- playbook劇本就像執行shell命令與寫shell腳本一樣,也可以理解為批量處理任務
- 使用playbook可以方便的重復使用這些代碼,可以移植到不同機器上,像函數一樣反復使用
- 實驗場景:
- 拓撲:
- ansible:192.168.8.5
- web:192.168.8.6
- nfs:192.168.8.7
- rsync:192.168.8.8
- 實驗說明:在第一臺機器上部署ansible,編寫playbook劇本,完成一鍵部署web,nfs,rsync架構的環境
- 實驗步驟:
- 1.在ansible上修改hosts文件
- vim /etc/hosts
- 192.168.8.5 ansible
- 192.168.8.6 web
- 192.168.8.7 nfs
- 192.168.8.8 rsync
- vim /etc/hosts
- 2.將每臺服務器的主機名稱修改為對應的服務名:
- hostnamectl set-hostname ansible
- hostnamectl set-hostname web
- hostnamectl set-hostname nfs
- hostnamectl set-hostname rsync
- 3.在ansible服務器上修改tab鍵=2
- vim /root/.vimrc
- set tabstop=2
- vim /root/.vimrc
- 4.在8.5主機安裝ansible和epel-release(提供額外軟件包)
- ymm -y install ansible
- yum -y install epel-release
- 5.ssh免密登錄8.6、8.7、8.8
- ssh-keygen -t rsa
- ssh-copy-id root@web
- ssh-copy-id root@nfs
- ssh-copy-id root@rsync
- 6.創建ansible劇本中所需的目錄
- mkdir -p /etc/ansible/ansible_playbook/{conf,file,scripts,tools}
- 7.編輯ansible清單:
- vim /etc/ansible/hosts
- 添加:
- [web]
- 192.168.8.6
- [nfs]
- 192.168.8.7
- [rsync]
- 192.168.8.8
- 添加:
- vim /etc/ansible/hosts
- 8.使用ansible的copy模塊 覆蓋另外三臺的hosts文件
- ansible all -m copy -a "src=/etc/hosts dest=/etc"
- 9.編寫playbook劇本部署基本環境:
- 關閉防火墻
- 配置yum倉庫
- 安裝rsync、nfs-utils
- 創建組、用戶
- 創建目錄,修改權限
- 推送腳本
- 推送rsync客戶端所需的密碼文件,修改權限
- 計劃任務
- vim /etc/ansible/ansible_playbook/base.yaml
- - hosts: all
- tasks:
- - name: stop firewalld
- shell: systemctl stop firewalld
- - name: stop selinux
- shell: setenforce 0
- - name: clear repos.d
- file: path=/etc/yum.repos.d/ state=absent
- - name: create repos.d
- file: path=/etc/yum.repos.d/ recurse=yes
- - name: install base repo
- get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo
- - name: install epel repo
- 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 create_home=no shell=/sbin/nologin
- - name: create rsync client password
- copy: content='1' dest=/etc/rsync.pass mode=600
- - name: create scripts directory
- file: path=/server/scripts/ recurse=yes state=directory
- - name: push scripts
- copy: src=./scripts/rsync_backup.sh dest=/server/scripts
- - name: crontab
- cron: name="backup scripts" hour=01 minute=00 job="/usr/bin/bash /server/scripts/rsync_backup.sh &> /dev/null"
- - name: stop firewalld
- tasks:
- 進入到腳本目錄,將需要的腳本拖拽至目錄下
- cd /etc/ansible/ansible_playbook/scripts
- 預先運行腳本,檢查語法有沒有錯誤
- ansible-playbook -C base.yaml
- - hosts: all
- 10.編輯對rsync主機進行配置的劇本
- 劇本流程:
- 安裝rsync
- 配置
- 啟動
- 腳本
- 計劃任務
- vim /etc/ansible/ansible_playbook/rsync.yaml
- - hosts: rsync
- tasks:
- - name: install rsync
- yum: name=rsync state=installed
- - name: config rsync
- copy: src=./conf/rsyncd.conf dest=/etc/rsyncd.conf
- notify: restart rsync
- - name: create rsync local 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 rsync
- service: name=rsyncd state=started enabled=yes
- - name: push check scripts
- copy: src=./scripts/rsync_check.sh dest=/server/scripts
- - name: crond check scripts
- cron: name="check scripts" hour=05 minute=00 job="/usr/bin/bash /server/scripts/rsync_check.sh &> /dev/null"
- handlers:
- - name: restart rsync
- service: name=rsyncd state=restarted
- 將所需配置文件拖至conf目錄下
- cd /etc/ansible/ansible_playbook/conf
- 運行腳本,檢查是否有錯誤
- ansible-playbook -C rsync.yaml
- 劇本流程:
- 11.編寫部署nfs服務的劇本:
- vim nfs.yaml
- - hosts: nfs
- tasks:
- - name: install nfs
- yum: name=nfs-utils,rpcbind state=installed
- - name: config nfs
- copy: src=./conf/exports dest=/etc/exports
- notify: restart nfs
- - name: create data
- file: path=/data state=directory recurse=yes owner=www group=www mode=755
- - name: start nfs
- service: name=nfs-server state=started enabled=yes
- handlers:
- - name: restart nfs
- service: name=nfs-server state=restarted
- 預運行檢查語法:
- ansible-playbook -C nfs.yaml
- vim nfs.yaml
- 12.部署sersync服務,實現及時監控
- 劇本流程:
- (1)在ansible服務器先下載sersync
- (2)解壓到/etc/ansible/ansible_playbook/并修改配置文件
- (3)推送到nfs
- (4)啟動sersync
- 進入tools目錄 將存放及時監控的軟件目錄 拖拽至tools目錄下
- cd /etc/ansible/ansible_playbook/tools
- 拖拽
- cd /etc/ansible/ansible_playbook/tools
- 編輯劇本:
- vim sersync.yaml
- - hosts: nfs
- tasks:
- - name: scp sersync
- copy: src=./tools/sersync/ dest=/usr/local/sersync owner=www group=www mode=755
- - name: start sersync
- shell: pgrep sersync;
- [ $? -eq 0 ] || /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
- vim sersync.yaml
- 劇本流程:
- 13.編寫部署web服務的劇本:
- 劇本流程:
- (1)本地安裝httpd
- (2)修改配置文件,復制到/etc/ansible/ansible_playbook/conf
- (3)掛載
- (4)啟動
- vim web.yaml
- - hosts: web
- tasks:
- - name: install httpd
- yum: name=httpd state=installed
- - name: mount nfs
- mount: src=nfs:/data path=/var/www/html fstype=nfs state=mounted
- - name: config httpd
- copy: src=./conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
- notify: restart httpd
- - name: start httpd
- service: name=httpd state=started enabled=yes
- handlers:
- - name: restart httpd
- service: name=httpd state=restarted
- 預運行檢查語法:
- ansible-playbook -C web.yaml
- 劇本流程:
- 至此所有劇本已編寫好了,現在將這幾個劇本依次匯總 集中來進行實際運行
- vim main.yaml
- - import_playbook: base.yaml
- - import_playbook: rsync.yaml
- - import_playbook: nfs.yaml
- - import_playbook: sersync.yaml
- - import_playbook: web.yaml
- 預檢測:ansible-playbook -C main.yaml
- 執行:ansible-playbook main.yaml
- 1.在ansible上修改hosts文件
- 測試:在nfs的/data目錄下編寫一個網頁 查看web服務器上的網頁根目錄是否同步到了網頁
- 再查看及時同步:nfs服務器中的/data目錄下的文件,是否及時的自動備份到了 rsync服務器的/backup目錄下
- 拓撲:
- 易錯的地方:
- 如果rsync服務器的rsync服務起不來,可能需要從新在nfs服務器執行一下此命令:
- /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
- 或者是rsync服務器的配置文件沒有修改:
- vim /etc/rsyncd.conf
-
- vim /etc/rsyncd.conf
- 也可能或是有了pid鎖文件,導致服務被鎖死
- rsyncd --daemon 此命令也可以啟動rsync
- 如果rsync服務器的rsync服務起不來,可能需要從新在nfs服務器執行一下此命令: