loading...

Ubuntu Server 18.04 – Putting it all together – Automating web server deployment

How to install and use docker on ubuntu 18.04

Speaking of automating the setup of a web server, why don’t we go ahead and do exactly that? It’ll be another simple example, but it will serve you well if we demonstrate more of what Ansible can do. We will set up a Playtutorial to perform the following tasks:

  1. Install Apache
  2. Start the apache2 service
  3. Copy an HTML file for the new site

First, let’s set up the Playtutorial to simply install Apache. I called mine apache.yml but the name is arbitrary:

---
- hosts: all
  become: true
  tasks:
  - name: Install Apache
    apt: name=apache2

No surprises here, we’ve already installed a package at this point. Let’s add an additional instruction to have the apache2 service started:

---
- hosts: all
  become: true
  tasks:
  - name: Install Apache
    apt: name=apache2
  - name: Start the apache2 services
    service: name=apache2 state=started

So far, the syntax should be self-explanatory. Ansible has a service module, which can be used to start a service on a host. In this case, we start the apache2 service. (The service would’ve already been started when apache2 was installed, but at least this way we can make sure it’s started). You do have to know the service name ahead of time, but you don’t have to pay attention to what utility needs to be used in the background to start the service. Ansible already knows how to start services on all the popular distributions, and will take care of the specifics for you.

Well, that was easy. Let’s create a simple web page for Apache to serve for us. It doesn’t need to be fancy, we just want to see that it works. Create the following content inside a file named index.html, in the same working directory of the other Ansible files:

<html>
<title>Ansible is awesome!</title>
<body>
    <p>Ansible is amazing. With just a small text file, we automated the setup of a web server!</p>
</body>
</html>

As you can see, that HTML file is fairly lame, but it will work just fine for what we need. Next, let’s add another instruction to our Apache Playtutorial:

---
- hosts: all
  become: true
  tasks:
  - name: Install Apache
    apt: name=apache2
  - name: Start the apache2 services
    service: name=apache2 state=started
  - name: Copy index.html
    copy: src=index.html dest=/var/www/html/index.html

With the copy module, we can copy a file from our local Ansible directory to the server. All we need to do is provide it a source (src) and destination (dest).

Let’s go ahead and run the new Playtutorial:

ansible-playtutorial apache.yml

Within a few minutes, you should have at least one web server configured by Ansible. In a real production environment, you would’ve only run this on servers within a specific role, but roles are beyond the scope of this chapter. But from the simple Playtutorial we created, you should be able to see the power of this amazing piece of software:

An example of a web page provisioned by Ansible

At this point in our exploration into configuration management with Ansible, we’ve installed packages, started services, and copied files. Admittedly, this isn’t much, but it’s really all you need in order to practice the basics. The documentation for Ansible will include guides on how to do all sorts of things and utilize modules to perform many different tasks.

To explore Ansible further, I recommend that you think about things you do on a regular basis that you would benefit from automating. Things like installing security updates, creating user accounts, setting passwords, and enabling services to automatically start at boot time are tasks that are easy to get started with.

In addition, you may want to consider adding a simple cron job to run under your ansible user, to run the Playtutorial every hour or so. This won’t be an issue from a resource perspective, since Ansible won’t be actually doing much unless you add a new command. In more advanced usage, you’ll want to have Ansible check out code from a repository and then apply the configuration if it has changed. This way, all you have to do is commit to a Git repository and all of your servers will download the configuration and run it during the next scheduled time. One of the things I love most about Ansible is that it’s easy to get started, but you’ll continue to find new ways to use it and benefit from it.

Comments are closed.

loading...