Ansible Playbooks

What is the Ansible Playbook?
An Ansible playbook is a configuration management and automation tool used for orchestrating and managing IT infrastructure, applications, and services. Ansible is an open-source automation framework that allows you to define a set of tasks, or "plays," in a playbook file using YAML (Yet Another Markup Language) syntax. These plays can be executed to automate various tasks on remote servers or network devices. Here are the key components and concepts associated with Ansible playbooks:
Playbook: A playbook is a YAML file that defines a series of plays. Each play specifies a set of tasks to be performed on one or more target hosts. Playbooks are easy to read and write, making them a powerful tool for automating complex tasks and infrastructure management.
Play: A play is a collection of tasks that should be applied to a specific group of hosts. You can group hosts by various criteria, such as their roles (e.g., web servers, database servers), their locations, or any other attribute. A playbook can consist of one or more plays.
Task: A task is an individual unit of work within a play. Each task in a playbook specifies a module (e.g., a shell command, package installation, file transfer) and the necessary parameters to carry out a specific action on a remote host.
Module: Modules are reusable, self-contained pieces of code that Ansible uses to perform tasks on remote hosts. Ansible includes a vast library of modules that cover a wide range of system administration and configuration tasks. Examples include the "yum" module for managing packages on Red Hat-based systems, the "apt" module for Debian-based systems, and the "file" module for managing files and directories.
Inventory: An inventory file defines the hosts and groups of hosts that Ansible can manage. Ansible uses this file to determine where to execute tasks specified in a playbook. The inventory can be static or dynamic, and it can include information like hostnames, IP addresses, and group assignments.
Variables: Playbooks can use variables to make them more dynamic and reusable. Variables can be defined at various levels, including in the playbook, in inventory files, or as facts gathered from remote hosts.
Roles: Roles are a way to organize and structure your playbooks and tasks in a more modular and reusable manner. They allow you to encapsulate related tasks, variables, and files into a single directory structure.
Handlers: Handlers are tasks that are only executed when another task triggers them. They are often used for actions that should be performed when a change requires a service to be restarted or when a specific condition is met.
Task-01
Write an Ansible playbook to create a file on a different server
- Write an ansible playbook to create a new user.
-
name: Create a User
hosts: all
become: yes
tasks:
- name: Create a User
user:
name: mizan
- To run the above playbook use the following command:
ansible-playbook user.yml
Write an ansible playbook to create a file on a different server.
name: Display of Date in Playbook
hosts: all
server: yes
tasks:
- name: Display of Date
command: date
- name: Greeting Message
command: echo "Hello World"
# Similarly we can create a file using file module
- name: Create a File
file:
path: /home/mizan/ansible.txt
state: touch
- To run the above playbook use the following command:
ansible-playbook date_play.yml
Write an Ansible playbook to install docker on a group of servers.
-
name: Playbook for Installing Docker
hosts: all
become: yes
tasks:
- name: Install Docker
apt:
name: docker.io
state: latest
- name: start and enable docker
service:
name: docker
state: started
enabled: yes
- To run the above playbook use the following command:
ansible-playbook docker_install.yml
Task-02
Ansible playbooks:
An Ansible playbook is a
structuredandhuman-readable documentwritten in YAML (YAML Ain't Markup Language) that defines aset of tasksand configurations to be performed on remote servers. Playbooks are the heart of Ansible automation and allow you to define the desired state of your systems and applications.YAML Syntax:
Playbooksare written in YAML format, which is ahuman-readable markup language. YAML uses indentation and colons to structuredata hierarchically. Indentation is crucial in YAML to define thenesting levels of data.Play: A
playbookstarts with alist of plays. Each play targets a specific group of hosts and defines a set of tasks to be executed on those hosts.name: A user-friendly name for the play.
hosts: The group(s) of hosts this play will target. Host groups are defined in the inventory file.
become: Indicates whether privilege escalation is required to perform tasks (using sudo).
tasks: List of tasks to be executed in this play.
For Example;
---
- name: My First Playbook
hosts: web_servers
become: yes
tasks:
# Tasks will be defined here
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
- name: A description of the task.
module: The Ansible module to use for the task (e.g., apt, yum, file, copy, etc.).
module_arguments: Arguments specific to the module being used.




