Video Summary

Day-03 | Write Your First Ansible Playbook | For Absolute Beginners

Abhishek.Veeramalla

Main takeaways
01

YAML is a human-readable data-serialization language used to structure Ansible playbooks.

02

Ansible playbooks are YAML files (start with '---') composed of plays, each targeting hosts and running tasks.

03

Tasks use modules (e.g., apt, copy) to install packages and transfer files; refer to Ansible docs for module options.

04

Run playbooks with ansible-playbook -i <inventory> <playbook.yaml>; each play begins with gather_facts.

05

Ensure EC2 security groups allow ingress on port 80 to access the deployed web application.

Key moments
Questions answered

What is YAML and why is it preferred over plain text for playbooks?

YAML is a human-readable data-serialization language with a standardized format. It's preferred because it enforces structure (lists, dictionaries, types) which prevents inconsistent formatting and parsing errors that occur with free-form text files.

How does an Ansible playbook file start and what does it contain?

An Ansible playbook is a YAML file that begins with three hyphens (---). It contains one or more plays; each play specifies target hosts, the remote user, and a sequence of tasks that call Ansible modules.

What are tasks and modules in a playbook example?

Tasks are individual actions defined inside a play; they use Ansible modules to perform work. For example, the 'apt' module installs Apache and the 'copy' module places an HTML file on the server.

How do you run a playbook against a specific inventory file?

Use the command ansible-playbook -i <inventory_file> <playbook.yaml>, where -i points to your inventory listing the target hosts.

Why is gather_facts important at the start of a play?

gather_facts runs automatically to validate connectivity and collect system variables from the target host, which can be used in subsequent tasks and to ensure the host is reachable.

What network change is required to view the deployed site on EC2?

You must configure the EC2 instance's security group to allow inbound HTTP traffic on port 80 so the deployed application can be accessed from a browser.

Introduction to Ansible Playbooks and YAML Basics 00:42

"Today is day three where we will jump into the world of Ansible Playbooks."

  • The focus of this episode is to explore Ansible Playbooks, beginning with a foundational understanding of YAML, which is essential for writing playbooks effectively.

  • The presenter will clarify the structure of Ansible Playbooks, including plays, modules, tasks, and collections.

  • By the end of this session, viewers will create their first Ansible Playbook aimed at deploying a static application on a web server located on an EC2 instance.

Understanding YAML 02:25

"YAML is a human-readable data serialization language."

  • YAML serves as a format to structure and represent data in a clear, readable way, making it preferable over plain text files, especially for complex data.

  • Using simple examples, the presenter illustrates how data can be structured with strings, numbers, and booleans in YAML syntax, emphasizing that there is a standardized format to follow.

  • The importance of using standards like YAML and JSON is highlighted, as they prevent discrepancies in data formatting that can lead to syntax errors in applications.

Writing YAML Files 07:10

"If you know how to write strings, numbers, and booleans in YAML, then you know YAML."

  • Learning to write YAML boils down to mastering a few key components: strings, numbers, booleans, lists, and dictionaries. This straightforward syntax involves using a key followed by a colon and space, then the value.

  • An example is provided where the presenter outlines how to document personal information in a YAML file, demonstrating both list and dictionary structures.

  • The distinction between lists, which use hyphens for each entry, and key-value pairs in dictionaries is made clear, as is the specific syntax required for both.

Ansible Playbook Structure 12:52

"Ansible Playbook is basically a YAML file and starts with three hyphens."

  • Ansible Playbooks are structured as lists of plays, where each play addresses a specific task, such as setting up a database server or an application server.

  • The content of a playbook includes various plays, allowing users to organize tasks efficiently and logically within a single YAML file.

  • The presenter emphasizes the modular aspect of Playbooks, enabling users to define and execute multiple related tasks distinctly and clearly.

Structure of Ansible Playbooks 14:02

“Ansible Playbooks are essentially a YAML file that starts with three hyphens, where you can define one or more plays.”

  • Ansible Playbooks serve as a structured format for executing tasks using YAML syntax. Each Playbook begins with three hyphens.

  • Within a Playbook, you can have multiple plays, which allows you to organize and separate your tasks based on their functions.

  • Each play requires you to specify the list of hosts it should execute against, similar to how you specify hosts in ad-hoc commands.

Components of a Play 14:35

“A play has a host section that indicates against which host it should execute.”

  • The first component of a play is the host section, which determines the target machine(s) for executing the tasks.

  • You also need to define the remote user account under which the tasks will be executed. This is crucial for permissions and execution rights.

  • Each play will consist of tasks, which represent the actions you want to perform on the host.

Defining Tasks Within a Play 16:44

“Tasks within a play utilize modules, which are responsible for performing the execution of actions.”

  • Tasks within a play are defined to perform specific operations, such as installing software or managing services.

  • Just like ad-hoc commands, which use modules, tasks in a playbook utilize Ansible modules to carry out their functions.

  • For example, common tasks may include installing a database or starting a service, which would reference the appropriate Ansible module to accomplish these tasks.

Example of a Playbook Task 19:59

“The first task in the playbook is to install the Apache server, followed by the task to place the HTML file in the correct directory.”

  • In an illustrative example, the first task is to install the Apache web server on a system using the 'apt' module, which allows you to install packages easily.

  • After the installation, the subsequent task involves copying an HTML file to the appropriate directory on the server so that it can be served to users.

  • This task uses the 'copy' module, which facilitates transferring files to remote locations in Ansible.

Utilizing Ansible Documentation 22:01

“You can access Ansible documentation for modules to understand their syntax and options.”

  • When writing tasks, it’s essential to refer to the official Ansible documentation to find the necessary modules and their syntax.

  • The documentation is a valuable resource, providing clear guidance on available modules and their functionalities, making it easier for beginners to implement various tasks in playbooks.

  • Familiarize yourself with the built-in modules to streamline the process of writing effective playbooks for your needs.

Writing Your First Ansible Playbook 27:09

"Use Ansible hyphen playbook minus I for inventory file location."

  • The Ansible playbook is initiated using the command with specific flags, where "-I" indicates the inventory file location. The playbook filename should end with ".yaml".

  • The playbook is designed to execute tasks against all hosts listed in the inventory file, ensuring that actions are spread across multiple nodes if necessary.

Understanding Gather Facts 27:44

"The first task in every Ansible play is to gather facts."

  • Every Ansible play begins with the "gather facts" task, which validates the connection to the target EC2 instance and retrieves essential information from it.

  • This step is critical for informing further actions in the playbook.

Task Execution Overview 30:21

"Each instance executed three tasks: gather facts, install Apache, and copy the file."

  • The playbook execution involved three tasks, where gathering facts was successful, but installing Apache was impacted by an earlier misconfiguration (state set to absent).

  • Upon correcting the state to present, the installation of Apache succeeded, and the playbook confirmed that the Apache service was indeed running.

Configuring Security for Access 31:13

"You need to enable the security group to allow ingress traffic on port 80."

  • To access the deployed application, security group settings must be adjusted to permit incoming traffic on HTTP port 80.

  • This involves creating a rule that allows HTTP traffic from any source, which is a necessary step before successfully loading the application in a web browser.

Learning Progression in Ansible 32:30

"As we progress over 14 days, different modules will be covered."

  • The learning path includes a promise to explore various Ansible modules over a two-week period, emphasizing documentation reference for example selection.

  • This structure is designed to enhance understanding of how to write increasingly complex Ansible playbooks through practical examples drawn from official documentation.