Processing math: 100%

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 "Startingprog: "
        mkdir -p /dev/shm/nginx_temp
        daemon NGINXSBINcNGINX_CONF
        RETVAL=?echoreturnRETVAL
}

stop() {
        echo -n "Stoppingprog: "
        killproc -p NGINXPIDNGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=?echoreturnRETVAL
}

reload(){
        echo -n "Reloadingprog: "
        killproc -p NGINXPIDNGINX_SBIN -HUP
        RETVAL=?echoreturnRETVAL
}

restart(){
        stop
        start
}

configtest(){
    NGINXSBINcNGINX_CONF -t
    return 0
}

case "1"instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;)echo"Usage: 0start|stop|reload|restart|configtest"RETVAL=1esacexitRETVAL

保存退出后修改启动脚本权限:

[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 'remoteaddrhttp_x_forwarded_for [timelocal]host "requesturi"status'
    '"httpreferer""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 ,如需转载请自行联系原作者






目录
打赏
0
0
0
0
347
分享
相关文章
nginx部署vue项目
本文介绍了将Vue项目部署到Nginx的步骤,包括构建Vue项目、上传dist文件夹到服务器、安装Nginx、配置Nginx代理静态文件以及重启Nginx,确保了Vue应用可以通过域名或IP地址访问。
270 1
linux安装nginx和前端部署vue项目(实际测试react项目也可以)
本文是一篇详细的教程,介绍了如何在Linux系统上安装和配置nginx,以及如何将打包好的前端项目(如Vue或React)上传和部署到服务器上,包括了常见的错误处理方法。
1562 0
linux安装nginx和前端部署vue项目(实际测试react项目也可以)
部署Django应用:使用Gunicorn和Nginx构建高效的生产环境
部署Django应用:使用Gunicorn和Nginx构建高效的生产环境
311 0
自动化运维的新篇章:使用Ansible进行服务器配置管理
【10月更文挑战第34天】在现代IT基础设施的快速迭代中,自动化运维成为提升效率、确保一致性的关键手段。本文将通过介绍Ansible工具的使用,展示如何实现高效的服务器配置管理。从基础安装到高级应用,我们将一步步揭开自动化运维的神秘面纱,让你轻松掌握这一技术,为你的运维工作带来革命性的变化。
自动化运维的利器:Ansible在配置管理中的应用
【10月更文挑战第39天】本文旨在通过深入浅出的方式,向读者展示如何利用Ansible这一强大的自动化工具来优化日常的运维工作。我们将从基础概念讲起,逐步深入到实战操作,不仅涵盖Ansible的核心功能,还会分享一些高级技巧和最佳实践。无论你是初学者还是有经验的运维人员,这篇文章都会为你提供有价值的信息,帮助你提升工作效率。
自动化运维之路:使用Ansible进行服务器管理
在现代IT基础设施中,自动化运维已成为提高效率和可靠性的关键。本文将引导您通过使用Ansible这一强大的自动化工具来简化日常的服务器管理任务。我们将一起探索如何配置Ansible、编写Playbook以及执行自动化任务,旨在为读者提供一条清晰的路径,从而步入自动化运维的世界。
自动化运维:使用Ansible实现批量服务器配置
在快速迭代的IT环境中,高效、可靠的服务器管理变得至关重要。本文将介绍如何使用Ansible这一强大的自动化工具,来简化和加速批量服务器配置过程。我们将从基础开始,逐步深入到更复杂的应用场景,确保即使是新手也能跟上节奏。文章将不包含代码示例,而是通过清晰的步骤和逻辑结构,引导读者理解自动化运维的核心概念及其在实际操作中的应用。
自动化运维:使用Ansible进行服务器配置管理
在现代IT架构中,自动化运维已成为提升效率、减少人为错误的关键。本文将介绍如何使用Ansible这一强大的自动化工具来简化和标准化服务器的配置管理过程。通过具体的代码示例和操作步骤,我们将展示如何快速部署应用、管理配置以及自动化日常任务,从而确保环境的一致性和可靠性。
自动化运维的利剑:Ansible在现代IT架构中的应用
在数字化浪潮中,企业对IT系统的敏捷性和可靠性要求日益提高。Ansible,一种简单但强大的自动化运维工具,正成为现代IT架构中不可或缺的一部分。它通过声明式编程语言YAM,简化了系统配置、应用部署和任务自动化的过程,显著提升了运维效率和准确性。本文将深入探讨Ansible的核心特性、应用场景以及如何有效整合进现有IT环境,为读者揭示其在自动化运维中的实用价值和未来发展潜力。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等