Lab Guide: Provisioning an AWS EC2 Instance with Ansible Lightspeed

A guide to using Ansible Lightspeed to generate a playbook that provisions a t3.small EC2 instance on AWS.


Introduction and Setup

Important Note About This Lab

IBM watsonx Code Assistant models are continuously updated and improved. As a result, some specific task generation challenges in this lab may not work exactly as described due to model updates. This is expected behavior.

The primary goal of this lab is to help you understand the key features of Ansible Lightspeed, including:

  • Playbook Generation - Generating entire playbooks from natural language prompts

  • Task Generation - Generating individual tasks or multiple tasks at once

  • Training Matches - Understanding the transparency of AI suggestions

  • Best Practices - Learning how Lightspeed incorporates Ansible best practices

If a specific prompt doesn’t generate suggestions as expected:

  • Try playbook generation instead of task generation (click the Ansible icon → Generate a playbook button, enter your prompt, review the outline, and click Continue)

  • Experiment with different prompt variations to see what gives you the best suggestions

  • Simplify complex multi-task prompts into separate single-task prompts

  • The learning objectives remain the same: understanding how to work with AI-assisted Ansible development

In this challenge, you will use Ansible Lightspeed to generate an Ansible Playbook that automates the provisioning of an Amazon Web Services (AWS) EC2 instance.

Your Mission

Your goal is to use Ansible Lightspeed to generate a playbook that automates the following tasks:

  1. Gathers information about an existing VPC subnet.

  2. Provisions a t3.small EC2 instance within that subnet.

  3. Waits for the instance to be ready and then prints its public IP address.

The examples used in this lab are available in the Ansible Lightspeed Demo repository.

AWS Credentials: This lab uses a temporary AWS account. You’ll find the credentials in the AWS/Azure tab at the top of your lab environment when you need them in Task 4.

Need Help? If you encounter difficulties or want to verify your work, you can reference the solution playbook at playbooks/cloud/aws/solution_provision_ec2_instance.yml in the repository.


Task 1: Generate Playbook Tasks with Ansible Lightspeed

