Video Summary

Day-06 | Create Resources on AWS using Ansible | Ansible Variables Demo | Ansible Vault Demo

Abhishek.Veeramalla

Main takeaways
01

Use Ansible collections to interact with cloud APIs from the control node instead of SSHing into managed nodes.

02

Install the Amazon/AWS collection and boto3 on the control node to enable AWS modules.

03

Keep AWS credentials in Ansible Vault and reference them as normal variables in playbooks.

04

Declare variables in defaults, group vars, role vars, or pass them via --extra-vars; extra-vars have highest precedence.

05

Use Jinja2 templating ({{ var }}) to reference variables and make playbooks reusable and environment-agnostic.

Key moments
Questions answered

Why use Ansible collections for AWS instead of built-in modules?

Collections contain cloud-specific modules and roles that run on the control node and interact with AWS APIs; built-in modules run on managed nodes via SSH and don't provide API-driven provisioning for cloud resources.

What do you need to install before using Ansible AWS modules?

Install the Amazon/AWS Ansible collection from Ansible Galaxy on the control node and install the boto3 Python library (pip install boto3 or pip3 install boto3).

How should AWS credentials be handled in Ansible playbooks?

Store AWS access key and secret in Ansible Vault (encrypted file) and reference them with normal variable syntax ({{ var_name }}) to avoid exposing secrets in code.

Where can you declare variables in Ansible and which have the highest precedence?

Variables can be in defaults, role vars, group vars, playbooks, or passed on the command line. Extra variables passed with -e/--extra-vars have the highest precedence.

Why set hosts: localhost when provisioning cloud resources with Ansible?

Using hosts: localhost tells Ansible to execute modules on the control node; cloud modules run locally and call provider APIs rather than connecting to remote managed nodes.

Overview of Ansible Series and Progression 00:00

"This is episode 6 of the Ansible Zero to Hero series, where we’ve completed five episodes so far."

  • The series consists of 14 episodes, with five episodes completed prior to this one.

  • Episode one introduced the basics of Ansible and how to get started.

  • In episode two, viewers learned about Ansible ad-hoc commands and passwordless authentication, as well as how to set up an inventory file.

  • Episode three covered the creation of a first Ansible Playbook and included a brief introduction to YAML syntax and data types.

  • Episode four focused on Ansible roles, detailing their folder structure and the differences between Playbooks, ad-hoc commands, and roles.

  • Episode five built on the previous episode by diving deeper into Ansible roles, utilizing Ansible Galaxy to implement pre-built roles, specifically showcasing the installation of Docker on EC2 instances.

Objectives of Episode 6: Ansible Variables and AWS Resources 02:10

"In this episode, we will learn about Ansible variables and their precedence, starting with an example of creating AWS resources."

  • The episode aims to demonstrate how to create resources on AWS using Ansible, specifically EC2 instances and S3 buckets.

  • Viewers will be using the Ansible AWS collection to facilitate this process.

  • There will be a demonstration of how using variables can enhance the project and illustrate their practical utility.

  • The session will also cover Jinja2 templating, a standard method for using variables within Ansible, as well as the concept of variable precedence, which dictates the order of priority for variable declarations.

Transitioning from SSH to API-Based Resource Management 05:08

"Now we want to talk to the APIs of AWS to create resources rather than SSH into virtual machines."

  • The video illustrates a different approach to using Ansible, where instead of connecting to virtual machines via SSH, commands will interact with APIs.

  • This is necessary for creating cloud resources, such as those on AWS, Azure, and other platforms, which do not support SSH protocols.

  • To accomplish this, Ansible uses specific modules designed for these environments without requiring passwordless authentication for executing commands.

  • The modules operate through APIs, with Ansible executing Python code on the control node that communicates with the cloud service APIs to carry out resource creation.

Understanding Ansible Collections and Installation Requirements 08:01

"Ansible has a concept called collections, which include various roles and modules related to specific third-party tools."

  • Collections in Ansible simplify the process of utilizing third-party tools by packaging related roles and modules together.

  • Users can refer to Ansible documentation to find collections for different technologies, allowing straightforward access to available modules.

  • For example, to work with AWS, users can find a specific Amazon collection that includes modules for managing EC2 instances, S3, IAM, and more.

  • To utilize these collections, users must install them first, as described in the tutorial, specifically emphasizing how to install the AWS collection via Ansible Galaxy.

  • The steps for this installation are provided in a dedicated folder, making it easy for viewers to follow along and set up their environment.

Utilizing Pre-Built Modules vs. Collections 11:43

