0/1 Lessons

Course Introduction

• 19min

0 / 2 lessons complete

IT Lab Setup

• 31min

0 / 3 lessons complete

Installing Ansible

• 57min

0 / 4 lessons complete

Managing your Ansible Inventory

• 2hr 45min

0 / 8 lessons complete

Ansible Basics

• 2hr 54min

0 / 9 lessons complete

Ansible Roles

• 2hr 38min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 28min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 20min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 23min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Creating a Web Server Role


Q&A (0)

Notes (0)

Resources (0)

Saving Progress...


There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

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:

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:

# tasks file for webserver
- name: Install Nginx
    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 following to the playbook after the task above:

- name: Copy index.html to web directory
    src: index.html
    dest: /var/www/html/index.html
  become: yes

Create the HTML File

Now we want to create the HTML a simple HTML file to be deployed by the role. Let's create an HTML web page in the files directory called index.html:

nano ~/roles/webserver/files/index.html

Add some basic HTML content to this file:

<!DOCTYPE html>
    <title>Welcome to Our Web Server</title>
    <h1>Hello, World!</h1>
    <p>This is a basic web page served by Nginx that was placed by Ansible.</p>

Add The Role to a New Play

Let's update our existing playbook by adding a new play that targets our inventory group webservers. Open the playbook in your preferred editor:

nano ~/first_playbook.yml

Add a new play to our first_playbook.yml file. Configure the hosts to include the inventory group webservers, and set the roles to include the webserver role

- hosts: all
  become: yes
    - common

- hosts: webservers
  become: yes
    - webserver

This playbook will apply the webserver role to all specified hosts.

Running the Playbook

Run the playbook to set up the web server across your managed nodes:

ansible-playbook first_playbook.yml

Executing this command will install Nginx and deploy your basic HTML file to the web directory of the target machines.

Ansible Running Multiple Plays, Tasks and Roles
Ansible Running Multiple Plays, Tasks and Roles

Things are starting to get cool now! If I open my web browser and navigate to the IP address of my managed-node-1 server, I see this:

Ansible Created a Web Server!
Ansible Created a Web Server!

Very cool stuff! You could actually lose all the data on our servers so far, and after re-installing the OS, we would just need to setup our SSH keys, configure the static IP and we could get our server fully configured as it is now with a single run of ansible-playbook!

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Notify of
profile avatar
Inline Feedbacks
View all comments