Day-6-Ansible

Creating AWS Resources with Ansible: A Comprehensive Guide to Variables and Precedence

TLDR: In this blog post, we explore how to create AWS resources using Ansible, focusing on the use of variables and their precedence. We cover the installation of necessary collections, the creation of EC2 instances, and the importance of securing sensitive information with Ansible Vault. Additionally, we discuss the various places to declare variables in Ansible and the implications of variable precedence.

Hello everyone, my name is Abhishek, and welcome back to my channel. Today marks episode 6 of the Ansible Zero to Hero series, a 14-episode journey into mastering Ansible. In the previous episodes, we covered a range of topics from the basics of Ansible to more advanced concepts like roles and using Ansible Galaxy. In this episode, we will focus on creating AWS resources using Ansible, specifically EC2 instances and S3 buckets, while also diving into the use of variables and their precedence.

Recap of Previous Episodes

Before we jump into today's topic, let's quickly recap what we've learned so far:

  1. Episode 1: Basics of Ansible and getting started.

  2. Episode 2: Ansible ad-hoc commands and passwordless authentication.

  3. Episode 3: Writing our first Ansible Playbook and understanding YAML.

  4. Episode 4: Introduction to Ansible roles and their folder structure.

  5. Episode 5: Deep dive into Ansible roles and using Ansible Galaxy to install pre-built roles.

Today's Focus: Ansible Variables and AWS Resource Creation

In this episode, we will learn about Ansible variables and their precedence. We will start with an example of creating AWS resources such as EC2 instances and S3 buckets using the Ansible AWS collection. After writing the Ansible role, we will explore how to use variables to enhance our project.

Understanding Ansible Variables

Ansible allows you to declare variables in multiple places, which can lead to complexity. Understanding where to declare variables and their precedence is crucial for effective playbook management. We will also cover Jinja2 templating, which is the standard for using variables in Ansible.

Creating AWS Resources with Ansible

To create AWS resources, we will use the Ansible AWS collection. This collection allows us to interact with AWS APIs directly from our control node, rather than connecting to virtual machines via SSH. Here’s how to get started:

  1. Install the Ansible AWS Collection: Use the command ansible-galaxy collection install amazon.aws to install the necessary collection.

  2. Install Boto3: Ensure that the Boto3 Python library is installed on your control node, as it is required for Ansible to communicate with AWS APIs. You can install it using pip install boto3.

Writing the Playbook

Next, we will write a playbook to create an EC2 instance. Here’s a simplified version of the steps:

  1. Create a folder for your playbook and an inventory file.

  2. Define the playbook structure, specifying the host as localhost since we are executing the playbook on our control node.

  3. Use the EC2 module to define the parameters for the instance, such as instance type, security group, and region.

Securing Sensitive Information with Ansible Vault

When creating resources on AWS, you will need to provide sensitive information such as AWS access keys and secret keys. To secure these credentials, we will use Ansible Vault. Here’s how:

  1. Create a vault password file to secure your sensitive information.

  2. Use the command ansible-vault create secrets.yml to create a vault file where you can store your AWS credentials.

  3. Reference these variables in your playbook using Jinja2 templating.

Variable Precedence in Ansible

Ansible allows you to declare variables in various places, leading to a hierarchy of precedence. Here are some key points:

  • Defaults: Variables declared in the defaults/main.yml file have the lowest precedence.

  • Role Variables: Variables declared in the vars/main.yml file have higher precedence than defaults.

  • Extra Variables: Variables passed via the command line using -e have the highest precedence and will override any other variable declarations.

Understanding this precedence is essential for managing your playbooks effectively. For example, if you declare an instance type in both the defaults and vars files, the value in the vars file will take precedence.

Conclusion

In this episode, we learned how to create AWS resources using Ansible, focusing on the use of variables and their precedence. We also covered the importance of securing sensitive information with Ansible Vault. As we move forward in this series, we will continue to explore more advanced topics and practical applications of Ansible.

Thank you for watching, and I hope you found this episode informative. In the next class, we will dive into more exciting features of Ansible. See you all next time!