playbook管理配置文件(总结)

- 将我们把一个服务部署到客户机上后(以nginx为例),我们经常需要更改一个配置文件,配置文件改好后我们还需要加载nginx的服务,这时就用到了管理配置文件,有时也会出现这样一个场景当我们更改了一个配置文件,发现改错了,需要回滚到之前的配置,并且对回滚的配置进行加载,这样我们应该怎么实现呢?也可以用playbook实现

- 如下是操作:

基本的目录创建与介绍

1
2
3
4
5
6
[root@chy01 nginx_install] # mkdir  -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启nginx服务的命令
[root@chy01 ansible] # cd nginx_config/
[root@chy01 nginx_config] # ls roles/
new  old
关于回滚,需要在执行playbook之前先备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不能随便去修改线上机器的配置,并且要保证new /files 下面的配置和线上的配置一致

先把nginx.conf和vhosts目录放到files目录下面

1
[root@chy01 conf] # cp -r nginx.conf vhost/ /etc/ansible/nginx_config/roles/new/files/

定义变量

1
2
[root@chy01 conf] # vim /etc/ansible/nginx_config/roles/new/vars/main.yml 
nginx_basedir:  /usr/local/nginx

定义重新加载nginx服务

1
2
3
4
[root@chy01 conf] # vim /etc/ansible/nginx_config/roles/new/handlers/main.yml  
[root@chy01 conf] # vim /etc/ansible/nginx_config/roles/new/handlers/main.yml  
- name: restart nginx
   shell:  /etc/init .d /nginx  reload

定义需要cp的目录(核心任务)

1
2
3
4
5
6
7
8
[root@chy01 conf] # 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

最后定义总入口配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@chy01 conf] # vim /etc/ansible/nginx_config/update.yml 
---
- hosts: chy02
   user: root
   roles:
   - new
[root@chy01 conf] # ansible-playbook /etc/ansible/nginx_config/update.yml
[root@chy01 conf] # ansible-playbook /etc/ansible/nginx_config/update.yml
 
PLAY [chy02] *********************************************************************************************************************
 
TASK [Gathering Facts] ***********************************************************************************************************
ok: [chy02]
 
TASK [new : copy conf  file ] ******************************************************************************************************
ok: [chy02] => (item={u 'dest' : u 'conf/nginx.conf' , u 'src' : u 'nginx.conf' })
ok: [chy02] => (item={u 'dest' : u 'conf/' , u 'src' : u 'vhost' })
 
PLAY RECAP ***********************************************************************************************************************
chy02                      : ok=2    changed=0    unreachable=0    failed=0   
// 执行成功

现在进行变更的一个测试

1
2
3
4
5
6
7
8
[root@chy01 files] # tail -2 nginx.conf 
#   include vhost/*.conf;
   }
(更改一下nginx的配置文件)
[root@chy02 ~] # tail -2 /usr/local/nginx/conf/nginx.conf
#   include vhost/*.conf;
   }
(在客户机上查看配置文件的include这行也是被注释的)

现在进行回滚操做

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@chy01 files] # 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 .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 5141 bytes  received 203 bytes  10688.00 bytes /sec
total size is 4430  speedup is 0.83
回滚操作就是把旧的配置覆盖,然后重新加载nginx服务, 每次改动nginx配置文件之前先备份到old里,对应目录为 /etc/ansible/nginx_config/roles/old/files 
[root@chy01 files] #  vim /etc/ansible/nginx_config/rollback.yml //最后是定义总入口配置
---
- hosts: chy02
   user: root
   roles:
   - old 
需要注意一点是在操作时必须要进行将你要操作的配置文件 cp 到old下才可以进行回滚,如果没有 cp 是不能进行回滚的)

希望看过的童鞋多多指教,谢谢!j_0063.gifj_0063.gif