Ansible-playbook manual compilation and installation of nginx

Source: Internet
Author: User
Tags nginx server
Although nginx can also be installed through yum, how can I use the source code package to install and enable some nginx functional modules, and send them to the managed cluster through ansible? The following provides a specific example for the bit viewer for reference.
First, we need to manually compile an nginx server as a template, and then use roles role management to decouple it and deliver it to the managed cluster. The specific steps are as follows:
Lab environment:
Ansibleserver, nginx template: (IP: centos7)
Mysqlserver: (IP: centos7)
Webserver: (IP: centos7)
1. manually compile and install an nginx server, enable the function module, and configure the conf file as the template
-------- Install dependency package -------------------
Yum-y install PCRE-devel zlib-devel GCC gcc-C ++ // install the compiling environment

Useradd-m-S/sbin/nologin nginx // create an nginx Management User

Tar zxvf nginx-1.12.0.tar.gz-C/OPT // unzip nginx source package (version 1.12.0)

----------- Compile ----------------------
CD nginx-1.12.0/

./Configure \
-- Prefix =/usr/local/nginx \
-- User = nginx \
-- Group = nginx \

Make & make install

Ln-S/usr/local/nginx/sbin/nginx/usr/local/sbin // create a soft connection for the system to identify nginx commands
Script Note: -- with-http_stub_status_module // support status statistics

2. Create nginx boot script
VI/etc/init. d/nginx
#! /Bin/bash
# Chkconfig:-99 20
# Description: nginx Service Control script
Prog = "/usr/local/nginx/sbin/nginx"
Pidf = "/usr/local/nginx/logs/nginx. PID"
Case "$1" in
$ Prog
Kill-s quit $ (cat $ pidf)
$0 stop
$0 start
Kill-s hup $ (cat $ pidf)
Echo "Usage: $0 {START | stop | restart | reload }"
Exit 1
Exit 0
Chmod + x/etc/init. d/nginx
Chkconfig -- add nginx
Chkconfig -- level 35 nginx on
Systemctl start nginx
Systemctl status nginx

Now that the nginx template is created, edit the playbook. (For more information about the functions and common optimizations of the nginx module, see here)

CD roles/
Mkdir-P {common, install}/{handlers, files, Meta, tasks, templates, vars}
The roles directory has two roles: Common stores files for installation and compilation environment operations, and install stores files for installation and nginx operations.

Meanings of directories in common and install
Files: used to store files called by the copy module or script module.
Templates: used to store the jinjia2 template. The template module automatically searches for the jinjia2 template file in this directory.
Tasks: This Directory should contain a main. yml file that defines the task list of this role. This file can be used to include other task files located in this directory.
Handlers: This Directory should contain a main. yml file to define the actions performed when the conditions are triggered in this role.
Vars: This Directory should contain a main. yml file to define the variables used by this role.
Defaults: This Directory should contain a main. yml file to set the default variables for the current role.
Meta: This Directory should contain a main. yml file to define the special settings of this role and their dependencies.

Package the nginx directory that has been manually compiled and installed, put it under files under the install role, and put the startup script and configuration file under templates.
Tar-czvf nginx.tar.gz -- exclude "nginx. conf" -- exclude "vhost" nginx // package files other than nginx. conf and name them nginx
MV nginx.tar.gz/etc/ansible/roles/install/files/
Cp conf/nginx. CONF/etc/ansible/roles/install/templates/
CP/etc/init. d/nginx/etc/ansible/roles/install/templates/

Vim main. yml

  • Name: Install initializtion require software
    Yum: Name = "zlib-devel, PCRE-devel, GCC, GCC-C ++" state = Latest
    // Define tasks under common as nginx to download dependency packages

Vim/etc/ansible/roles/install/vars/Main. yml
Nginx_user: nginx
Nginx_port: 80
// Define vars under intall, and manage users and ports in nginx. The path is defined as a variable to facilitate future calls.

Vim/etc/ansible/roles/install/tasks/copy. yml
Name: Copy nginx Software
Copy: src=nginx.tar.gz DEST =/opt/nginx.tar.gz owner = root group = root // copy nginx.tar.gz under the filefolder to/OPT. The owner is root and the group is root.

  • Name: uncompression nginx Software
    Shell: Tar zxf/opt/nginx.tar.gz-C/usr/local // decompress the package and release it to/usr/local.
  • Name: Copy nginx start script
    Template: src = nginx DEST =/etc/init. d/nginx owner = root group = root mode = 0755 // copy the startup script from templates under install to/etc/init. d. The owner is root, the group is root, and the execution permission is defined.
  • Name: Copy nginx config
    Template: src = nginx. conf DEST = {nginx_basedir}/CONF/owner = root group = root mode = 0644 // set the configuration file nginx. copy the conf file to the/CONF/folder in the nginx directory.

Vim/etc/ansible/roles/install/tasks/copy. yml

  • Name: Create nginx user
    User: Name = {nginx_user} state = present createhome = no shell =/sbin/nologin // create a management user for nginx
  • Name: Start nginx Service
    Shell:/etc/init. d/nginx start // use the startup script to start nginx
  • Name: Add boot start nginx Service
    Shell: chkconfig -- level 35 nginx on // set to 3, 5 levels boot automatically
  • Name: delete nginx Compression Files
    Shell: Rm-RF/opt/nginx.tar.gz // Delete the previous compressed package

Vim/etc/ansible/roles/install/tasks/Main. yml

  • Include: Copy. yml
  • Include: Install. yml
    // Create a total main. yml file under the install role, and call copy. yml and install. yml

Vim/etc/ansible/install. yml

  • Hosts: All
    Remote_user: Root
    Gather_facts: True
    • Common
    • Install
      // Create the nginx installation script nginx_install.yml in the/etc/ansible/directory and call the common and install roles in roles.

So far, we have compiled and installed nginx playbook manually.
Ansible-playbook nginx_install.yml -- syntax-check // check the syntax in the script

No problem. ansible-playbook nginx_install.yml executes the script.
Then go to the two managed servers to check whether nginx is enabled successfully.
Systemctl status nginx. Service

So far nginx has been compiled and installed manually through the script.

Ansible-playbook manual compilation and installation of nginx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.