天天看點

ansible實戰與配置 Ⅱ

 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] =&gt; (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] =&gt; (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>

ansible實戰與配置 Ⅱ

     本文轉自我不是瘦子51CTO部落格,原文連結:http://blog.51cto.com/chy940405/1980591,如需轉載請自行聯系原作者

繼續閱讀