Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Manipulating EC2 Instances with Ansible

EC2 is at the heart of AWS as the primary compute resource on the platform. Ansible provides several modules that allow us to interact with EC2 instances. Being able to provision and manipulate EC2 instances within Ansible allows for infrastructure automation to be built into a deployment strategy. This exercise will allow students to explore the EC2 functionality in Ansible.

Google Cloud Platform icon
Lab platform
Lab Info
Level
Intermediate
Last updated
Sep 21, 2025
Duration
30m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Create and Edit `/home/ansible/deploy.yml` and Add Ansible Tasks to Stop the Existing EC2 Instance, by Tag, Then Deploy a New EC2 Instance That Meets the Specification Described in the Instructions.

    After logging into the EC2 instance, run su - ansible to become the ansible user. The password is the same as it is for cloud_user.

    Create and edit the playbook (/home/ansible/deploy.yml) so that it resembles the following:

    - hosts: localhost
      gather_facts: no
      vars_files:
        - /home/ansible/keys.yml
      tasks:
        - name: Get Subnet ID and AMI ID from existing server.
          ec2_instance_facts:
            aws_access_key: "{{ AWS_ACCESS_KEY_ID }}"
            aws_secret_key: "{{ AWS_SECRET_ACCESS_KEY }}"
            region: "{{ AWS_REGION }}"
            filters:
              tag:Name: Leo
          register: ec2_facts
    
        - name: Stop Leo Instance
          ec2:
            aws_access_key: "{{ AWS_ACCESS_KEY_ID }}"
            aws_secret_key: "{{ AWS_SECRET_ACCESS_KEY }}"
            ec2_region: us-east-1
            state: stopped
            instance_tags:
              Name: Leo
    
        - name: Deploy new EC2 Instance
          ec2:
           aws_access_key: "{{ AWS_ACCESS_KEY_ID }}"
           aws_secret_key: "{{ AWS_SECRET_ACCESS_KEY }}"
           ec2_region: us-east-1
           instance_type: t2.micro
           image: "{{ ec2_facts.instances[0].image_id }}"
           assign_public_ip: yes
           vpc_subnet_id: "{{ ec2_facts.instances[0].subnet_id }}"
           instance_tags:
             Name: New
    
  2. Challenge

    Run the Playbook `/home/ansible/deploy.yml` to Perform the Required Tasks, Then Log into the AWS Console to Validate that Everything Works
    • Run the following command:
      • ansible-playbook /home/ansible/deploy.yml
    • Log into the AWS Console, and in the EC2 Dashboard (find it by searching for EC2 in the Find Services search box) confirm the new instance's existence and state.
      • It might be best to wait a bit before checking. Once everything is finished processing though, we'll see a Leo instance that's stopped, and a new one called New that is running.
About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight