七、用playbook安裝Nginx、playbook管理配置文件

一、用playbook安裝Nginx

思路:先在一臺機器上編譯安裝好nginx、打包,然後再用ansible去下發

# cd /etc/ansible   進入ansible配置文件目錄
# mkdir  nginx_install   創建一個nginx_install的目錄,方便管理
# cd nginx_install
# mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}

說明:roles目錄下有兩個角色,common爲一些準備操作,install爲安裝nginx的操作。每個角色下面又有幾個目錄,handlers下面是當發生改變時要執行的操作,通常用在配置文件發生改變,重啓服務。files爲安裝時用到的一些文件(拷貝到對方機器的文件),meta爲說明信息,說明角色依賴等信息(可以留空),tasks裏面是核心的配置文件,templates通常存一些配置文件,啓動腳本等模板文件,vars下爲定義的變量。


需要事先準備好安裝用到的文件,具體如下:

1、在一臺機器上事先編譯安裝好nginx,配置好啓動腳本,配置好配置文件。

2、安裝好後,我們需要把nginx目錄打包,並放到/etc/ansible/nginx_install/roles/install/files/下面,名字爲nginx.tar.gz

3、啓動腳本、配置文件都要放到/etc/ansible/nginx_install/roles/install/templates下面

nginx、啓動腳本,nginx配置文件。

# ls /usr/local/nginx/
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@fuxi01 nginx_install]# ls /etc/init.d/nginx 
/etc/init.d/nginx
[root@fuxi01 nginx_install]# ls /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
編譯好的nginx,啓動腳本,配置文件。
然後打包:
# cd /usr/local/
[root@fuxi01 local]# tar czvf nginx.tar.gz --exclude "nginx.conf" --exclude "vhost" nginx/    //過濾掉nginx.conf和vhost目錄不要。
[root@fuxi01 local]# mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/   //移動到該目錄下
[root@fuxi01 local]# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@fuxi01 local]# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/


定義common的tasks,nginx是需要一些依賴包的,用到了yum模塊,安裝zlib和pcre包。

# cd  /etc/ansible/nginx_install/roles
# vim  ./common/tasks/main.yml //內容如下
- name: Install initializtion require software
  yum: name={{ item }} state=installed
  with_items:
    - zlib-devel
    - pcre-devel

定義變量

爲什麼要把它放到模板裏?

因爲模板裏支持變量的。

在部署的時候,可以根據機器環境的不同,定義不同的值。比如第一臺機器定義的www,第二臺機器定義的是Apache。左邊是變量名,右邊是變量值(www)。

# vim /etc/ansible/nginx_install/roles/install/vars/main.yml //內容如下
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx

首先要把所有用到的文檔拷貝到目標機器

# vim /etc/ansible/nginx_install/roles/install/tasks/copy.yml //內容如下
- name: Copy Nginx Software
  copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
- name: Uncompression Nginx Software
  shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
- name: Copy Nginx Start Script
  template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
- name: Copy Nginx Config
  template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644

這裏的nginx.tar.gz沒有寫絕對路徑,因爲copy模塊默認就會在install/files目錄下去找,template模塊會到templates下面去找。


接下來會建立用戶,啓動服務,刪除壓縮包。(這就是一個總的yml。)

# vim   /etc/ansible/nginx_install/roles/install/tasks/install.yml  //內容如下
- name: Create Nginx User
  user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
- name: Start Nginx Service
  shell: /etc/init.d/nginx start
- name: Add Boot Start Nginx Service
  shell: chkconfig --level 345 nginx on
- name: Delete Nginx compression files
  shell: rm -rf /tmp/nginx.tar.gz

說明:

模塊user,變量nginx_user,在install/vars/main.yml中定義的,present已存在的,createhome是否創建用戶的家目錄。

創建用戶,啓動nginx服務,開機啓動nginx服務,刪除nginx的壓縮包。


再創建main.yml並且把copy和install調用

# vim /etc/ansible/nginx_install/roles/install/tasks/main.yml //內容如下
- include: copy.yml
- include: install.yml

到此兩個roles:common和install就定義完成了,接下來要定義一個入口配置文件

# vim /etc/ansible/nginx_install/install.yml  //內容如下
---
- hosts: testhost
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install

執行: ansible-playbook /etc/ansible/nginx_install/install.yml

執行這一步以前,一定要保證對方機器上是沒有安裝nginx的,執行成功後,到對方機器上查看nginx進程是這樣的才爲正確:

[root@yw03 ~]# ps aux |grep nginx
root       1127  0.0  0.0  45912  1284 ?        Ss   11月21   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     1128  0.0  0.2  48404  4168 ?        S    11月21   0:43 nginx: worker process
nobody     1129  0.0  0.2  48404  3912 ?        S    11月21   0:00 nginx: worker process
root       8061  0.0  0.0 112732   972 pts/0    S+   22:02   0:00 grep --color=auto nginx

思路圖1:

1.png

思路圖2:

2.png



二、playbook管理配置文件

生產環境中大多時候是需要管理配置文件的,安裝軟件包只是在初始化環境的時候用一下。下面寫一個管理nginx配置文件的playbook。

# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}

其中new爲更新時用到的,old爲回滾時用到的,files下面爲nginx.conf和vhosts目錄,handlers爲重啓nginx服務的命令。

關於回滾,就是改錯了配置文件需要回滾到原來的配置文件,然後把對應的服務進行加載。需要在執行playbook之前先備份一下舊的配置,所以對於老配置文件的管理一定要嚴格,千萬不能隨便去修改線上機器的配置,並且要保證new/files下面的配置和線上的配置一致。

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

# cd /usr/local/nginx/conf/
# cp -r nginx.conf vhost  /etc/ansible/nginx_config/roles/new/files/

# vim /etc/ansible/nginx_config/roles/new/vars/main.yml   //定義變量
nginx_basedir: /usr/local/nginx

# vim /etc/ansible/nginx_config/roles/new/handlers/main.yml  //定義重新加載nginx服務
- name: restart nginx
  shell: /etc/init.d/nginx reload

# 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
  說明:
  這裏的循環對象有兩個子對象。一個是src,一個是dest,item.src的只循環items裏的src,item.dest的只循環items裏的dest。
  將nginx.conf拷貝到/usr/local/nginx/conf/nginx.conf。
  notify,這裏就是handlers,用的是restart nginx    在該文件內:/etc/ansible/nginx_config/roles/new/handlers/main.yml

# vim /etc/ansible/nginx_config/update.yml // 最後是定義總入口配置
---
- hosts: testhost
  user: root
  roles:
  - new

執行: ansible-playbook /etc/ansible/nginx_config/update.yml

當變更了某個文件內容時,再次執行此命令,只會對那一個變更的文件進行更新。

而回滾的backup.yml對應的roles爲old

# rsync -av  /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/

回滾操作就是把舊的配置覆蓋,然後重新加載nginx服務, 每次改動nginx配置文件之前先備份到old裏,對應目錄爲/etc/ansible/nginx_config/roles/old/files

# vim /etc/ansible/nginx_config/rollback.yml // 最後是定義總入口配置
---
- hosts: testhost
  user: root
  roles:
  - old

順序爲:更改配置文件內容前,先rsync備份到old下,然後再對/new/files下的配置文件進行更改,更改後執行ansible-playbook update.yml,如果沒問題,那就OK,如果有問題,再執行ansible-playbook rollback.yml回滾到前面的配置。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章