自動化運維Ansible 一鍵安裝nginx環境(實戰)

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)

目錄結構

3、準備安裝包

安裝包nginx官網下載http://nginx.org/

4、腳本編寫

[root@wld-01-71 roles]# cat site.yml

- hosts: all

  roles:

    - base

- hosts: webservers

  roles:

    - nginx

  tags: nginx

[root@wld-01-71 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

開機自啓腳本

[root@wld-01-71 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、主控端輸出結果:

6、被控端輸出結果:

被控端nginx

大家可以把編譯包做成yum源直接安裝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章