Day 58: Ansible Playbooks

Radheya Zunjur
5 min readSep 4, 2023

--

Welcome to the 58th day of devops challenge. Today we are diving into the world of ansible playbooks. We will control all hosts from single machine. Let’s start.

What is Ansible Playbooks?

Ansible playbooks run multiple tasks, assign roles, and define configurations, deployment steps, and variables. If you’re using multiple servers, Ansible playbooks organize the steps between the assembled machines or servers and get them organized and running in the way the users need them to. Consider playbooks as the equivalent of instruction manuals.

Here’s an overview of how to write an Ansible playbook and why it’s needed:

1. Define the Playbook Structure:

  • Begin by creating a YAML file with a .yml extension to store your playbook.
  • The playbook typically starts with a list of plays. Each play defines a set of tasks to be executed on a specific group of hosts.
  • Specify the target hosts for each play either by explicitly naming them or by referencing host groups defined in your Ansible inventory.

2. Define Tasks:

  • Within each play, you define a series of tasks.
  • Each task should have a name, a module (the Ansible module responsible for performing the task), and module-specific parameters.
  • Modules are Ansible’s way of performing various actions like copying files, installing packages, managing users, and more.

3. Use Variables and Facts:

  • Playbooks can use variables to store data, making them flexible and reusable.
  • You can define variables globally, within plays, or even within individual tasks.
  • Ansible also provides system facts, which are automatically collected data about the target hosts. You can use these facts in your playbook for conditional logic or to retrieve information about the host.

4. Implement Conditionals and Loops:

  • You can use conditional statements within tasks to control when and how tasks are executed based on specific conditions.
  • Loops allow you to repeat tasks multiple times with different input data, often iterating over lists or dictionaries.

5. Organize Playbooks with Roles:

  • Roles are a way to organize playbooks into reusable components. They consist of tasks, variables, and files organized in a standardized directory structure.
  • Roles make it easier to modularize your automation and share common tasks across different playbooks.

6. Use Handlers:

  • Handlers are tasks that are only executed when notified by other tasks. They are often used to respond to changes made by tasks, such as restarting a service after a configuration change.

7. Execute the Playbook:

  • To execute a playbook, use the ansible-playbook command followed by the playbook filename.
  • Ansible will connect to the target hosts specified in the playbook and execute the defined tasks in the order specified.
  • The results of each task execution are displayed in the terminal, making it easy to identify successes and failures.

Basic syntax of writing ansible playbooks

---
- name: Playbook Name
hosts: target_hosts
become: yes # Optional: Use "yes" if you need sudo privileges, else "no"
become_user: root # Optional: Specify the user to become, if using "become"
gather_facts: yes # Optional: Gather facts about the target system, default is "yes"

tasks:
- name: Task Name
module_name:
# Module-specific options and parameters
# Additional task properties

- name: Another Task
module_name:
# Module-specific options and parameters
# Additional task properties

# Handlers (optional, for handling tasks triggered by "notify")

# Variables (optional, for defining playbook-specific variables)
# vars:
# variable_name: variable_value

# Roles (optional, for including external roles)
# roles:
# - role_name
# - role_name: role_variable

# Include other playbooks (optional)
# - include: other_playbook.yml
# vars:
# var_name: var_value

# Conditional statements (optional)
# when: condition

# Looping (optional)
# loop: "{{ item }}"
# loop_control:
# loop_var: item

Why Ansible Playbooks Are Needed:

  • Automation: Playbooks automate routine and complex tasks, reducing the need for manual intervention and minimizing human error.
  • Consistency: Playbooks ensure that configurations and tasks are applied consistently across all targeted hosts, promoting uniformity in your infrastructure.
  • Scalability: Ansible playbooks are scalable, allowing you to manage configurations and deployments across a large number of servers or devices.
  • Reusability: Playbooks and roles can be reused across different projects and environments, saving time and effort in developing automation.
  • Documentation: Playbooks serve as self-documenting scripts, making it clear how systems are configured and what actions have been automated.
  • Version Control: Playbooks can be stored in version control systems like Git, providing a history of changes and facilitating collaboration among team members.

Tasks

Task 1) Write an ansible playbook to create a file on a different server

To create files on remote hosts, create a file with name “create_file.yaml”

---
- name: Create file on hosts
hosts: all
become: yes

tasks:
- name : create a file
file :
path: /home/ubuntu/abc.txt
state: touch

You can run this playbook using the ansible-playbook command.

Let’s check on remote host whether the file has been created on or not in /home/ubuntu

Task 2) Write an ansible playbook to create a new user.

To create users on remote hosts, create a file with name “create_user.yaml”

---
- name: create user on hosts
hosts: all
become: yes

tasks:
- name: create a user
user: name=radhey

Let’s check if the user “radhey” is created on host or

Task 3) Write an ansible playbook to install docker on a group of servers

To install docker on hosts machine create a file called install_docker.yaml


---
- name: This playbook will install Docker
hosts: all
become: true
tasks:
- name: Add Docker GPG apt Key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Add Docker Repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu focal stable
state: present

- name: Install Docker
apt:
name: docker-ce
state: latest

Let’s check on host machines, if docker is installed

Yay!! You’ve installed docker from master machine to all the slave machines. Isn’t ansible fun???

--

--

Radheya Zunjur
Radheya Zunjur

Written by Radheya Zunjur

Database Engineer At Harbinger | DevOps | Cloud Ops | Technical Writer

No responses yet