All posts

Getting Started with Ansible Automation

Introduction to Ansible #

Ansible is an open-source automation platform that simplifies configuration management, application deployment, and task automation. It uses a simple YAML-based language (YAML Ain’t Markup Language) to describe automation jobs in a way that approaches plain English.

Why Use Ansible? #

  • Agentless: No need to install any additional software on the nodes you want to automate
  • Idempotent: Can be run multiple times without side effects
  • Simple: Uses YAML syntax (called Playbooks) which are easy to write and understand
  • Powerful: Can manage complex deployments and orchestrate entire application lifecycles
  • Extensible: Large collection of built-in modules and the ability to create custom ones

Basic Ansible Concepts #

Inventory #

Ansible works against multiple systems in your infrastructure at the same time. It does this by selecting portions of systems listed in your inventory file (by default located at /etc/ansible/hosts).

Example inventory file:

[webservers]
web1.example.com
web2.example.com

[database]
db1.example.com

Playbooks #

Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.

Example playbook (webserver.yml):

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

Modules #

Modules are the units of code Ansible executes. Each module is mostly standalone and can be written in any language that can return JSON.

Common modules include:

  • apt/yum: Package management
  • copy: Copy files to remote locations
  • file: Set attributes of files, symlinks, and directories
  • service: Manage services
  • template: Template a file out to a remote server

Getting Started #

  1. Installation (on Ubuntu/Debian):

    sudo apt update
    sudo apt install -y ansible
    
  2. Verify Installation:

    ansible --version
    
  3. Test Connection:

    ansible all -i inventory.ini -m ping
    

Best Practices #

  1. Use Roles: Organize your playbooks into reusable components
  2. Use Variables: Make your playbooks more flexible with variables
  3. Use Vault: Store sensitive data using Ansible Vault
  4. Tag Your Tasks: Makes it easier to run specific parts of a playbook
  5. Documentation: Always document your playbooks and roles

Conclusion #

Ansible provides a simple yet powerful way to automate IT infrastructure. Its agentless architecture and easy-to-understand YAML syntax make it accessible for beginners while being powerful enough for complex automation tasks.

Further Reading #