Package an Application

In this lab you’ll use Ansible and Terraform to package the work you did in the Installing Software lab. In the last lab you modified your VM manually, which is important for learning how to get things working, but can’t be quickly copied and redone.

Project Note

Use the code you develop in main.tf and playbook.yaml as a start for Project 1

Copy the base Directory

In your git repository, create copy the base directory to create a new directory for this lab.

$ cp -R base lab07
$ cd lab07

Danger

You should have performed terraform destroy in base before making a copy

Configure the ACL

In the main.tf file update the allowed ports to include only:

  • 22 (SSH)

  • 80 (HTTP)

Update Packages

The playbook.yaml file already has a play that installs the python3-pip and python3-venv packages. Add the following packages to the list:

  • apache2

  • nodejs

Create a Custom HTML File

Here’s some HTML. Update it with your name and copy paste it into a file called index.html in the same directory as playbook.yaml.

<!DOCTYPE html>
<html>
<body>
<h1>This is my Lab 07 Server</h1>
<p>Your name here.</p>
</body>
</html>

Write a Play to Install index.html

Ansible can copy files from the local machine to the target. You can find the full documentation in the documentation of ansible.builtin.copy. Add the following task to your existing ansible play.

- name: Copy file with owner and permissions
  ansible.builtin.copy:
    src: index.html
    dest: /var/www/html/index.html
    owner: www-data
    group: www-data
    mode: '0644'

Test your Work

Start by using Terraform to create your infrastructure:

$ terraform init 
$ terraform apply 

Now use Ansible to apply the VM configuration:

$ ansible-playbook --private-key ~/.ssh/google_compute_engine -i ../inventory.gcp.yaml playbook.yaml 

You should see your custom HTML by directing your browser to your IP address.

Commit and Push Your Changes

In class I demonstrate committing your work using the Cloud Shell Editor. You can also do it on the command line:

$ git add index.html main.tf playbook.yaml
$ git commit -m "First commit of lab 07"
$ git push 

Turn In

Turn in your main.tf and playbook.yaml files.

Danger

Don’t forget to terraform destroy your resources so you don’t get charged!