๐Ÿ“˜ Ansible Summary Guide

๐Ÿ“˜ Ansible Summary Guide


I. What is Ansible?

Ansible is an open-source automation tool used for:

  • Configuration management

  • Application deployment

  • Provisioning

  • Orchestration
    It is agentless, using SSH (or WinRM) to manage remote systems.


II. Core Concepts

  1. Playbook
    A YAML file that defines a set of tasks to be executed on hosts. It's the main way you describe automation in Ansible.

  2. Inventory
    A list of managed nodes (hosts), defined in a file (hosts), INI, YAML, or dynamically (e.g., via AWS).

  3. Module
    A unit of work. Ansible ships with hundreds of modules (e.g., yum, apt, copy, template, user, service, etc.).

  4. Task
    A single action to be executed (e.g., install a package, start a service).

  5. Role
    A standardized way to organize tasks, handlers, files, variables, etc. Reusable and modular.

  6. Handler
    Special tasks triggered only by a notify from another task (e.g., restart nginx if config changes).

  7. Facts
    System information (gathered by setup module) about a host (OS, IP, memory, etc.).


III. Ansible Architecture


  1. Control Node
    The machine where Ansible is installed and run from. It connects to managed nodes.

  2. Managed Nodes
    The remote systems (Linux, Windows, etc.) that are configured and managed.

  3. Connection Methods

    • SSH (default): For Linux/Unix nodes.

    • WinRM: For Windows nodes.

  4. Inventory File Example

    ini
    [webservers]
    server1 ansible_host=192.168.1.10 server2 ansible_host=192.168.1.11

IV. Playbook Example

yaml

- name: Install and start Apache hosts: webservers become: yes tasks: - name: Install Apache apt: name: apache2 state: present - name: Start Apache service: name: apache2 state: started enabled: yes

V. Important Directories (in Roles)


markdown

roles/ webserver/ tasks/ handlers/ files/ templates/ vars/ defaults/ meta/

VI. Command Line Usage

CommandDescription
ansibleRun ad-hoc commands.
ansible-playbookRun playbooks.
ansible-galaxyManage roles from Ansible Galaxy.
ansible-vaultEncrypt/decrypt sensitive data.

Examples:
bash

ansible all -m ping # Ad-hoc ping test ansible-playbook site.yml # Run a playbook ansible-playbook --check site.yml # Dry-run (no changes) ansible-vault encrypt secrets.yml # Encrypt a file

VII. Variables in Ansible

  • Defined in playbooks, inventory, roles, or extra vars.

  • Can be encrypted with Vault.

  • Precedence: CLI > Playbook > Role vars > Inventory vars > Facts > Defaults


VIII. Ansible Galaxy

A public hub for sharing Ansible roles.

bash

ansible-galaxy install geerlingguy.nginx

IX. Error Handling and Debugging

  • Use ignore_errors: yes to skip errors.

  • Use register and debug to capture and print variable data.

yaml

- name: Check disk shell: df -h register: disk_output - debug: var: disk_output.stdout_lines

X. Best Practices

  1. Use roles to organize reusable automation code.

  2. Use Ansible Vault for secrets.

  3. Maintain separate inventories for staging, production.

  4. Always run playbooks in check mode before applying.

  5. Use handlers for idempotent service restarts.

  6. Test playbooks with Molecule or Vagrant.

  7. Keep your playbooks idempotent (running twice = same result).



Comments

Popular posts from this blog

SAVE TAX ๐Ÿ’ต

LIFE A JOURNEY

๐ŸฆŸ The Truth About Mosquitoes: More Than Just an Itchy Bite