Day58 ----> 90DaysOfDevOps Challenge @TWS

Day58 ----> 90DaysOfDevOps Challenge @TWS

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., using sudo).

  • The playbook contains two tasks: "Task 1" and "Task 2". Each task uses different modules (module1 and module2) and specifies their respective parameters.

  • The second task (Task 2) is conditionally executed (when) based on the value of some_condition.

  • The handlers section defines a single handler named "Restart Service" that uses the service module to restart a service named "myservice".

  • The vars section defines two variables (variable1 and variable2) 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.

image

$ ansible-inventory --list -y

image

$ ansible servers -m ping

image

$ vim create_file.yml

image

$ ansible-playbook create_file.yml

image

image

Write an Ansible playbook to create a new user.

$ vim create_new_user.yml

image

image

image

Write an Ansible playbook to install docker on a group of servers

$ vim install_docker.yml

image

$ ansible-playbook install_docker.yml

image

image

Watch this video to learn about Ansible Playbooks

Task-02

Write a blog about writing Ansible playbooks with the best practices.

image

Happy Learning :)

Day 58 task is complete!

90DaysOfDevOps Tasks👇

github.com/Chaitannyaa/90DaysOfDevOps.git

Chaitannyaa Gaikwad | LinkedIn

Â