05.20 如何使用 Ansible 打補丁以及安裝應用

如何使用 Ansible 打補丁以及安裝應用

編譯自: https://opensource.com/article/18/3/ansible-patch-systems

作者: Jonathan Lozada De La Matta

使用 Ansible IT 自動化引擎節省更新的時間。

你有沒有想過,如何打補丁、重啟系統,然後繼續工作?

如果你的回答是肯定的,那就需要了解一下 Ansible[1] 了。它是一個配置管理工具,對於一些複雜的有時候需要幾個小時才能完成的系統管理任務,又或者對安全性有比較高要求的時候,使用 Ansible 能夠大大簡化工作流程。

以我作為系統管理員的經驗,打補丁是一項最有難度的工作。每次遇到 公共漏洞批露(Common Vulnearbilities and Exposure)(CVE)通知或者 信息保障漏洞預警(Information Assurance Vulnerability Alert)(IAVA)時都必須要高度關注安全漏洞,否則安全部門將會嚴肅追究自己的責任。

使用 Ansible 可以通過運行封裝模塊[2]以縮短打補丁的時間,下面以 yum 模塊[3]更新系統為例,使用 Ansible 可以執行安裝、更新、刪除、從其它地方安裝(例如持續集成/持續開發中的 rpmbuild)。以下是系統更新的任務:

- name: update the system

yum:

name: "*"

state: latest

在第一行,我們給這個任務命名,這樣可以清楚 Ansible 的工作內容。第二行表示使用 yum 模塊在CentOS虛擬機中執行更新操作。第三行 name: "*" 表示更新所有程序。最後一行 state: latest 表示更新到最新的 RPM。

系統更新結束之後,需要重新啟動並重新連接:

- name: restart system to reboot to newest kernel

shell: "sleep 5 && reboot"

async: 1

poll: 0

- name: wait for 10 seconds

pause:

seconds: 10

- name: wait for the system to reboot

wait_for_connection:

connect_timeout: 20

sleep: 5

delay: 5

timeout: 60

- name: install epel-release

yum:

name: epel-release

state: latest

shell 模塊中的命令讓系統在 5 秒休眠之後重新啟動,我們使用 sleep 來保持連接不斷開,使用 async 設定最大等待時長以避免發生超時,poll 設置為 0 表示直接執行不需要等待執行結果。暫停 10 秒鐘以等待虛擬機恢復,使用 wait_for_connection 在虛擬機恢復連接後儘快連接。隨後由 install epel-release 任務檢查 RPM 的安裝情況。你可以對這個劇本執行多次來驗證它的冪等性,唯一會顯示造成影響的是重啟操作,因為我們使用了 shell 模塊。如果不想造成實際的影響,可以在使用 shell 模塊的時候 changed_when: False。

現在我們已經知道如何對系統進行更新、重啟虛擬機、重新連接、安裝 RPM 包。下面我們通過 Ansible Lightbulb[4] 來安裝 NGINX:

- name: Ensure nginx packages are present

yum:

name: nginx, python-pip, python-devel, devel

state: present

notify: restart-nginx-service

- name: Ensure uwsgi package is present

pip:

name: uwsgi

state: present

notify: restart-nginx-service

- name: Ensure latest default.conf is present

template:

src: templates/nginx.conf.j2

dest: /etc/nginx/nginx.conf

backup: yes

notify: restart-nginx-service

- name: Ensure latest index.html is present

template:

src: templates/index.html.j2

dest: /usr/share/nginx/html/index.html

- name: Ensure nginx service is started and enabled

service:

name: nginx

state: started

enabled: yes

- name: Ensure proper response from localhost can be received

uri:

url: "http://localhost:80/"

return_content: yes

register: response

until: 'nginx_test_message in response.content'

retries: 10

delay: 1

以及用來重啟 nginx 服務的操作文件:

# 安裝 nginx 的操作文件

- name: restart-nginx-service

service:

name: nginx

state: restarted

在這個角色裡,我們使用 RPM 安裝了 nginx、python-pip、python-devel、devel,用 PIP 安裝了 uwsgi,接下來使用 template 模塊複製 nginx.conf 和 index.html 以顯示頁面,並確保服務在系統啟動時啟動。然後就可以使用 uri 模塊檢查到頁面的連接了。

這個是一個系統更新、系統重啟、安裝 RPM 包的劇本示例,後續可以繼續安裝 nginx,當然這裡可以替換成任何你想要的角色和應用程序。

- hosts: all

roles:

- centos-update

- nginx-simple

這只是關於如何更新系統、重啟以及後續工作的示例。簡單起見,我只添加了不帶變量[5]的包,當你在操作大量主機的時候,你就需要修改其中的一些設置了:

  • async & poll[6]

  • serial[7]

  • forks[8]

這是由於在生產環境中如果你想逐一更新每一臺主機的系統,你需要花相當一段時間去等待主機重啟才能夠繼續下去。


via: https://opensource.com/article/18/3/ansible-patch-systems

作者:Jonathan Lozada De La Matta[9] 譯者:HankChow 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出


分享到:


相關文章: