ansible部署nginx1.8.0

系统环境:centos7

1、先在其中一台机器上面源码安装NGINX1.8.0

yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel -y

tar -zxvf nginx-1.8.0.tar.gz

cd nginx-1.8.0

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre

make

make install

本例当中将NGINX监听的端口设置为808

[root@cons7s nginx]# cat /usr/local/nginx/conf/nginx.conf | grep listen
        listen       808;

打包编译安装成功的NGINX

cd /usrl/local

tar -czvf nginx.tar.gz --exclude "nginx.conf" --exclude "vhost" nginx/

nginx服务的启动脚本如下:

[root@c783 conf]# cat /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac

2、ansible服务器的配置:

[root@c780 ansible]# cat /etc/ansible/hosts | grep -v '^#'
[cli1]
193.168.120.80
193.168.120.81
193.168.120.83
193.168.120.85
[cli1:vars]
ansible_ssh_user=root 
ansible_ssh_pass=yourpassword
ansible_ssh_port=22

新建相关的文件夹

cd /etc/ansible/

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

ansible服务器的目录结构:

[root@c780 ansible]# tree nginx_install/
nginx_install/
|-- install.retry
|-- install.yml
`-- roles
    |-- common
    |   |-- files
    |   |-- handlers
    |   |-- meta
    |   |-- tasks
    |   |   `-- main.yml
    |   |-- templates
    |   `-- vars
    `-- install
        |-- files
        |   `-- nginx.tar.gz
        |-- handlers
        |-- meta
        |-- tasks
        |   |-- copy.yml
        |   |-- install.yml
        |   `-- main.yml
        |-- templates
        |   |-- nginx
        |   `-- nginx.conf
        `-- vars
            `-- main.yml

15 directories, 10 files

在前面安装好NGINX的服务器上面将文件拷到ANSIBLE服务器对应的目录:

cd /usr/local/

 scp nginx.tar.gz [email protected]:/etc/ansible/nginx_install/roles/install/files/
 scp nginx/conf/nginx.conf [email protected]:/etc/ansible/nginx_install/roles/install/templates/
 scp /etc/init.d/nginx [email protected]:/etc/ansible/nginx_install/roles/install/templates/

各个yml文件内容如下:

[root@c780 ansible]# cat nginx_install/install.yml 
---
- hosts: 193.168.120.85
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install

[root@c780 ansible]# cat nginx_install/roles/common/tasks/main.yml 
- name: Install initializtion require software
  yum: name="gcc,gcc-c++,automake,pcre,pcre-devel,zlip,zlib-devel,openssl,openssl-devel" state=installed
  ignore_errors: yes

这个地方安装经常出现失败的情况,导致openssl还要重装,也可以改成使用shell模块

- name: Install initializtion require software
  shell: yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel -y
  ignore_errors: yes

[root@c780 ansible]# cat nginx_install/roles/install/tasks/main.yml 
- include: copy.yml
- include: install.yml

[root@c780 ansible]# cat 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

[root@c780 ansible]# cat nginx_install/roles/install/tasks/install.yml 
- name: Create Nginx User  
  shell: useradd -s /sbin/nologin -M nginx
  ignore_errors: yes
- name: Create dir
  shell: mkdir -p /var/log/nginx/ /var/tmp/nginx/client/ /var/run/nginx/
  ignore_errors: yes
- 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@c780 ansible]# cat nginx_install/roles/install/vars/main.yml 
nginx_user: www
nginx_port: 808
nginx_basedir: /usr/local/nginx

执行安装:

[root@c780 nginx_install]#  ansible-playbook /etc/ansible/nginx_install/install.yml

PLAY [193.168.120.85] *******************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************
ok: [193.168.120.85]

TASK [common : Install initializtion require software] **********************************************************************************
fatal: [193.168.120.85]: FAILED! => {"changed": false, "msg": "No package matching 'zlip' found available, installed or updated", "rc": 126, "results": ["gcc-4.8.5-28.el7_5.1.x86_64 providing gcc is already installed", "gcc-c++-4.8.5-28.el7_5.1.x86_64 providing gcc-c++ is already installed", "pcre-8.32-17.el7.x86_64 providing pcre is already installed", "pcre-devel-8.32-17.el7.x86_64 providing pcre-devel is already installed", "No package matching 'zlip' found available, installed or updated"]}
...ignoring

TASK [install : Copy Nginx Software] ****************************************************************************************************
changed: [193.168.120.85]

TASK [install : Uncompression Nginx Software] *******************************************************************************************
 [WARNING]: Consider using unarchive module rather than running tar
changed: [193.168.120.85]

TASK [install : Copy Nginx Start Script] ************************************************************************************************
changed: [193.168.120.85]

TASK [install : Copy Nginx Config] ******************************************************************************************************
changed: [193.168.120.85]

TASK [install : Create Nginx User] ******************************************************************************************************
changed: [193.168.120.85]

TASK [install : Create dir] *************************************************************************************************************
 [WARNING]: Consider using file module with state=directory rather than running mkdir
changed: [193.168.120.85]

TASK [install : Start Nginx Service] ****************************************************************************************************
changed: [193.168.120.85]

TASK [install : Add Boot Start Nginx Service] *******************************************************************************************
changed: [193.168.120.85]

TASK [install : Delete Nginx compression files] *****************************************************************************************
 [WARNING]: Consider using file module with state=absent rather than running rm
changed: [193.168.120.85]

PLAY RECAP ******************************************************************************************************************************
193.168.120.85             : ok=11   changed=9    unreachable=0    failed=0   
 

猜你喜欢

转载自blog.csdn.net/lsysafe/article/details/82229448
今日推荐