Deploying to AWS with Ansible

1 hour
  • 5 Learning Objectives

About this Hands-on Lab

This exercise provides a simple yet realistic task of deploying a basic website to dynamically provisioned AWS infrastructure. We will work with multiple AWS components through Ansible, and also perform basic web server configuration against a Linux host with Ansible. This exercise will help demonstrate a portion of the power provided by rolling cloud provisioning into deployment automation.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Run the Provided Playbook `/home/ansible/get-environment-details.yml` to Collect Necessary Environment Details
  1. After logging into the EC2 instance, become the ansible user:

    su - ansible

    The password is the same as it is for cloud_user.

  2. Run the following command:

    ansible-playbook /home/ansible/get-environment-details.yml
Replace the word “placeholder” in `/home/ansible/env_vars.yml` with a Unique S3 Bucket Name
  1. Open /home/ansible/env_vars.yml with a text editor.
  2. Change the value placeholder to a unique S3 bucket name of your choosing.
    • Be sure to stick to the S3 naming conventions (https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html for details).
Create a Playbook and Add a Play per the Lab Instructions
  1. Create /home/ansible/deploy.yml
  2. Add an Ansible play that configures your EC2 key pair, EC2 instance, and S3 bucket.
  3. Use the provided variable file for required parameter configuration.
Add Another Play to Further Configure the New EC2 Instance
  1. Add another play that will:
    • Configure the new EC2 instance.
    • Install the httpd package
    • Start and enable the httpd service.
    • Deploy the provided template file into /var/www/html.
  2. Use /home/ansible/env_vars.yml for required parameter configuration values.
Run `/home/ansible/deploy.yml` to Perform the Required Tasks

Run the following command:

ansible-playbook /home/ansible/deploy.yml

Additional Resources

In an effort to get senior management on board with using AWS for web content, we have been asked to produce a quick proof of concept. We need to demonstrate how quickly we can stand up a new website using EC2 for compute and S3 for content. A colleague has started the work but taken ill. We can pick up where they left off.

We have been provided an Ansible Control node and a sandbox AWS environment.

We'll start our work on the Ansible Control node:

  • Run the provided playbook /home/ansible/get-environment-details.yml to complete another provided file, /home/ansible/env_vars.yml with environment-specific configuration information.
  • Edit /home/ansible/env_vars.yml and update the placeholder value for the BUCKET variable. It should be a unique S3 bucket name that meets the S3 naming restrictions.
  • Create the playbook /home/ansible/deploy.yml to perform the following tasks:
    • Using the provided SSH key for the ansible system user in /home/ansible/.ssh/id_rsa.pub, create a new AWS key pair named ansible_keypair.
    • Create a new EC2 instance that meets the following requirements:
      • Use the subnet, security group, and AMI defined in /home/ansible/env_vars.yml.
      • Use ansible_keypair, created in the initial task, as the instance login key pair.
      • Set the instance type to t2.micro.
      • The instance should be deployed with a public IP.
      • The instance should have a tag with the key type and value web.
      • We only need a single instance for the purpose of the proof of concept.
    • Create a new S3 bucket meeting the following requirements:
      • The bucket should be named after the BUCKET variable in /home/ansible/env_vars.yml.
      • The bucket should have public read permissions.
    • Upload the provided image /home/ansible/webimage.png to the new S3 bucket with an object name of /webimage.png
    • The playbook will need to configure the new EC2 instance as a web server in another play in the same playbook.
    • The following tasks must be performed on the EC2 instance after it is provisioned:
      • Install the httpd package using yum.
      • Start and enable the httpd package.
      • Deploy the provided template /home/ansible/index.html.j2 to /var/www/html/index.html. We will need to include the variable file /home/ansible/env_vars.yml in the play to successfully deploy the template.
  • Run the playbook /home/ansible/deploy.yml to build the environment.
  • Verify the work by loading http://<PUBLIC_IP_ADDRESS_OF_NEW_EC2_INSTANCE>/index.html in a web browser. If we did everything correctly, we should see a statement and an image in a browser.

The Ansible control node has been configured, and Ansible is installed. The control node also has a system user named ansible configured with SSH access keys and necessary system privileges.

An IAM user called ansible has been created on the provided AWS sandbox account. The access keys for the ansible IAM user are stored in /home/ansible/keys.sh and /home/ansible/keys.yml for whichever authentication method we prefer. The ansible IAM user has appropriate permissions to perform the required task.

The default Ansible inventory has been configured to include the Ansible control host as localhost.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?