TLDR: This blog post introduces Ansible Playbooks, covering the basics of YAML, the structure of Ansible Playbooks, and a practical example of creating a web server and deploying a static application on an EC2 instance.
Welcome to the third episode of the Ansible Zero to Hero series. In this post, we will dive into the world of Ansible Playbooks, building on the knowledge gained in the previous episodes. We will start with a brief introduction to YAML, which is essential for writing Ansible Playbooks, and then we will explore the structure of an Ansible Playbook. Finally, we will create our first Ansible Playbook to set up a web server and deploy a static application.
Recap of Previous Episodes
In the first two episodes, we covered the following topics:
Day 1: Introduction to Ansible and getting started.
Day 2: Passwordless authentication and Ansible ad-hoc commands.
Understanding YAML
Before we jump into Ansible Playbooks, let's discuss YAML, as many subscribers have requested a basic understanding of it. YAML stands for "YAML Ain't Markup Language" and is a human-readable data serialization language. It is commonly used for configuration files and data exchange between languages with different data structures.
Why Use YAML?
Using a plain text file for data input can lead to inconsistencies, especially with complex data. Unlike text files, YAML provides a standardized format that ensures data is structured correctly. This standardization helps avoid syntax errors that can occur when using plain text files.
Basic Syntax of YAML
To write a YAML file, you need to understand the following:
Strings, Numbers, and Booleans: Use the format
name: value
.Lists: Start each item with a hyphen followed by a space.
Dictionaries: Use key-value pairs without hyphens.
Lists of Dictionaries: Combine the syntax of lists and dictionaries.
Example of a YAML File
Here’s a simple example of a YAML file:
---
name: Abishek
age: 30
working: true
tasks:
- YouTube content creation
- Take care of home
- Exercise daily
address:
street: Some Street
city: Some City
state: Some State
pin_code: 123456
country: Some Country
Structure of Ansible Playbooks
Ansible Playbooks are essentially YAML files that define a series of tasks to be executed on remote hosts. Each Playbook starts with three hyphens (---
) and consists of a list of plays.
Components of a Playbook
Plays: A Playbook can contain multiple plays. Each play defines a set of tasks to be executed on specified hosts.
Hosts: Each play specifies the target hosts on which the tasks will run.
Remote User: The user account under which the tasks will be executed.
Tasks: The actions to be performed, which utilize Ansible modules.
Example Playbook Structure
Here’s a basic structure of an Ansible Playbook:
---
- name: Install web server and deploy application
hosts: all
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Copy HTML file
copy:
src: index.html
dest: /var/www/html/index.html
Creating Your First Ansible Playbook
In this section, we will create a Playbook that installs an Apache web server and deploys a static HTML application on an EC2 instance.
Step 1: Define the Playbook
Start by creating a YAML file named first_playbook.yaml
with the following content:
---
- name: Set up web server
hosts: all
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Copy HTML file
copy:
src: index.html
dest: /var/www/html/index.html
Step 2: Running the Playbook
To execute the Playbook, use the following command in your terminal:
ansible-playbook -i inventory.ini first_playbook.yaml
Step 3: Verify the Installation
After running the Playbook, check the EC2 instance to ensure that Apache is installed and the HTML file is correctly placed in the /var/www/html/
directory. You can do this by connecting to the instance and running:
sudo systemctl status apache2
Conclusion
In this post, we covered the basics of YAML and the structure of Ansible Playbooks. We also created our first Playbook to set up a web server and deploy a static application. As we progress through this series, we will explore more complex Playbooks and Ansible modules. Thank you for joining me, and I look forward to seeing you in the next episode!