ansible自动化安装nginx

ansible自动化安装nginx

  • 需求:
    使用源码包远程编译安装nginx,而且通过playbook发送到远程主机
  • 思路:
    先在一台机器上编译安装好nginx,打包,再远程发送到另一台主机上。
  • 文件结构
    ansible自动化安装nginx_第1张图片
  • 示图:
    ansible自动化安装nginx_第2张图片
  • 实现:
    一、编译安装nginx
    1)使用wget下载nginx包,下载地址:http://mirrors.sohu.com/nginx/nginx-1.9.6.tar.gz
    2)解压下载的nginx包
./configure --prefix=/usr/local/nginx
make && make install

3)编写/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="/usx/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

4)清空配置文件并重新编写

# > /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;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/1ocal/nginx/html$fastcgi_script_name;
}
}
}

5)编写完成后可以检查一下

# /usr/local/nginx/sbin/nginx -t

6)启动nginx

# service nginx start

编译安装完成

二.环境准备
1)将nginx.tar.gz复制到/etc/ansible/nginx_install/roles/install/files下
启动脚本和配置文件都放到/etc/ansible/nginx_install/roles/install/template下

# mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/

2)编写需要的yml文件

[root@ansible2 nginx_install]# cat install.yml 
---
- hosts: 192.168.2.101		//入口文件
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install
[root@ansible2 nginx_install]# cat roles/common/tasks/main.yml 
- name: install initialization require software  //安装需要的依赖
  yum: name={{ item }} state=installed
  with_items:		
    - zlib-devel
    - pcre-devel
    - gcc
[root@ansible2 nginx_install]# cat 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		//复制nginx配置文件
  template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root
mode=0644
[root@ansible2 nginx_install]# cat 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
[root@ansible2 nginx_install]# cat roles/install/tasks/main.yml 
- include: copy.yml	//调用copy.yml和install.yml
- include: install.yml
[root@ansible2 nginx_install]# cat roles/install/vars/main.yml 
nginx_user: www		//定义所需变量
nginx_port: 80
nginx_basedir: /usr/local/nginx

三、 执行文件
运行install.yml文件

# ansible-playbook /etc/ansible/nginx_install/install.yml

注:要检查远程机器存在端口占用,及时卸载。
结果如下:
ansible自动化安装nginx_第3张图片

你可能感兴趣的:(ansible自动化)