playbook管理配置文件
http://blog.51cto.com/zero01/2067444
筆記內容:playbook管理配置文件
筆記日期:2018-01-31
- 24.29/24.30 playbook管理配置文件
playbook管理配置文件
上一篇文章中我們成功的通過playbook安裝了nginx,而生產環境中大多時候是需要管理配置文件的,例如修改配置文件然後進行重啟服務,修改配置文件時可能會出現誤修改的情況,所以我們還需要準備一個回滾的操作。至於安裝軟件包只是在初始化環境的時候用一下。下面我們來寫個管理nginx配置文件的playbook。
1.創建相應的目錄:
[root@server ~]# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks} [root@server ~]# cd /etc/ansible/nginx_config/ [root@server /etc/ansible/nginx_config]# ls roles [root@server /etc/ansible/nginx_config]# ls roles/ new old [root@server /etc/ansible/nginx_config]# ls roles/new/ files handlers tasks vars [root@server /etc/ansible/nginx_config]# ls roles/old/ files handlers tasks vars [root@server /etc/ansible/nginx_config]#
其中new為更新時用到的,old為回滾時用到的,files下面為nginx.conf和vhosts目錄,handlers為重啟nginx服務的命令。
關於回滾,需要在執行playbook之前先備份一下舊的配置,所以對於老配置文件的管理一定要嚴格,千萬不能隨便去修改線上機器的配置,並且要保證new/files下面的配置和線上的配置一致。
2.把nginx.conf和vhost目錄放到files目錄下面,我這裏是之前創建了vhost目錄的,如果你沒有創建過這個目錄的話,就只需要拷貝nginx.conf即可:
[root@server /etc/ansible/nginx_config]# cd /usr/local/nginx/conf/ [root@server /usr/local/nginx/conf]# cp -r nginx.conf vhost /etc/ansible/nginx_config/roles/new/files/ [root@server /usr/local/nginx/conf]#
3.編輯用於定義變量的文件:
[root@server ~]# vim /etc/ansible/nginx_config/roles/new/vars/main.yml
nginx_basedir: /usr/local/nginx
4.編輯用於定義重新加載nginx服務的文件:
[root@server ~]# vim /etc/ansible/nginx_config/roles/new/handlers/main.yml
- name: restart nginx
shell: /etc/init.d/nginx reload
5.編輯用於執行核心任務的文件:
[root@server ~]# vim /etc/ansible/nginx_config/roles/new/tasks/main.yml - name: copy conf file 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: vhost, dest: conf/ } notify: restart nginx
6.最後是定義總入口配置文件:
[root@server ~]# vim /etc/ansible/nginx_config/update.yml
---
- hosts: testhost
user: root
roles:
- new
7.執行總入口配置文件:
[root@server ~]# ansible-playbook /etc/ansible/nginx_config/update.yml
PLAY [testhost] *************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************
ok: [192.168.77.128]
TASK [new : copy conf file] *************************************************************************************************
ok: [192.168.77.128] => (item={u‘dest‘: u‘conf/nginx.conf‘, u‘src‘: u‘nginx.conf‘})
ok: [192.168.77.128] => (item={u‘dest‘: u‘conf/‘, u‘src‘: u‘vhost‘})
PLAY RECAP ******************************************************************************************************************
192.168.77.128 : ok=2 changed=0 unreachable=0 failed=0
[root@server ~]#
8.然後更改一下配置文件的內容:
[root@server ~]# vim /etc/ansible/nginx_config/roles/new/files/nginx.conf
# 增加一行註釋內容
9.再次執行以下命令:
[root@server ~]# ansible-playbook /etc/ansible/nginx_config/update.yml
PLAY [testhost] *************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************
ok: [192.168.77.128]
TASK [new : copy conf file] *************************************************************************************************
changed: [192.168.77.128] => (item={u‘dest‘: u‘conf/nginx.conf‘, u‘src‘: u‘nginx.conf‘})
ok: [192.168.77.128] => (item={u‘dest‘: u‘conf/‘, u‘src‘: u‘vhost‘})
RUNNING HANDLER [new : restart nginx] ***************************************************************************************
changed: [192.168.77.128]
PLAY RECAP ******************************************************************************************************************
192.168.77.128 : ok=3 changed=2 unreachable=0 failed=0
[root@server ~]#
10.然後到客戶端上查看一下nginx.conf文件,是否有我們加上的那一行內容:
[root@client ~]# tail -n2 /usr/local/nginx/conf/nginx.conf
# 增加一行註釋內容
}
[root@client ~]#
以上的操作是針對於更新、修改配置文件的,下面來介紹一下回滾操作:
1.回滾對應的roles為old,所以首先把new目錄下的所有文件同步到old目錄下,這一步相當於是備份一份在old目錄下,之後回滾就是從old目錄下進行拷貝文件:
[root@server ~]# rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/
sending incremental file list
files/
files/nginx.conf
files/vhost/
files/vhost/aaa.com.conf
files/vhost/default.conf
files/vhost/ld.conf
files/vhost/proxy.conf
files/vhost/ssl.conf
files/vhost/test.com.conf
handlers/
handlers/main.yml
tasks/
tasks/main.yml
vars/
vars/main.yml
sent 5171 bytes received 222 bytes 10786.00 bytes/sec
total size is 4391 speedup is 0.81
[root@server ~]#
回滾操作就是把舊的配置覆蓋,然後重新加載nginx服務, 每次改動nginx配置文件之前先備份到old裏,對應目錄為/etc/ansible/nginx_config/roles/old/files。如果你修改nginx配置文件之前沒有備份old裏,那麽你就無法進行回滾操作了。
編輯總入口配置文件:
[root@server ~]# vim /etc/ansible/nginx_config/rollback.yml
---
- hosts: testhost
user: root
roles:
- old
ok,完成以上操作後,我們來演示一個簡單的回滾操作:
1.例如我現在要修改nginx配置文件,我在要這個文件的末尾增加一行註釋,但是註意了,在增加這行註釋之前需要先將配置文件同步到old目錄下:
[root@server ~]# rsync -av /etc/ansible/nginx_config/roles/new/files/nginx.conf /etc/ansible/nginx_config/roles/old/files/nginx.conf
sending incremental file list
nginx.conf
sent 1465 bytes received 31 bytes 2992.00 bytes/sec
total size is 1387 speedup is 0.93
[root@server ~]#
2.然後才增加這行註釋:
[root@server ~]# echo "# 這是一行註釋內容" >> /etc/ansible/nginx_config/roles/new/files/nginx.conf
[root@server ~]#
3.執行update.yml文件向客戶端更新文件:
[root@server ~]# ansible-playbook /etc/ansible/nginx_config/update.yml
PLAY [testhost] *************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************
ok: [192.168.77.128]
TASK [new : copy conf file] *************************************************************************************************
changed: [192.168.77.128] => (item={u‘dest‘: u‘conf/nginx.conf‘, u‘src‘: u‘nginx.conf‘})
ok: [192.168.77.128] => (item={u‘dest‘: u‘conf/‘, u‘src‘: u‘vhost‘})
RUNNING HANDLER [new : restart nginx] ***************************************************************************************
changed: [192.168.77.128]
PLAY RECAP ******************************************************************************************************************
192.168.77.128 : ok=3 changed=2 unreachable=0 failed=0
[root@server ~]#
4.然後到客戶端上查看是否有我們增加的那一行註釋:
[root@client ~]# tail -n1 /usr/local/nginx/conf/nginx.conf
# 這是一行註釋內容
[root@client ~]#
5.確認之後,執行rollback.yml文件進行回滾操作:
[root@server ~]# ansible-playbook /etc/ansible/nginx_config/rollback.yml
PLAY [testhost] *************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************
ok: [192.168.77.128]
TASK [old : copy conf file] *************************************************************************************************
changed: [192.168.77.128] => (item={u‘dest‘: u‘conf/nginx.conf‘, u‘src‘: u‘nginx.conf‘})
ok: [192.168.77.128] => (item={u‘dest‘: u‘conf/‘, u‘src‘: u‘vhost‘})
RUNNING HANDLER [old : restart nginx] ***************************************************************************************
changed: [192.168.77.128]
PLAY RECAP ******************************************************************************************************************
192.168.77.128 : ok=3 changed=2 unreachable=0 failed=0
[root@server ~]#
6.然後到客戶端上查看是否已恢復:
[root@client ~]# tail -n1 /usr/local/nginx/conf/nginx.conf
}
[root@client ~]#
如上,可以看到,之前增加的那一行註釋沒有了。
所以,所謂的回滾就是在進行改動前,先備份一份,然後如果出現誤修改了,就將之前備份的文件覆蓋上去,這樣就起到了一個回滾的效果。
playbook管理配置文件