"The pre-built Ansible modules are included with the installation, while collections must be installed separately to access additional functionalities."

  • Pre-built Ansible modules, like the apt or copy modules, are readily available after installing Ansible and do not require additional steps to access.

  • Collections, on the other hand, require installation before use, particularly for interacting with cloud platforms or other external services.

  • Users will write their YAML files similarly for both pre-built modules and collections, but the execution process varies; built-in modules connect through SSH, whereas collections execute on the control node and interact with APIs.

  • This understanding is crucial for leveraging the full potential of Ansible in cloud resource management and automation tasks.

Installing AWS Collection in Ansible 14:11

"The first step is to install the AWS collection."

  • To begin working with AWS in Ansible, the AWS collection needs to be installed from Ansible Galaxy. The process mirrors the installation of roles—search for "Amazon" in the collections section to find the AWS collection and retrieve the installation command.

  • It's important to ensure the AWS collection is installed on the Ansible control node because this node communicates with AWS APIs. Additionally, the Boto3 Python module must be installed, as it's essential for Ansible to access these APIs. You can install Boto3 by running pip install boto3.

  • Be aware that the installation command may vary based on your operating system or Python version; you may need to use pip3 if Python 3 is installed.

Setting Up the Environment for EC2 Instance Creation 16:29

"Now I'm ready to use any of the AWS modules."

  • Once the AWS collection and Boto3 are successfully installed, you're prepared to create EC2 instances using Ansible playbooks.

  • Start by creating a directory for your EC2 creation tasks, such as naming it "ec2." Inside this, create a YAML file (e.g., ec2_create.yaml) for the playbook.

  • An inventory file is essential for specifying the hosts Ansible will operate on. However, since AWS operations are executed using the AWS API, you won’t have traditional managed nodes in this instance.

  • A good practice is to maintain an inventory file anyway for organizational purposes, and even using existing entries, such as app and DB instances, is helpful.

Understanding Playbooks and Variables in Ansible 20:14

"We need to see an Ansible Playbook itself."

  • The playbook will define the host as "localhost," which signifies that the commands will execute on the control machine rather than on managed remote nodes.

  • To connect with AWS's APIs, API keys (access key and secret ID) are needed. However, security is crucial, as including these sensitive keys directly in your playbook could lead to a security breach.

Securing Sensitive Information with Ansible Vault 24:11

"Ansible has a built-in Vault integration to secure your variables."

  • Ansible Vault allows for the secure storage of passwords and API tokens, which is essential for maintaining security while using sensitive information in playbooks. You should establish a password for the Vault to access this sensitive information safely.

  • The command you will run first is for creating a random Base64 encoded password, which will be stored in a file named vault.pass. This adds a layer of protection similar to a real vault.

  • To create a new file for the vault, use the ansible-vault create command, which requires the password you generated earlier. This ensures that any sensitive data remains safe while still being accessible for playbooks.

Adding AWS Access Key and Secret Key 26:59

"To add sensitive information such as AWS access keys to Ansible, it is advisable to create a dedicated IAM user with necessary permissions, rather than using the root IAM user."

  • In the video, the presenter explains the process of adding AWS credentials to Ansible, including both the access key and secret key.

  • He emphasizes the importance of using a dedicated IAM (Identity and Access Management) user for Ansible, as opposed to utilizing the root IAM user, which is generally not recommended for organizational security.

  • The presenter illustrates how to create an access key from the AWS console and pauses to safeguard sensitive information before completing the process.

Using Ansible Vault for Storing Secrets 28:19

"Ansible Vault allows you to securely store sensitive variables, enabling easy access during playbook execution without exposing them in the code."

  • After securing the AWS credentials in Ansible Vault, the presenter describes how to reference these variables in the playbook using double curly brackets.

  • Variables stored in Ansible Vault can be directly accessed without needing special syntax. By placing the variable name within double curly brackets, Ansible recognizes it as a variable reference.

  • The presenter prepares to execute a playbook and explains that the password used to secure the Vault will need to be provided when running the playbook.

Executing the Playbook and Debugging 29:30

"When executing the playbook, the most common errors can often be traced back to syntax issues or misconfigured variables."

  • Upon attempting to execute the playbook, the presenter encounters an error related to unrecognized variable definitions, necessitating debugging.

  • He explains that it's crucial to confirm the accuracy of the variable definitions, such as AWS access keys, which might be misconfigured within the Vault file.

  • The importance of linting the playbook to identify syntax issues is highlighted, allowing for clearer debugging of the tasks defined in the YAML file.

Importance of Using Collections for Resource Provisioning 34:45

