playbook管理配置文件(总结)
- 将我们把一个服务部署到客户机上后(以nginx为例),我们经常需要更改一个配置文件,配置文件改好后我们还需要加载nginx的服务,这时就用到了管理配置文件,有时也会出现这样一个场景当我们更改了一个配置文件,发现改错了,需要回滚到之前的配置,并且对回滚的配置进行加载,这样我们应该怎么实现呢?也可以用playbook实现
- 如下是操作:
基本的目录创建与介绍
<code>[root@chy01 nginx_install]</code><code># mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}</code>
<code>其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启nginx服务的命令</code>
<code>[root@chy01 ansible]</code><code># cd nginx_config/</code>
<code>[root@chy01 nginx_config]</code><code># ls roles/</code>
<code>new old</code>
<code>关于回滚,需要在执行playbook之前先备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不能随便去修改线上机器的配置,并且要保证new</code><code>/files</code><code>下面的配置和线上的配置一致</code>
先把nginx.conf和vhosts目录放到files目录下面
<code>[root@chy01 conf]</code><code># cp -r nginx.conf vhost/ /etc/ansible/nginx_config/roles/new/files/</code>
定义变量
<code>[root@chy01 conf]</code><code># vim /etc/ansible/nginx_config/roles/new/vars/main.yml </code>
<code>nginx_basedir: </code><code>/usr/local/nginx</code>
定义重新加载nginx服务
<code>[root@chy01 conf]</code><code># vim /etc/ansible/nginx_config/roles/new/handlers/main.yml </code>
<code>- name: restart nginx</code>
<code> </code><code>shell: </code><code>/etc/init</code><code>.d</code><code>/nginx</code> <code>reload</code>
定义需要cp的目录(核心任务)
<code>[root@chy01 conf]</code><code># vim /etc/ansible/nginx_config/roles/new/tasks/main.yml </code>
<code>- name: copy conf </code><code>file</code>
<code> </code><code>copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=</code><code>yes</code> <code>owner=root group=root mode=0644</code>
<code> </code><code>with_items:</code>
<code> </code><code>- { src: nginx.conf, dest: conf</code><code>/nginx</code><code>.conf }</code>
<code> </code><code>- { src: vhost, dest: conf/ }</code>
<code> </code><code>notify: restart nginx</code>
最后定义总入口配置
<code>[root@chy01 conf]</code><code># vim /etc/ansible/nginx_config/update.yml </code>
<code>---</code>
<code>- hosts: chy02</code>
<code> </code><code>user: root</code>
<code> </code><code>roles:</code>
<code> </code><code>- new</code>
<code>[root@chy01 conf]</code><code># ansible-playbook /etc/ansible/nginx_config/update.yml</code>
<code>PLAY [chy02] *********************************************************************************************************************</code>
<code>TASK [Gathering Facts] ***********************************************************************************************************</code>
<code>ok: [chy02]</code>
<code>TASK [new : copy conf </code><code>file</code><code>] ******************************************************************************************************</code>
<code>ok: [chy02] => (item={u</code><code>'dest'</code><code>: u</code><code>'conf/nginx.conf'</code><code>, u</code><code>'src'</code><code>: u</code><code>'nginx.conf'</code><code>})</code>
<code>ok: [chy02] => (item={u</code><code>'dest'</code><code>: u</code><code>'conf/'</code><code>, u</code><code>'src'</code><code>: u</code><code>'vhost'</code><code>})</code>
<code>PLAY RECAP ***********************************************************************************************************************</code>
<code>chy02 : ok=2 changed=0 unreachable=0 failed=0 </code>
<code>//</code><code>执行成功</code>
现在进行变更的一个测试
<code>[root@chy01 files]</code><code># tail -2 nginx.conf </code>
<code># include vhost/*.conf;</code>
<code> </code><code>}</code>
<code>(更改一下nginx的配置文件)</code>
<code>[root@chy02 ~]</code><code># tail -2 /usr/local/nginx/conf/nginx.conf</code>
<code>(在客户机上查看配置文件的include这行也是被注释的)</code>
现在进行回滚操做
<code>[root@chy01 files]</code><code># rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/</code>
<code>sending incremental </code><code>file</code> <code>list</code>
<code>files/</code>
<code>files</code><code>/nginx</code><code>.conf</code>
<code>files</code><code>/vhost/</code>
<code>files</code><code>/vhost/aaa</code><code>.conf</code>
<code>files</code><code>/vhost/ld</code><code>.conf</code>
<code>files</code><code>/vhost/proxy</code><code>.conf</code>
<code>files</code><code>/vhost/ssl</code><code>.conf</code>
<code>files</code><code>/vhost/test</code><code>.com.conf</code>
<code>handlers/</code>
<code>handlers</code><code>/main</code><code>.yml</code>
<code>tasks/</code>
<code>tasks</code><code>/main</code><code>.yml</code>
<code>vars/</code>
<code>vars</code><code>/main</code><code>.yml</code>
<code>sent 5141 bytes received 203 bytes 10688.00 bytes</code><code>/sec</code>
<code>total size is 4430 speedup is 0.83</code>
<code>回滚操作就是把旧的配置覆盖,然后重新加载nginx服务, 每次改动nginx配置文件之前先备份到old里,对应目录为</code><code>/etc/ansible/nginx_config/roles/old/files</code>
<code>[root@chy01 files]</code><code># vim /etc/ansible/nginx_config/rollback.yml //最后是定义总入口配置</code>
<code> </code><code>- old </code>
<code>需要注意一点是在操作时必须要进行将你要操作的配置文件</code><code>cp</code><code>到old下才可以进行回滚,如果没有</code><code>cp</code><code>是不能进行回滚的)</code>

本文转自我不是瘦子51CTO博客,原文链接:http://blog.51cto.com/chy940405/1980591,如需转载请自行联系原作者