ansible playbook實戰——下發部署nginx以及更新、回滾

之前介紹了 ansible 的安裝配置及實例:http://msiyuetian.blog.51cto.com/8637744/1748143

以及 ansible 的 playbook 詳解:http://msiyuetian.blog.51cto.com/8637744/1752326

下面這篇文章主要是通過 ansible 下發部署安裝 nginx 以及後期發佈更新配置,還有回滾機制來認識 ansible 的 playbook。


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


一、安裝nginx


首先我們需要在安裝了 ansible 的機器上編譯安裝好nginx,詳細步驟如下:

1、下載解壓

[root@master ~]# cd /usr/local/src/

[root@master src]# wget http://nginx.org/download/nginx-1.4.4.tar.gz

[root@master src]# tar -zxvf nginx-1.4.4.tar.gz

2、安裝依賴包

[root@master src]# yum install -y gcc zlib-devel openssl openssl-devel pcre-devel

3、配置編譯參數

[root@master src]# cd nginx-1.4.4

[root@master nginx-1.4.4]# ./configure \

--prefix=/usr/local/nginx \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_stub_status_module  \
--with-pcre
3、編譯安裝nginx

[root@master nginx-1.4.4]# make

[root@master nginx-1.4.4]# make install

4、編寫啓動腳本

[root@master nginx-1.4.4]# vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
        stop
        start
}

configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL

保存退出後修改啓動腳本權限:

[root@master nginx-1.4.4]# chmod 755 /etc/init.d/nginx


5、更改配置文件

[root@master nginx-1.4.4]# > /usr/local/nginx/conf/nginx.conf                //清空原有配置