You will now edit a pre-created playbook file and use natural language prompts to have Ansible Lightspeed generate the necessary tasks.

  1. Open the playbook file in VS Code.

    1. Click on the VS Code tab at the top of your lab environment

      Work in a new browser tab: For the best experience with Ansible Lightspeed features, click the VS Code tab to open it in a new browser tab rather than using the inline embedded window.

    2. On the left side of VS Code, click the Explorer icon (two overlapping documents) to show the file explorer

    3. In the file explorer, expand these folders: playbookscloudaws

    4. Click on demo_provision_ec2_instance.yml to open it in the editor

  2. Generate the subnet info and variable tasks.

    Multi-task prompt: This comment starts with # and uses & to separate TWO tasks. Do NOT uncomment - just press ENTER at the end.

    1. Find the line:

          # Gather info from subnet called subnet-lightspeed & create vpc_subnet_id var
    2. Do NOT uncomment this line - leave the # symbol in place

    3. Place your cursor at the end of the line (after "var")

    4. Press ENTER to create a new line

    5. Press TAB when Lightspeed shows suggestions for BOTH tasks

      _Lightspeed Highlight:_ Lightspeed generates two tasks from this single prompt. The first gathers information about the named subnet, and the second uses the `set_fact` module to create a new variable, `vpc_subnet_id`, from the results of the first task.
  3. Generate the EC2 instance provisioning task.

    1. Find the commented line:

          #- name: Provision t3.small instance using my_instance var
    2. Uncomment the line by removing the # (or press CTRL+/ for Windows/Linux or CMD+/ for Mac)

    3. Place your cursor at the end of the line (after "var")

    4. Press ENTER to create a new line

    5. Press TAB when Lightspeed shows a suggestion

      _Lightspeed Highlight:_ Lightspeed correctly uses the `amazon.aws.ec2_instance` module. It also incorporates the `my_instance` variable (defined in the playbook's `vars` section) and the `vpc_subnet_id` variable created in the previous step.
  4. Generate the tasks to get and print the public IP.

    Multi-task prompt: This will generate TWO tasks - one to get the IP and one to print it.

    1. Find the line:

          # Create var from public_ip_address with retry & print it out
    2. Do NOT uncomment this line - leave the # symbol in place

    3. Place your cursor at the end of the line (after "out")

    4. Press ENTER to create a new line

    5. Press TAB when Lightspeed shows suggestions for BOTH tasks

      _Lightspeed Highlight:_ Lightspeed generates a task to create a new variable for the public IP address, including a `wait_for` condition to ensure the IP is available. It then creates a second task to print the value of that new variable.
  5. Save the playbook.

    Click **File** → **Save** from the top menu, or press `CTRL+S` (Windows/Linux) or `CMD+S` (Mac).

Task 2: Review Ansible Lightspeed Training Matches

One of Ansible Lightspeed’s key differentiators is providing information on the potential training data used to generate suggestions.

  1. Open the Lightspeed Training Matches view.

    1. At the top of VS Code, click View in the menu bar

    2. Select Open View…​

    3. In the search box that appears, type Lightspeed

    4. Click on Lightspeed Training Matches from the list

  2. Examine a training match.

    1. In your playbook, click on any task name you generated

    2. Delete one line of the generated code and press ENTER to regenerate the suggestion

    3. Look at the Lightspeed Training Matches pane (usually at the bottom of VS Code)

    4. You’ll see potential training sources. Click on any entry to see details including the content source, author, and license information.

Task 3: Run the Playbook

You can now choose to run the completed playbook using either the automation controller or ansible-navigator.

Option 1: Using Automation Controller

  1. Commit and push the playbook to Git.

    1. On the left side of VS Code, click the Source Control icon (it looks like a branch with circles)

    2. You should see demo_provision_ec2_instance.yml listed under "Changes"

    3. Hover over the file name and click the + (plus) icon that appears to stage the changes

    4. At the top, in the "Message" box, type a commit message like: Provision EC2 instance

    5. Click the Commit button (checkmark icon)

    6. Click the Sync Changes button to push your playbook to the Git repository

  2. Run the playbook from Automation Controller.

    1. Click the Ansible Automation Platform tab at the top of the lab window

    2. If you see a login screen, enter:

      • Username: admin

      • Password: ansible123!

    3. On the left sidebar, click ResourcesTemplates

    4. Find the template named Provision EC2 instance

    5. Click the launch icon (🚀 rocket ship) on the right side of that row

    6. Wait for the job to complete successfully (you’ll see a green "Successful" status)

Option 2: Using ansible-navigator

  1. Open a terminal in VS Code.

    1. Make sure you’re on the VS Code tab

    2. Look at the top menu bar and click TerminalNew Terminal

    3. A terminal panel will open at the bottom of VS Code

  2. Navigate to the playbook folder.

    In the terminal, type the following command and press `ENTER`:
    cd playbooks/cloud/aws
  3. Run the playbook with ansible-navigator.

    Type the following command and press `ENTER`:
    ansible-navigator run demo_provision_ec2_instance.yml
    The playbook will run and show you the results. Press the `ESC` key to return to the terminal prompt when it's done.

Task 4: Verify the EC2 Instance Creation

Finally, verify that the new EC2 instance is running in the AWS console.

  1. Get your AWS credentials and access the console.

    1. At the very top of your lab environment, click the AWS/Azure tab

    2. You’ll see AWS account credentials displayed (Account ID, Username, Password, and Console URL)

    3. Click the AWS Console URL link (or copy it to a new browser tab)

  2. Log in to the AWS Console.

    1. Go back to the AWS/Azure tab to copy the credentials

    2. Copy the Username and Password shown

    3. Paste them into the AWS login page

    4. Click Sign In

  3. View your EC2 instance.

    1. At the top of the AWS Console, make sure the region is set to N. Virginia (us-east-1)

    2. In the search bar at the top, type EC2 and click on EC2 service

    3. On the left sidebar, click Instances (under "Instances")

    4. You should see your new instance named lightspeed-instance with a status of "Running"

    5. This confirms that your Ansible playbook successfully created the EC2 instance!


Conclusion and Additional Resources

Congratulations! You’ve successfully created an Ansible Playbook using Ansible Lightspeed to provision an AWS EC2 Instance!

Solution Playbook Available: If you encountered issues or want to compare your work, you can review the complete solution at playbooks/cloud/aws/solution_provision_ec2_instance.yml in the repository.

Additional Resources

Feedback

Please provide your feedback or report bugs using the Ansible Lightspeed Feedback section located in the VS Code Ansible Extension sidebar.