Ansible Playbooks

Ansible Playbooks

Day 57, Day 58

Table of contents

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

Ansible playbook that creates a file on a different server:

---
- name: Create File on Remote Server
  hosts: demo-servers
  become: true
  tasks:
    - name: Create file
      file:
        path: /home/ubuntu/test.txt
        state: touch

In this playbook:

  • demo-servers represents the hostname or group name of the target server where you want to create the file. Replace it with the actual hostname or group name defined in your inventory file.

  • The become: true the option allows the playbook to execute tasks with administrative privileges (sudo).

  • The file module is used to create the file. The path the parameter specifies the file path and name, and the state: touch option ensures the file is created if it doesn't exist.

Save the playbook in a YAML file, e.g., create_file.yaml, and then run it using the ansible-playbook command:

ansible-playbook -i /path/to/inventory/file create_file.yaml

Replace /path/to/inventory/file with the actual path to your Ansible inventory file.

When you run the playbook, it will connect to the specified server and create the file at the specified path with the specified ownership and permissions.

Write an Ansible playbook to create a new user.

Ansible playbook to create a new user:

---
- name: Create User
  hosts: demo-servers
  become: true
  tasks:
    - name: Create user
      user:
        name: Dwarika
        state: present

In this playbook:

  • demo-servers represents the hostname or group name of the target server where you want to create the user. Replace it with the actual hostname or group name defined in your inventory file.

  • become: true option allows the playbook to execute tasks with administrative privileges (sudo).

  • user module is used to create the user. The name parameter specifies the username of the new user, and the state: present option ensures the user is created if it doesn't exist.

Save the playbook in a YAML file, e.g., create_user.yaml and then run it using the ansible-playbook command:

ansible-playbook -i /path/to/inventory/file create_user.yaml

Replace /path/to/inventory/file with the actual path to your Ansible inventory file.

When you run the playbook, it will connect to the specified server and create a new user with the specified properties.

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

Ansible playbook to install Docker on a group of servers:

---
 name: Install Docker
 hosts: demo-servers
 become: yes

 tasks:
   - name: install docker
     apt:
       name: docker.io
       state: latest

In this playbook:

  • demo_servers represents the group name of the servers where you want to install Docker. Replace it with the actual group name defined in your inventory file.

  • The become: true option allows the playbook to execute tasks with administrative privileges (sudo).

  • apt module is used to install packages using the APT package manager.

  • name: docker.io specifies the package name to be installed. In this case, it's docker.io.

  • state: latest ensures that the package is installed and updated to the latest version.

Save the playbook in a YAML file, e.g., install_docker.yaml, and then run it using the ansible-playbook command:

ansible-playbook -i /path/to/inventory/file install_docker.yaml

Replace /path/to/inventory/file with the actual path to your Ansible inventory file.

When you run the playbook, it will connect to the servers in the docker_servers group and install Docker on each server.

Note: Update node using ansible all -i inventory_file -m apt -a "upgrade=yes update_cache=yes" --become this command

Verify that Docker has been installed on multiple servers.

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

Task-02

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

Ansible is a powerful open-source automation tool that allows you to automate IT infrastructure management. Ansible playbooks are the configuration files that describe the desired state of your systems. Writing Ansible playbooks with best practices can make your automation more robust, reliable, and scalable. In this blog, we will discuss some best practices for writing Ansible playbooks.

Use a clear structure:

Make sure your playbook is organized and easy to read. Use comments and section headers to explain what each part of the playbook does. Avoid nesting too many tasks or plays inside each other, as it can make the playbook difficult to read and maintain.

Use idempotent tasks:

Idempotency is one of the key principles of Ansible. It means that a task should be designed in such a way that it can be executed repeatedly without changing the state of the system. This makes your playbook more robust and prevents unexpected changes to the system. You can achieve idempotency by using modules that are designed to be idempotent, such as apt, yum, and copy.

Example:

- name: Install Apache web server
  apt:
    name: apache2
    state: latest

In this example, the apt module will only install the Apache web server if it is not already installed or if there is a newer version available.

Use variables:

Variables can make your playbook more flexible and reusable. They allow you to store values that can be used across multiple tasks and playbooks. You can define variables in the playbook itself or in separate files, such as group_vars or host_vars.

Example:

- name: Install Nginx web server
  apt:
    name: nginx
    state: latest

- name: Configure Nginx web server
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
  vars:
    nginx_port: 80

In this example, we are using a variable nginx_port to define the port number used by Nginx. The variable is defined in the vars section of the template task.

Use roles:

Roles are a way to organize your tasks, variables, and files into reusable components. They can be used across multiple playbooks and provide a way to share functionality between different teams. Roles can also make your playbook more modular and easier to maintain.

Example:

- name: Install web serve
  hosts: all
  roles:
    - webserver

In this example, we are using a role called web server to install the web server on all groups of hosts.

Use tags:

Tags allow you to selectively run specific tasks or groups of tasks within a playbook. This can be useful when testing or debugging your playbook or when you need to run a subset of tasks. Tags can be added to individual tasks or to entire plays.

Example:

- name: Install web serve
  hosts: all
  become: yes
  tags:
    - webserver


  tasks:
    - name: Install Apache web server
      apt:
        name: apache2
        state: latest
      tags:
        - apache


    - name: Install Nginx web server
      apt:
        name: nginx
        state: latest
      tags:
        - nginx

In this example, we are using tags to selectively run the Apache or Nginx installation tasks.

Use conditionals:

Conditionals allow you to execute a task only if a specific condition is met. This can be useful when you need to run a task only on a subset of hosts or when you need to run a task only if a specific variable is defined.

Example:

---
- name: Example playbook with a conditional
  hosts: all
  vars:
    myvar: "tmp"
  tasks:
    - name: Task 1
      debug:
        msg: "This task will always run"

    - name: Task 2
      debug:
        msg: "This task will run if myvar equals 'tmp'"
      when: myvar == "tmp"

In this example, the playbook has two tasks. Task 1 will always run because it doesn't have a conditional attached to it. Task 2 will only run if the variable myvar is equal to "tmp". If myvar has any other value, Task 2 will be skipped.

Thankyou.....