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




      本文转自 M四月天 51CTO博客,原文链接:http://blog.51cto.com/msiyuetian/1753146 ,如需转载请自行联系原作者






相关文章
|
2月前
|
Kubernetes 应用服务中间件 nginx
百度搜索:蓝易云【使用Kubernetes部署Nginx应用教程】
现在,你已经成功在Kubernetes集群上部署了Nginx应用。通过访问Service的外部IP地址,你可以访问Nginx服务。
42 4
|
22天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
91 0
|
7天前
|
应用服务中间件 Linux 开发工具
如何在阿里云服务器快速搭建部署Nginx环境
以下是内容的摘要: 本文档主要介绍了在阿里云上购买和配置服务器的步骤,包括注册阿里云账号、实名认证、选择和购买云服务器、配置安全组、使用Xshell和Xftp进行远程连接和文件传输,以及安装和配置Nginx服务器的过程。在完成这些步骤后,你将能够在服务器上部署和运行自己的网站或应用。
|
22天前
|
应用服务中间件 nginx Windows
windows下Nginx+RTMP部署
windows下Nginx+RTMP部署
19 0
|
1月前
|
应用服务中间件 Linux nginx
web后端-linux-nginx-1.18操作命令和部署
web后端-linux-nginx-1.18操作命令和部署
|
6月前
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
|
8月前
|
网络协议 网络安全
Ansible模块介绍——防火墙模块
Ansible模块介绍——防火墙模块
142 0
|
6月前
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
|
5月前
|
网络安全 数据安全/隐私保护
ansible的get_url模块
ansible的get_url模块
|
5月前
|
存储 Linux Python
ansible手动添加模块
ansible手动添加模块
47 0