Table of contents
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.
Ansible playbooks are written in YAML (YAML Ain't Markup Language) syntax, which is a human-readable data serialization format. The syntax is designed to be simple, expressive, and easy to understand.
Here's a sample Ansible playbook code that demonstrates the basic syntax:
- name: Sample Playbook
hosts: servers
become: true
tasks:
- name: Task 1
module1:
parameter1: value1
parameter2: value2
- name: Task 2
module2:
parameter3: value3
parameter4: value4
when: some_condition == true
handlers:
- name: Restart Service
service:
name: myservice
state: restarted
vars:
variable1: value1
variable2: value2
In the above sample playbook:
The playbook starts with three dashes (
---
) to indicate the beginning of a YAML document.The play is named "Sample Playbook" and will be executed on the hosts defined in the "servers" group.
The
become
keyword is used to elevate privileges and execute tasks as a privileged user (e.g., usingsudo
).The playbook contains two tasks: "Task 1" and "Task 2". Each task uses different modules (
module1
andmodule2
) and specifies their respective parameters.The second task (
Task 2
) is conditionally executed (when
) based on the value ofsome_condition
.The
handlers
section defines a single handler named "Restart Service" that uses theservice
module to restart a service named "myservice".The
vars
section defines two variables (variable1
andvariable2
) with their respective values.
This is a basic structure that you can build upon to create more complex and powerful playbooks to automate various tasks using Ansible.
Task-01
Write an Ansible playbook to create a file on different servers.
$ ansible-inventory --list -y
$ ansible servers -m ping
$ vim create_file.yml
$ ansible-playbook create_file.yml
Write an Ansible playbook to create a new user.
$ vim create_new_user.yml
Write an Ansible playbook to install docker on a group of servers
$ vim install_docker.yml
$ ansible-playbook install_docker.yml
Watch this video to learn about Ansible Playbooks
Task-02
Write a blog about writing Ansible playbooks with the best practices.
Happy Learning :)
Day 58 task is complete!
90DaysOfDevOps Tasks👇