自动化运维Ansible 一键安装nginx环境(实战)

时间:2024-03-24 19:40:31

1、准备4台centost7

IP:192.168.1.71        主控端        

IP:192.168.1.72  被控端       web01

IP:192.168.1.73  被控端       web02

IP:192.168.1.74             被控端       lb01

2、目前被控端很干净的环境(最小化安装的centos)

自动化运维Ansible 一键安装nginx环境(实战)

目录结构

自动化运维Ansible 一键安装nginx环境(实战)

3、准备安装包

安装包nginx官网下载http://nginx.org/

自动化运维Ansible 一键安装nginx环境(实战)

4、脚本编写

[[email protected] roles]# cat site.yml

- hosts: all

  roles:

    - base

- hosts: webservers

  roles:

    - nginx

  tags: nginx

[[email protected] roles]# cat nginx/tasks/main.yml

---

- name: Istalled Packages

  yum: name={{ packages }} state=present

  vars:

    packages:

      - pcre

      - pcre-devel

      - openssl

      - openssl-devel

      - gcc

- name: Add Copy Nginx  Packages

  copy: src=/etc/ansible/roles/nginx/templates/nginx-1.14.2.tar.gz dest=/software/src

- name: Tar Nginx

  shell: cd /software/src;tar -xvf nginx-1.14.2.tar.gz

- name: Add Nginx_module Server

  copy: src=/etc/ansible/roles/nginx/templates/nginx-module-vts.tar.gz dest=/software/src

- name: Tar Nginx_module

  shell: cd /software/src;tar -xvf nginx-module-vts.tar.gz -C /software/

- name: Install Nginx

  shell: cd /software/src/nginx-1.14.2;./configure --user=www --group=www --with-http_ssl_module --add-module=/software/nginx-module-vts --with-http_stub_status_module --prefix=/software/nginx-1.14.2/;make&& make install

- name: Create Soft Link

  file: src={{ "/software/nginx-1.14.2" }} dest={{ "/software/nginx" }} state=link owner=www group=www

- name: Create Configure Directory

  file:

    path: /software/nginx/conf/vhosts/

    state: directory

    mode: '0755'

- name: Configure Nginx Server

  template: src=/etc/ansible/roles/nginx/templates/nginx.conf.j2 dest=/software/nginx/conf/nginx.conf

  notify: Restart Nginx Server

- name: Check Nginx Configure

  shell: cp /software/nginx/sbin/nginx /bin/

- name: Nginx -T

  shell: nginx -t

  register: check_nginx

  changed_when:

    - check_nginx.stdout.find( 'successful' )

    - false

- name: Nginx Starting Up

  copy: src=/etc/ansible/roles/nginx/templates/nginx dest=/etc/init.d/

- name: Chmod Nginx

  file:

    path: /etc/init.d/nginx

    owner: www

    group: www

    mode: 755

- name: Add boot Bootstrap

  shell: cd /etc/init.d/;chkconfig nginx on

 

- name: Start Nginx Server

  systemd: name=nginx state=started enabled=yes

开机自启脚本

[[email protected] roles]# cat nginx/templates/nginx

#! /bin/bash

# chkconfig: - 85 15

PATH=/software/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

exit 0

5、主控端输出结果:

自动化运维Ansible 一键安装nginx环境(实战)

6、被控端输出结果:

自动化运维Ansible 一键安装nginx环境(实战)

被控端nginx

自动化运维Ansible 一键安装nginx环境(实战)

大家可以把编译包做成yum源直接安装!