Install NGINX and NGINX Plus using Ansible

Source: Internet
Author: User
Tags centos server ansible playbook

Install NGINX and NGINX Plus using Ansible

In the production environment, I prefer to do everything related to automation. If the computer can complete your task, why do you need to do it yourself? However, in an ever-changing environment with multiple technologies, creating and implementing automation is an arduous task. That's why I like Ansible. Ansible is an open-source tool for IT configuration management, deployment, and business processes. IT is very convenient to use.

One of my favorite features of Ansible is that it has no clients at all. To manage a system and establish a connection through SSH, it uses Paramiko (a Python Library) or local OpenSSH. Another attraction of Ansible is that it has many scalable modules. These modules can be used by system administrators to execute common tasks. In particular, they use the powerful Ansible tool to install and configure any program across multiple servers, environments, or operating systems, with only one control node required.

In this tutorial, I will take you through Ansible to complete installation and deployment of Open Source NGINX and our commercial product NGINX Plus. I will demonstrate it on the CentOS server, but the following section "create Ansible Playbook on Ubuntu to install NGINX and NGINX Plus" contains the deployment details on the Ubuntu server.

Install and use the automation tool Ansible in CentOS 7

Batch manage remote servers using Ansible

Functions and usage of Ansible and Docker

Ansible batch build LAMP Environment

Ansible: a configuration management and IT automation tool

In this tutorial, I will use Ansible 1.9.2 and deploy and run it on the CentOS 7.1 server.

  1. $ ansible --version
  2. ansible 1.9.2
  3. $ cat/etc/RedHat-release
  4. CentOSLinux release 7.1.1503(Core)

If you do not have Ansible, you can view the instructions on the Ansible website and install it.

If you are using CentOS, installing Ansible is very simple. Just enter the following command. If you want to use the source code to compile and install or use other releases, see the description in the above Ansible link.

  1. $ sudoyum install -y epel-release &&sudoyum install -y ansible

Depending on the environment, some commands in this tutorial may require sudo permissions. The file path, user name, and target server depend on your environment.

Create an Ansible Playbook to install NGINX (CentOS)

First, create a working directory for NGINX deployment, including subdirectories and deployment configuration files. I usually recommend that you create this directory in your main directory, which is described in all examples in the article.

  1. $ cd $HOME
  2. $ mkdir-p ansible-nginx/tasks/
  3. $ touch ansible-nginx/deploy.yml
  4. $ touch ansible-nginx/tasks/install_nginx.yml

The directory structure looks like this. You can use the tree Command to view details.

  1. $ tree $HOME/ansible-nginx/
  2. /home/kjones/ansible-nginx/
  3. ├── deploy.yml
  4. └── tasks
  5. └── install_nginx.yml
  6. 1 directory,2 files

If you have not installed the tree Command, run the following command to install it.

  1. $ sudoyum install -y tree
Create a master deployment File

Next, open deploy. yml in the text editor. I like to use vim on the command line to edit the configuration file. It will also be used throughout the tutorial.

  1. $ vim $HOME/ansible-nginx/deploy.yml

The deploy. yml file is the main file for Ansible deployment. In the "deploy NGINX using Ansible" section, we will use this file when running the ansible prepare playbook command. In this file, we specify the library used for Ansible running and other configuration files.

In this example, I use the include module to specify the configuration file to install NGINX step by step. Although you can create a very large playbook file, I suggest you split it into small files to make them more organized. In the include example, you can copy static content, copy the configuration file, and set variables for more advanced deployment using logical configuration.

Enter the following lines in the file. My comment on the top contains the file name for reference.

  1. #./ansible-nginx/deploy.yml
  2. - hosts: nginx
  3. tasks:
  4. -include:'tasks/install_nginx.yml'

The hosts statement indicates that Ansible deploys all servers in the nginx group, and the server is specified in/etc/ansible/hosts. We will edit this file in the "Create NGINX Server LIST" section below.

The include statement indicates that Ansible reads and executes the content in the install_nginx.yml file from the tasks directory during deployment. This file includes the following steps: Download, install, and start NGINX. We will create this file in the next section.

Create deployment files for NGINX

Now, save the deploy. yml file and open install_nginx.yml in the editor.

  1. $ vim $HOME/ansible-nginx/tasks/install_nginx.yml

This package contains commands (written in YAML format). Ansible installs and configures our NGINX deployment process according to the instructions. Each section (steps in the process) starts withnameStatement (followed by a hyphen ).nameThe following string is output to the standard output during Ansible deployment. You can modify it as needed. The next line of the Section in the YAML file is the module to be used during deployment. In the following configurationyumAndserviceModule.yumThe module is used to install software packages on CentOS.serviceThe module is used to manage UNIX services. Specify the parameters of several modules in the last line or lines of this section (in this examplenameAndstate).

Enter the following lines in the file. Just like deploy. yml, the first line in our file is a comment on the file name for reference. The first section tells Ansible to install the. rpm file from the NGINX repository on CentOS 7. This allows the Software Package Manager to directly install the latest and most stable versions from the NGINX repository. Modify the path based on your CentOS version. The list of all available packages can be found on the open-source NGINX website. The following two sections tell Ansible how to useyumThe module installs the latest NGINX version, and then usesserviceModule to start NGINX.

Note: In the first section, the path name in the CentOS package may be displayed as two consecutive rows in width. Enter the full path on a row.

  1. #./ansible-nginx/tasks/install_nginx.yml
  2. - name: NGINX |Installing NGINX repo rpm
  3. yum:
  4. name: http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  5. - name: NGINX |Installing NGINX
  6. yum:
  7. name: nginx
  8. state: latest
  9. - name: NGINX |Starting NGINX
  10. service:
  11. name: nginx
  12. state: started
Create an NGINX Server LIST

Now, we have set all the configuration files for Ansible deployment, and we need to tell Ansible which server to deploy. We need to specify the hosts file in Ansible. Back up existing files and create a new file for deployment.

  1. $ sudomv/etc/ansible/hosts /etc/ansible/hosts.backup
  2. $ sudovim/etc/ansible/hosts

Enter (or edit) the following lines in the file to create a group named nginx and list the servers that install NGINX. You can specify the server through the host name, IP address, or in a range, such as server [1-3] .domain.com. Here, I use an IP address to specify a server.

  1. # /etc/ansible/hosts
  2. [nginx]
  3. 172.16.239.140
Set security

Close to completion, but before deployment, we need to ensure that Ansible has been authorized to access our target server through SSH.

The preferred and safest method is to add the rsa ssh key of the Ansible server to the authorized_keys file of the target server. This gives Ansible unlimited SSH permissions on the target server. To learn more

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: info-contact@alibabacloud.com 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.