Configure an Nginx Web Server

Configure a Linux host as an Nginx web server with an Ansible playbook.

We'll cover the following

Ad-hoc commands are like a rubber mallet—great for certain situations but infrequently used. Playbooks, on the other hand, are like a hammer—a general tool you’ll use a majority of the time.

Not all of the automation you write with Ansible will start as an ad-hoc command. Playbooks are also a starting point.

One example is creating a web server. Creating a web server involves several tasks, with each one building upon the next. In some cases, a task has a dependency on what came before it. A playbook is an excellent place to start with this type of automation. After all, Ansible is, first and foremost, a configuration management tool.

Let’s learn how to configure a Linux host as an Nginx web server with an Ansible playbook.

  1. Create a new file and name it configure_nginx_web_server.yml.
  2. Add the hosts line, target the all group.
Press + to interact
---
- hosts: all
  1. Use vars_prompts to prompt for the username and password.
Press + to interact
vars_prompt:
- name: user
prompt: "Enter ssh user"
private: no
- name: password
prompt: "Enter password for ssh user"
  1. Define the connection variables for the Linux host.
Press + to interact
vars:
ansible_user: "{{ user }}"
ansible_password: "{{ password }}"
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
  1. Add the tasks list.
Press + to interact
tasks:
  1. Install the nginx package.

package is a generic OS package manager. It works on several Linux distributions to install, upgrade or remove packages.

Press + to interact
- name: install nginx
package:
name:
- nginx
state: latest

The parameter of the state determines how the package is installed. Using latest ensures that the package manager installs the latest version. It also upgrades the package if it detects a version difference.

  1. Create an index.html file.
Press + to interact
<!DOCTYPE html>
<html>
<body>
<h1>become Ansible: It's like shoulder surfing a co-worker </h1> </body>
</html>
  1. Copy the index.html to the remote Linux host.
Press + to interact
- name: copy index.html
copy:
src: index.html
dest: /usr/share/nginx/html/index.html
  1. Start the nginx service.
Press + to interact
- name: start nginx service
service:
name: nginx
state: started
  1. Run the playbook, when prompted enter the username and password.
Press + to interact
# Replace <Public Ip Address> with remote Linux host IP Address
ansible-playbook configure_nginx_web_server.yml -i <Public Ip Address>,

Root User
TASK [install nginx packages] This command has to be run under the root user.

The package module requires you to run it with elevated permissions. At the moment, the task is equivalent to apt-get install nginx.

  1. Add the become to the install nginx task.
Press + to interact
- name: install nginx
package:
name:
- nginx
state: latest
become: yes

Ansible has a module for elevating permissions called become. Adding become: yes to the install nginx task elevates the credentials, which equates to sudo apt-get install nginx.

To use become, the user Ansible connects with must be in the sudoers file. Password prompts must also be disabled for that user. Not disabling password prompts causes the error Missing sudo password when using become. For more details, please see this StackOverflow post.

  1. Re-run the playbook.
Press + to interact
# Replace <Public Ip Address> with remote Linux host IP Address
ansible-playbook configure_nginx_web_server.yml -i <Public Ip Address>,

Root User
TASK [Copy index.html] Destination /usr/share/nginx/html not writable.

The copy index.html task also requires elevated permissions. And so does the following task, start nginx service.

  1. Add become to the copy index.html and start nginx service tasks.
Press + to interact
- name: copy index.html
copy:
src: index.html
dest: /usr/share/nginx/html/index.html
become: yes
- name: start nginx service
service:
name: nginx
state: started
become: yes
  1. Re-run the playbook.
Press + to interact
# Replace <Public Ip Address> with remote Linux host IP Address
ansible-playbook configure_nginx_web_server.yml -i <Public Ip Address>,
  1. Verify the configuration by hitting the web server’s landing page.
# Replace <Public Ip Address> with remote Linux host IP Address
# Bash
curl <Public Ip Address>
Hit the server

Review the index.html and the final form of the configure_nginx_web_server.yml playbook created in this lesson. Execute the playbook and curl the newly configured service by clicking on the Run button.

<!DOCTYPE html>
<html>
<body>
<h1>become Ansible: It's like shoulder surfing a co-worker. </h1>
</body>
</html>

Once the environment is set up, execute the steps 14 and 15 in the Bash terminal.

Congratulations! You set up an Nginx server successfully!

Try it now

  1. Add a task that executes any shell command of your choice in the configure_nginx_web_server.yml file.

    • Find the supported Linux shell commands in the Ansible’s shell module link.
<!DOCTYPE html>
<html>
<body>
<h1>become Ansible: It's like shoulder surfing a co-worker. </h1>
</body>
</html>
Add a shell task
  1. Run the playbook with verbosity set to 3.

Click on the Run button and wait for the environment to set up. Execute the playbook in the terminal.

Press + to interact
# Replace the <Public IP Address> with the
# remote Linux server's IP Address
ansible-playbook configure_nginx_web_server.yml -i <Public Ip Address>, -vvv

In this lesson, you successfully set up an nginx server on the Linux remote host using Ansible.

Get hands-on with 1300+ tech skills courses.