背景:安裝軟體包,隻是在初始化環境時用一下,大多時候需要修改配置檔案;管理Nginx配置檔案的playbook,執行playbook之前,一定要備份舊的配置;機器上的配置檔案需和new/files保持一緻
1. 建立目錄和配置檔案
說明:new為更新時用到,old為復原時用到,即将new目錄内容拷貝進來
mkdir -p /etc/anxible/nginx_config/roles/new/{files,handlers,vars,tasks}
mkdir -p /etc/anxible/nginx_config/roles/old
<a href="https://s1.51cto.com/wyfs02/M01/8F/2B/wKioL1jV94TzqMcgAAAsAd5zSVE278.png" target="_blank"></a>
nginx_config:
vim /etc/ansible/nginx_config/update.yml # 最新總入口配置
---
- hosts: agent.huangzp.com
user: root
roles:
- new
<a href="https://s3.51cto.com/wyfs02/M02/8F/2D/wKiom1jV9sGByaioAAAOSIItTgc021.png" target="_blank"></a>
vim /etc/ansible/nginx_config/backup.yml # 復原總入口配置,
- old
<a href="https://s3.51cto.com/wyfs02/M00/8F/2B/wKioL1jV9unDBrzVAAAORc-VM2A398.png" target="_blank"></a>
注:復原時,backup.yml對應的roles為old,即把舊的配置覆寫,然後重新加載Nginx服務
files:目錄下為nginx.conf和vhosts目錄
cp -r /usr/local/nginx/conf/{nginx.conf,vhosts} /etc/ansible/nginx_config/roles/new/files
handlers:目錄下為重新開機nginx服務的指令
vim /etc/ansible/nginx_config/roles/new/handlers/main.yml #定義重新加載Nginx服務
- name: restart nginx
shell: /etc/init.d/nginx reload
<a href="https://s2.51cto.com/wyfs02/M02/8F/2B/wKioL1jV9wnAgC9AAAAStpWOwzs716.png" target="_blank"></a>
vars:
vim /etc/ansible/nginx_config/roles/new/vars/main.yml # 定義變量
nginx_basedir: /usr/local/nginx
<a href="https://s1.51cto.com/wyfs02/M00/8F/2D/wKiom1jV9xngqTz_AAAKzZw8mrc287.png" target="_blank"></a>
tasks:
vim /etc/ansible/nginx_config/roles/new/tasks/main.yml # 核心任務,拷貝配置目錄和vhosts目錄
- name: copy config
copy: src=` item`.`src ` dest=` nginx_basedir `/` item`.`dest ` backup=yes owner=root group=root mode=0644
with_items:
- { src: nginx.conf, dest: conf/nginx.conf }
- { src: vhosts, dest: conf/ }
notify: restart nginx
<a href="https://s5.51cto.com/wyfs02/M00/8F/2B/wKioL1jV9y6z3UEUAAAo4W-NG9Y745.png" target="_blank"></a>
2. 執行過程
2.1 vim /etc/ansible/nginx_config/roles/new/files/vhosts/1.conf //加入
test1
vim /etc/ansible/nginx_config/roles/new/files/nginx.conf //加入
include vhosts/*.conf;
2.2 執行:ansible-playbook update.yml
<a href="https://s3.51cto.com/wyfs02/M01/8F/2B/wKioL1jV97vgdcSlAAA4wCG97GI327.png" target="_blank"></a>
配置檔案同步到agent機器:
<a href="https://s1.51cto.com/wyfs02/M02/8F/2D/wKiom1jV9_PxnsXrAAAJh_ccW4Y254.png" target="_blank"></a>
2.3 vim /etc/ansible/nginx_config/roles/new/files/vhosts/1.conf //将配置檔案内容"test1" 修改為"test2",修改之前先備份至old
rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/
<a href="https://s4.51cto.com/wyfs02/M00/8F/2D/wKiom1jV-ArS9DhPAABBpiVvtsU101.png" target="_blank"></a>
2.4 再次執行,ansible-playbook update.yml,配置檔案修改了,Nginx重新開機服務
<a href="https://s3.51cto.com/wyfs02/M00/8F/2B/wKioL1jV-CfDQNiJAABEqV3ssVo723.png" target="_blank"></a>
配置檔案更新:
<a href="https://s1.51cto.com/wyfs02/M01/8F/2D/wKiom1jV-DmCtTa4AAAJBkEHuLQ928.png" target="_blank"></a>
2.5 執行復原 ansible-playbook backup.yml,配合檔案修改了,Nginx再次重新開機
<a href="https://s5.51cto.com/wyfs02/M00/8F/2D/wKiom1jV-EaS57P0AABEppAh9sw888.png" target="_blank"></a>
配置檔案復原到原來:
<a href="https://s3.51cto.com/wyfs02/M01/8F/2B/wKioL1jV-FXiBIxlAAAIltZ372g339.png" target="_blank"></a>
本文轉自 huangzp168 51CTO部落格,原文連結:http://blog.51cto.com/huangzp/1910288,如需轉載請自行聯系原作者