0%

0/1 Lessons

Course Introduction

• 16min

0 / 3 lessons complete

IT Lab Setup

• 20min

0 / 3 lessons complete

Installing Ansible

• 35min

0 / 3 lessons complete

Managing your Ansible Inventory

• 2hr 7min

0 / 8 lessons complete

Ansible Basics

• 1hr 43min

0 / 9 lessons complete

Ansible Roles

• 1hr 46min

0 / 8 lessons complete

Ansible Galaxy

• 1hr 33min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 1hr 51min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 1hr 41min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

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:

  • 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:

---
# 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 following to the playbook after the task above:

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

Create the HTML File

Now we want to create 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>
<html>
<head>
    <title>Welcome to Our Web Server</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a basic web page served by Nginx that was placed by Ansible.</p>
</body>
</html>

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
  roles:
    - common

- hosts: webservers
  become: yes
  roles:
    - 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!

Conclusion

You've now created a functional Ansible role for setting up a basic Nginx web server and deploying a simple HTML file. This role demonstrates the power of Ansible in automating web server setup and configuration. As you progress, you can enhance this role with more complex configurations, such as setting up virtual hosts, SSL, or even deploying dynamic content.

Great job on completing this lesson! You're on your way to mastering the automation of server deployments using Ansible. See you in the next lesson!

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