"Using Ansible collections enables more robust automation, particularly for infrastructure provisioning and network automation, as they interact directly with API endpoints."

  • The presenter discusses the role of Ansible collections in automating tasks, especially when provisioning infrastructure on cloud platforms like AWS.

  • He emphasizes that built-in modules can only handle limited automation tasks, while collections containing roles and modules facilitate communication with APIs for provisioning.

  • The presenter contrasts the execution of built-in modules on managed nodes with the API interactions via collections running on control nodes, highlighting the necessity of using the correct method for specific tasks.

The Need for Variables in Playbooks 37:47

"Hardcoding values in Ansible playbooks restricts their usability; utilizing variables allows for flexibility and adaptability across different environments."

  • The presenter stresses the significance of utilizing variables within playbooks rather than hardcoding specific values, such as instance types or operating system images.

  • Hardcoded playbooks limit users to predefined configurations, making it challenging to accommodate diverse requirements, such as choosing between different operating systems or instance sizes.

  • He points out the complexity of variable declaration in Ansible, where users can define variables in multiple locations, which can lead to confusion and inefficiency. The presenter believes there's a need for improvement in how variables are handled within Ansible.

Variable Declaration in Ansible 40:42

Ansible allows users to declare variables at multiple places within the code, which can lead to complications if not managed correctly.

  • In Ansible, variables can be declared in various locations, each with its own level of priority.

  • The primary places to declare variables include the defaults of a role and the main playbook file (main.yml).

  • For instance, if a user defines a variable for instance type in the defaults directory as t2.micro, this can be referenced in the playbook using the Jinja2 templating standard within double curly braces.

  • It is essential to both define (declare) the variable and then utilize it appropriately in your Ansible code to maintain clarity and functionality.

Variable Precedence in Ansible 47:11

Ansible determines which variable to prioritize based on where it is declared, affecting how the playbook executes.

  • Ansible utilizes a hierarchy for variable precedence, where roles and specific files can influence the final value of variables.

  • For example, if you declare type as t2.medium in a variables file while also declaring it as t2.micro in defaults, the value from the variables file will take precedence.

  • This precedence allows for flexibility when sharing playbooks among teams, as teams can customize their own variable overrides without altering the default definitions.

  • It is recommended to use the defaults for general settings while allowing users to override as necessary in their variable files.

Utilizing Extra Variables 52:36

Extra variables have the highest precedence in Ansible, allowing them to override any default settings specified.

  • When running a playbook, you can pass extra variables using the -e flag in the command line, which will take precedence over both role defaults and other defined variables.

  • This means that if you specify the instance type as t2.micro through extra variables, it will override any other values set, ensuring that the execution uses your specified settings.

  • This feature is particularly useful for dynamic deployments where users might want to change environment-specific settings without altering the foundational code provided in the playbook.

Variable Precedence in Ansible 54:06

"Extra variables provide the highest precedence in Ansible and can override any previously declared variable."

  • In Ansible, variable values can be defined and overridden by project teams through files like vars/main.yaml.

  • The command line allows users to pass variables using the -e or --extra-vars option, which holds the highest precedence over any other declared variables.

  • This means that if a variable is defined in multiple places, the value provided through extra variables will take priority.

Common Places to Declare Variables 54:51

"You can use variables directly in your Playbook for each specific task."

  • Variables can be declared directly in the Playbook, making it easier to reference and utilize them for specific tasks.

  • A common practice is to use template references where variables are called within the Playbook.

  • Additionally, grouping variables in an inventory file can be useful. For example, if you have separate groups for application and database instances, you can define different variable values for each group accordingly.

Group Variables Usage 56:10

"Group variables allow you to set distinct values for different instance types, such as app and DB instances."

  • In scenarios where application and database instances require different variables (like passwords), you can create group variable files.

  • For instance, creating an app.yaml file can store variables specifically relevant to the application instances, while a db.yaml file caters to the database instances.

  • This helps in managing configurations more effectively, especially when different groups share similar structural roles but need unique configurations.

Summary of Variable Options 58:41

"The most common places to utilize variables are defaults, group variables, and extra vars."

  • Understanding where and how to declare variables is crucial in Ansible. The key areas include:

    • defaults/main.yaml: Regularly used by developers to set defaults that could be overridden.

    • Group variables: Define variables that vary by group, ensuring appropriate values are used for different instances.

    • Extra vars: Provide flexibility and the ability to override anything declared elsewhere in the Playbook.

  • While grasping the precedence of these variables is useful, it is not critical to memorize all details; referring to official documentation for clarification is a sound strategy.