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.
- Create a new file and name it
configure_nginx_web_server.yml. - Add the
hostsline, target theallgroup.
- Use
vars_promptsto prompt for the username and password.
- Define the connection variables for the Linux host.
- Add the
taskslist.
- Install the
nginxpackage.
package is a generic OS package manager. It works on several Linux distributions to install, upgrade or remove packages.
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.
- Create an
index.htmlfile.
- Copy the index.html to the remote Linux host.
- Start the
nginxservice.
- Run the playbook, when prompted enter the username and password.
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.
- Add the
becometo theinstall nginxtask.
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.
- Re-run the playbook.
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.
- Add
becometo thecopy index.htmlandstart nginx servicetasks.
- Re-run the playbook.
- Verify the configuration by hitting the web server’s landing page.
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.
/
- configure_nginx_web_server.yml
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#
-
Add a task that executes any shell command of your choice in the
configure_nginx_web_server.ymlfile.- Find the supported Linux shell commands in the Ansible’s shell module link.
/
- configure_nginx_web_server.yml
- 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.
In this lesson, you successfully set up an nginx server on the Linux remote host using Ansible.
