Skip to main content

Command Palette

Search for a command to run...

Day 43: Ansible Playbooks

Published
2 min read
Day 43: Ansible Playbooks
P

Hi, I am an AWS-certified cloud engineer and I write about my progress and learnings of DevOps.

Hello everyone!

Yesterday we saw Ansible Ad-hoc commands and how to establish relationships among the Master and worker servers. Today we are diving into the world of ansible playbooks. We will control all hosts from a single machine. Let’s start.

What are 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.

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.

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=Sam

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

To install docker on the 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

Yay!! You’ve installed docker from master machine to all the slave machines.


Thanks for reading ;)

More from this blog

C

Cloud Cognizance

49 posts

Swimmer | Technical Writer | Cloud Engineer