- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud

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.

Lab Info
Table of Contents
-
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 theansible
user. The password is the same as it is forcloud_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 }}"
-
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
- Log into the Ansible control node as the
About the author
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.