[root@master nginx-1.4.4]# vim /usr/local/nginx/conf/nginx.conf

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
    include vhosts/*.conf;

}

保存退出後檢查配置文件是否有錯:

[root@master nginx-1.4.4]# /usr/local/nginx/sbin/nginx -t

新建虛擬主機目錄(後面用於測試):

[root@master nginx-1.4.4]# mkdir /usr/local/nginx/conf/vhosts


6、啓動服務

[root@master nginx-1.4.4]# chkconfig --add nginx

[root@master nginx-1.4.4]# chkconfig nginx on

[root@master nginx-1.4.4]# service nginx start



二、下發nginx


1、新建所需目錄

[root@master ~]# cd /etc/ansible/

[root@master ansible]# mkdir -p nginx_install/roles

[root@master ansible]# cd nginx_install/roles/

[root@master roles]# mkdir common install

[root@master roles]# mkdir common/tasks

[root@master roles]# mkdir install/{files,tasks,templates,vars}

說明:官方建議創建以下目錄(我這裏簡單化了,不需要的就沒有創建):

# mkdir -p nginx_install/roles/{common,delete,install}/{handlers,files,meta,tasks,templates,vars}

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


2、打包nginx並拷貝文件

[root@master roles]# cd /usr/local/

[root@master local]# tar czvf nginx.tar.gz nginx

[root@master local]# cp nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/

[root@master local]# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/

說明:把安裝文件放於 install/files/ 目錄下,把啓動腳本放於install/templates/ 目錄下。


3、定義common的tasks

[root@master local]# cd /etc/ansible/nginx_install/roles/

[root@master roles]# vim common/tasks/main.yml              //定義nginx需要安裝的一些依賴包

---

- name: Install initializtion require software

  yum: name=` item ` state=installed

  with_items:

    - gcc

    - zlib-devel

    - pcre-devel

    - openssl-devel

4、定義install的vars

[root@master roles]# vim install/vars/main.yml                           //定義變量

nginx_user: nobody

nginx_basedir: /usr/local/nginx

說明:這裏的 nginx_user 要與 nginx.conf 配置文件中定義的用戶一致。變量還可以定義一些其他的,如下:

nginx_port: 80

nginx_web_dir: /data/www

nginx_version: 1.4.4


5、定義install的tasks

[root@master roles]# vim 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

說明:這裏是拷貝文件到遠程機器/tmp/目錄下,然後解壓。其中的 copy: src 相對於 install/files/ 目錄下,template: src 相對於 install/templates/ 目錄下。


[root@master roles]# vim install/tasks/install.yml

- name: Create Nginx User

  user: name=` nginx_user ` state=present createhome=no shell=/sbin/nologin

- name: Start Nginx Service

  service: name=nginx state=started

- name: Add Boot Start Nginx Service

  shell: chkconfig --level 345 nginx on

- name: Delete Nginx compression files

  shell: rm -rf /tmp/nginx.tar.gz

說明:這裏會對遠程機器建立用戶,啓動服務,刪除壓縮包等操作。不過我們還可以定義nginx_web_dir目錄,存放虛擬主機文件


[root@master roles]# vim install/tasks/main.yml

- include: copy.yml

- include: install.yml

說明:這裏創建的是調用 copy.yml 和 install.yml 的文件。


6、定義總入口文件

[root@master roles]# cd /etc/ansible/nginx_install/

[root@master nginx_install]# vim install.yml

---

- hosts: testhost

  remote_user: root

  gather_facts: True

  roles:

    - common

    - install


7、執行下發

先修改下 hosts 文件,因爲之前實驗把本機也添加到了 [testhost] 組裏面去了,這裏只保留一個遠程機:

[root@master nginx_install]# vim /etc/ansible/hosts

[testhost]

192.168.0.114

[root@master nginx_install]# ansible-playbook install.yml

wKioL1buSEeyfqerAAB0_BiMgAk528.png


8、在遠程機上測試結果

[root@slaver ~]# rpm -qa |egrep 'gcc|zlib|pcre|openssl'

[root@slaver ~]# ls /usr/local/nginx/

[root@slaver ~]# ps aux |grep nginx

[root@slaver ~]# chkconfig --list nginx

wKiom1btaR2QmOdtAABzHUTOS0E301.png

由上圖可知下發已經成功。



三、更新nginx


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

1、新建及拷貝文件

[root@master ~]# cd /etc/ansible/

[root@master ansible]# mkdir -p nginx_config/roles

[root@master ansible]# cd nginx_config/roles/

[root@master roles]# mkdir -p new/{vars,files,tasks,handlers}

[root@master roles]# cp /usr/local/nginx/conf/nginx.conf new/files

[root@master roles]# cp -r /usr/local/nginx/conf/vhosts new/files

說明:其中 new 爲更新時用到的,後面會新建old 爲回滾時用到的,new/files 下面爲 nginx.conf文件 和 vhosts 目錄,handlers 爲重啓 nginx 服務所需目錄。

2、定義變量

[root@master roles]# vim new/vars/main.yml

nginx_basedir: /usr/local/nginx

3、定義重新加載服務

[root@master roles]# vim new/handlers/main.yml

- name: restart nginx

  shell: /etc/init.d/nginx reload

4、定義tasks核心任務

[root@master roles]# vim 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: vhosts, dest: conf/ }

  notify: restart nginx

注意:這裏的copy是相當於數組。notify定義了需要調用handlers/main.yml下的重啓nginx。

5、定義總入口配置

[root@master roles]# cd ..

[root@master nginx_config]# vim update.yml

---

- hosts: testhost

  user: root

  roles:

  - new

6、測試更新

1)新建一個虛擬主機配置文件

[root@master nginx_config]# vim roles/new/files/vhosts/1.conf

#msiyuetian.blog.51cto.com

2)發佈更新

[root@master nginx_config]# ansible-playbook update.yml       //右下可知更新成功

wKiom1buUdXSmO2TAABF19KLeU0893.png

3)遠程機器查看已經同步成功:

wKioL1buUmjw5YpCAAAReYx0POI366.png



四、回滾nginx


關於回滾,需要在執行 playbook 之前先備份一下舊的配置,所以對於老配置文件的管理一定要嚴格,千萬不能隨便去修改線上機器的配置,並且要保證 new/files 目錄下面的配置和線上的配置一致。

1、備份

[root@master nginx_config]# mkdir roles/old

[root@master nginx_config]# rsync -av roles/new/ roles/old/

注意:拷貝整個new/目錄即實現備份,這裏用rsync而不用cp,是因爲rsync會直接覆蓋相同的文件。若沒有rsync命令,直接yum install -y rsync安裝即可。

2、定義回滾入口

[root@master nginx_config]# vim backup.yml

---

- hosts: testhost

  user: root

  roles:

  - old

3、更新

1)修改虛擬主機文件

[root@master nginx_config]# vim roles/new/files/vhosts/1.conf              //添加三行

#msiyuetian.blog.51cto.com

#msiyuetian.blog.51cto.com

#msiyuetian.blog.51cto.com

#msiyuetian.blog.51cto.com

2)下發更新

[root@master nginx_config]# ansible-playbook update.yml        //發佈更新

3)遠程機器查看是否更新

wKiom1buWlXwd_7SAAAjGrHxCEI009.png

由上圖可知已經更新成功

4、回滾

1)執行回滾

[root@master nginx_config]# ansible-playbook backup.yml

wKioL1buW2nTQELJAABFhINDlCQ794.png

2)遠程機器查看效果

wKioL1buW53gOZeRAAArTc4wgj0350.png

由上圖可知已經回滾成功。



樣例庫:https://github.com/dl528888/ansible-examples

我們可以下載整個樣例庫

# git clone git://https://github.com/dl528888/ansible-examples.git



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