Ansible Playbooks

·

2 min read

Intro to Ansible Playbooks

Playbooks are the simplest way in Ansible to automate repeating tasks in the form of reusable and consistent configuration files. Playbooks are defined in YAML files and contain any ordered set of steps to be executed on our managed nodes.

Tasks in a playbook are executed from top to bottom. At a minimum, a playbook should define the managed nodes to target and some tasks to run against them.

In playbooks, data elements at the same level must share the same indentation while items that are children of other items must be indented more than their parents.

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.

Ex1: Ansible playbook to create a file on a different server

  1. Let us create a playbook with the name create_file.yml
-
 name: Create file playbook
 hosts: servers
 become: yes
 tasks:
   - name: this will create a new file
     file:
       path: "/home/ubuntu/new_file.txt"
       state: touch
  1. Once the playbook is created, we shall run the playbook with the command ansible-playbook create_file.yml

  1. If we ssh to any one of the servers, we can see that there is a new file with the name "new_file.txt" created

Ex2: Ansible playbook to create a new user.

  1. Let us create a playbook with the name create_new_user.yaml

     -
      name: Create a new file
      hosts: servers
      become: true
      tasks:
        - name: Create a new user
          user: 
            name: sowmya
            state: present
            createhome: yes
    
  2. We shall run the playbook which is in the current folder with the command ansible-playbook create_new_user.yaml

  3. Let us ssh with the command sh -i "ansible_new_key.pem" ubuntu@34.219.67.239 into one of our servers and check if a new user "sowmya" is created

We can see that the user named "sowmya" has been successfully created

Ex3:Ansible playbook to install docker on a group of servers

    1. Let us create a playbook with the name install_docker.yaml

         -
          name: Install docker
          hosts: servers
          become: yes
          tasks:
             - name: update apt cache
               apt:
                 update_cache: yes
             - name: Install docker
               apt:
                 name: docker.io       
                 state: present
             - name: start docker
               service:
                 name: docker
                 status: started
                 enabled: yes
      
      1. Run this playbook and check if it is successful

      2. Let ssh to one of the servers and check the status of docker

        We can see that docker is successfully installed and running.