Ansible Playbooks

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

  • Write an ansible playbook to create a new user.

  • Write an ansible playbook to install docker on a group of servers

Ansible playbook to create a file on a different server

Create a file named create_file.yml . Use the vim create_file.yml command.

---
- name: create a file
  hosts: all
  become: yes

  tasks:
    - name: create a new file
      file:
        path: /home/ubuntu/demo.txt
        state: touch

Run the playbook with ansible-playbook create_file.yml command.

Verify that the file has been successfully created.

Ansible playbook to create a new user

Create a file named create_user.yml . Use the vim create_user.yml command.

---
- name: create a user using ansible playbook
  hosts: all
  become: yes

  tasks:
    - name: create a user
      user:
        name: ronke

Run the playbook with ansible-playbook create_user.yml command.

Verify that the user has been successfully created.

Ansible playbook to install docker on a group of servers

Create a file named install_docker.yml . Use the vim docker_install.yml command.

---
- name: Install Docker on a group of servers
  hosts: all
  become: yes
  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

Run the playbook with ansible-playbook create_user.yml command.

Verify Docker installation.

Writing ansible playbooks with the best practices.

  1. Naming: Use meaningful and descriptive names for roles, tasks, and variables for easy identification and understanding.

  2. Use YAML Syntax Properly: Follow consistent indentation rules using spaces or tabs for readability. For easy comprehension, use clear and descriptive names for playbooks, roles, tasks, and variables.

  3. Organize Playbooks and Roles: Structure playbooks logically into distinct sections for clear understanding. Divide tasks into roles based on functionality or purpose for code reusability. Utilize directories like tasks, handlers, templates, and vars within roles for better organization.

  4. Idempotency: Ensure your playbooks are idempotent, meaning they can be executed multiple times without causing unintended or irreversible changes. Utilize Ansible modules and tasks that support idempotent operations, such as modifying configurations without introducing inconsistencies or disrupting existing states.

  5. Variable Usage: Define variables in separate YAML files for better organization and management. Use meaningful variable names for enhanced code readability and understanding.