Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Ansible Facts in AWS

For just about every Ansible module that performs an AWS task, there is a corresponding module for collecting facts regarding the related AWS component. A thorough understanding of the AWS principles in Ansible can help with implementing automation. This exercise promotes exploration of the facts provided for various AWS-related modules.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Jul 19, 2019

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/report.yml` and Add Ansible Tasks That Output the Required Values into `report.txt`

    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 such that it resembles the following:

    - hosts: localhost
    gather_facts: no
    vars_files:
    - /home/ansible/keys.yml
    tasks:
    - name: Get VPC facts
     ec2_vpc_net_facts:
       aws_access_key: "{{ AWS_ACCESS_KEY_ID }}"
       aws_secret_key: "{{ AWS_SECRET_ACCESS_KEY }}"
       region: "{{ AWS_REGION }}"
     register: vpc_facts
    - name: Add line to facts.txt
     lineinfile:
       path: /home/ansible/facts.txt
       line: "VPC ID: {{ vpc_facts.vpcs[0].vpc_id }}"
    
    - name: Get VPC Subnet Facts
     ec2_vpc_subnet_facts:
       aws_access_key: "{{ AWS_ACCESS_KEY_ID }}"
       aws_secret_key: "{{ AWS_SECRET_ACCESS_KEY }}"
       region: "{{ AWS_REGION }}"
       filters:
         vpc-id: "{{ vpc_facts.vpcs[0].vpc_id }}"
     register: subnet_facts
    - name: Add line to facts.txt
     lineinfile:
       path: /home/ansible/facts.txt
       line: "Subnet ID: {{ subnet_facts.subnets[0].subnet_id }}"
    
    - name: Get EC2 instance facts
     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: Get Security Group facts
     ec2_group_facts:
       aws_access_key: "{{ AWS_ACCESS_KEY_ID }}"
       aws_secret_key: "{{ AWS_SECRET_ACCESS_KEY }}"
       region: "{{ AWS_REGION }}"
       filters:
         group-id: "{{ ec2_facts.instances[0].security_groups[0].group_id }}"
     register: security_group_facts
    - name: Add line to facts.txt
     lineinfile:
       path: /home/ansible/facts.txt
       line: "Security Group Rule Set: {{ security_group_facts.security_groups[0].ip_permissions }}"
    
  2. Challenge

    Run the Modified `/home/ansible/report.yml` to Validate That the Playbook Successfully Generates the Report

    • Log into the Ansible control node as the ansible user.
    • Run the following command:
      • ansible-playbook /home/ansible/report.yml

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans