My Learning Blog

Basic About Ansible - 9/18/2024

automation tool for Devops

Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It enables IT administrators to automate repetitive tasks across multiple servers or systems. Ansible operates by using a simple language called YAML to define automation jobs in “playbooks.” It doesn’t require installing agents on the target systems, relying instead on SSH or Windows Remote Management (WinRM) for communication.

Key features of Ansible include:

Summary of an Ansible Workflow:

Install and configure Ansible on the control node. Create an inventory of hosts to manage. Write playbooks defining tasks to be performed. Run the playbook on target hosts. Review output to monitor success and changes. Handle errors and troubleshoot if necessary. Modularize with roles for better organization. Integrate into CI/CD pipelines to automate workflows at scale.

Playbook

In Ansible, a playbook is a YAML file that contains a series of instructions or “plays” to be executed on remote systems. It defines tasks such as installing software, configuring services, or managing files across a set of machines.

You can think of a playbook as a set of tasks, and each task uses a module to perform specific actions. A playbook groups these tasks together to define a complete workflow or process that you want to automate. Each task in a playbook uses a module to perform a specific function.

In a more structured way:

In summary, a playbook = 1 or more Plays,define the full automation workflow, and it orchestrates the execution of tasks (using modules) on specified hosts, which describe:

“At what time”

In Ansible refers to the conditions or events that determine when a task is executed, not real-time scheduling.

You control when tasks run using:

In these ways, Ansible manages timing through logic and events, controlling the flow of automation based on system states or conditions.

module - very granular and specific

A module is a pre-built, reusable piece of code that performs a specific task or action on a managed node.

Modules are at the heart of Ansible automation, enabling you to define specific tasks in your playbooks without needing to write custom scripts for each operation.

- name: Install and Configure Apache
  hosts: webservers
  become: yes
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present

    - name: Ensure Apache is started
      service:
        name: apache2
        state: started

    - name: Copy custom configuration
      copy:
        src: /path/to/local/apache2.conf # Ensure the path matches the correct Apache config file
        dest: /etc/apache2/apache2.conf # Update this to match the correct destination path
        owner: root
        group: root
        mode: "0644"

In this playbook:

hosts - on which the tasks will be executed