Day 58: Ansible Playbooks

Day 58: 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.

Task-01

  • Write an ansible playbook to create a file on a different server
---
- name: Create file in server
  hosts: all
  become : true
  tasks:
    - name: Create a file
      file:
        path: /home/ubuntu/file.txt
        state: touch
ansible-playbook file-name.yml -i <inventory-file-path>

Verify that the file has been created on different servers

ansible all -a "ls /home/ubuntu" -i <inventory-file>

  • Write an ansible playbook to create a new user.
---
  - name: Playbook
    hosts: all
    become: yes
    become_user: root
    tasks:
      - name: add list
        user: name='{{item}}' state=present
        with_items:
                - sanjana
                - Sayali
ansible-playbook file-name.yml -i <inventory-file-path>

To verify if the user has been created, look for the user’s name in the /etc/passwd file.” in server 1

 ansible server1  -a "cat /etc/passwd" -i <inventory-file-path>

  • Write an ansible playbook to install docker on a group of servers
---
- 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
ansible-playbook file-name.yml -i <inventory-file-path>

Verify that Docker has been installed on multiple servers.

ansible all -a "docker --version" -i <inventory-path>

Watch this video to learn about ansible Playbooks

Task-02

  • Write a blog about writing ansible playbooks with the best practices.

Ansible playbook is a set of instructions that define a series of tasks that need to be performed on one or more remote hosts. It is a configuration management tool that automates the deployment and management of applications and services on a large number of hosts simultaneously.

A playbook is written in YAML format and consists of one or more plays, each containing a set of tasks that are executed sequentially on a specific set of hosts. Each task in a playbook is a discrete action, such as installing a package, copying a file, or starting a service.

Some of the key features of Ansible playbook include:

  • idempotent: Playbooks are idempotent, meaning that they can be run multiple times without causing any adverse effects. If the system is already in the desired state, Ansible will not make any changes.

  • modular: Playbooks are modular, allowing administrators to create reusable components that can be used across different playbooks.

  • agentless: Ansible does not require any agent or software to be installed on the remote host, making it easy to use and deploy.

  • flexible: Playbooks can be customized to meet the specific needs of an organization, allowing administrators to automate tasks that are unique to their environment.

when and with are two powerful control structures in Ansible that allow you to perform conditional operations and loop over lists or dictionaries, respectively.

Here are some examples of how you can use when and with in your Ansible playbooks: - name: Install nginx on webservers hosts: all become: true tasks: - name: Install nginx apt: name: nginx state: present when: ansible_distribution == "Debian"

In this example, the apt module will only be executed if the ansible_distribution variable is set to "Debian”. Otherwise, the task will be skipped.

Example using with:

Suppose you have a playbook that needs to create multiple users. You can use the with_items keyword to loop over a list of users and execute the task for each user.

- name: Create users
  user:
    name: "{{ item }}"
    state: present
  with_items:
    - user1
    - user2