Level 1
0 / 100 XP

Creating a Web Server Role

In this lesson, you will get some more practice creating Ansible Role's by creating a role to set up a basic web server. This role, named webserver, will be responsible for installing the Nginx package and deploying a simple HTML file to the server's web directory.

__

We're going to move a little faster in this lesson because you should already have created a basic role before. Repetition is how you learn, and that's the goal of this lesson (as well as making a cool web server).

Creating the Role Directory Structure

Assuming you have already created a roles directory from previous lessons, let's create a new role named webserver. Open your terminal and run the following command to set up our directories:

mkdir -p ~/roles/webserver/{tasks,handlers,templates,files,vars,defaults,meta}

Defining Tasks for the Web Server Role

Next, we're going to use the apt module to install a package called nginx, which is a lightweight and easy-to-use web server.

Review help docs before getting started

From here, you know the drill, use ansible-doc to review the documenation for:

  • The apt module: See how install packages by specifying the name (nginx) and setting the state (present).
  • The copy module: See how to copy a file from our role directory to our target server. We want to set (src) and (dest).

After you've done that, let's define the tasks for the webserver role. Create a main.yml file in the tasks directory:

nano roles/webserver/tasks/main.yml

Add the following tasks to the main.yml file:

Text
--- # tasks file for webserver - name: Install Nginx apt: name: nginx state: present become: yes

This will install the nginx package on our server. The next task in the playbook should be to copy our index.html file, which doesn't exist yet, but we will create it after we update this playbook. Add the…