playbook管理配置文件

时间:2022-07-25
本文章向大家介绍playbook管理配置文件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

笔记内容: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 ~]# 

如上,可以看到,之前增加的那一行注释没有了。

所以,所谓的回滚就是在进行改动前,先备份一份,然后如果出现误修改了,就将之前备份的文件覆盖上去,这样就起到了一个回滚的效果。