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
hosts
line, target theall
group.
---- hosts: all
- Use
vars_prompts
to prompt for the username and password.
vars_prompt:- name: userprompt: "Enter ssh user"private: no- name: passwordprompt: "Enter password for ssh user"
- Define the connection variables for the Linux host.
vars:ansible_user: "{{ user }}"ansible_password: "{{ password }}"ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
- Add the
tasks
list.
tasks:
- Install the
nginx
package.
package
is a generic OS package manager. It works on several Linux distributions to install, upgrade or remove packages.
- name: install nginxpackage:name:- nginxstate: 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.
- Create an
index.html
file.
<!DOCTYPE html><html><body><h1>become Ansible: It's like shoulder surfing a co-worker </h1> </body></html>
- Copy the index.html to the remote Linux host.
- name: copy index.htmlcopy:src: index.htmldest: /usr/share/nginx/html/index.html
- Start the
nginx
service.
- name: start nginx serviceservice:name: nginxstate: started
- Run the playbook, when prompted enter the username and password.
# Replace <Public Ip Address> with remote Linux host IP Addressansible-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
.
- Add the
become
to theinstall nginx
task.
- name: install nginxpackage:name:- nginxstate: latestbecome: 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.
- Re-run the playbook.
# Replace <Public Ip Address> with remote Linux host IP Addressansible-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.
- Add
become
to thecopy index.html
andstart nginx service
tasks.
- name: copy index.htmlcopy:src: index.htmldest: /usr/share/nginx/html/index.htmlbecome: yes- name: start nginx serviceservice:name: nginxstate: startedbecome: yes
- Re-run the playbook.
# Replace <Public Ip Address> with remote Linux host IP Addressansible-playbook configure_nginx_web_server.yml -i <Public Ip Address>,
- Verify the configuration by hitting the web server’s landing page.
# Replace <Public Ip Address> with remote Linux host IP Address# Bashcurl <Public Ip Address>
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
-
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>
- 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.
# Replace the <Public IP Address> with the# remote Linux server's IP Addressansible-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.