Skip to main content

Beginner's Guide to Ansible

·404 words·2 mins· loading · loading · · ·
Table of Contents

Introduction
#

Ansible is an agentless automation tool that uses simple YAML playbooks and SSH to manage configuration, deployment, and orchestration across machines. It’s easy to start with, scales from a laptop to hundreds of servers, and is ideal for teams that prefer readable, idempotent automation over complex agent-based systems.

Core concepts you need to know
#

  • Control node: where you run ansible/ansible-playbook (your workstation or CI runner).
  • Managed nodes: target servers reachable via SSH.
  • Inventory: a file (INI or YAML) listing hosts and groups.
  • Playbook: a YAML file describing ordered tasks to reach a desired state.
  • Module: a unit of work (apt, yum, service, template, copy, etc.).
  • Role: reusable collection of tasks, defaults, handlers, and templates.

These map to everyday tasks: ensure packages are installed, services running, configs templated, and files synchronized.

Quick example: install and enable nginx
#

A minimal playbook that’s idempotent (re-running it won’t change state unnecessarily):

- name: Install and start nginx on web nodes
  hosts: web
  become: true

  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
        update_cache: yes

    - name: Ensure nginx is running
      service:
        name: nginx
        state: started
        enabled: true

Run it:

ansible-playbook -i inventory.ini site.yml

Set up SSH key-based access to avoid password prompts and keep automation non-interactive.

Best practices to adopt early
#

  • Use modules, not raw shell, when possible—modules are idempotent and clearer.
  • Store playbooks in Git and review changes via pull requests.
  • Use Ansible Vault for secrets; never commit plaintext credentials.
  • Structure larger projects with roles (roles/role_name/{tasks,templates,vars}).
  • Lint playbooks with ansible-lint and test roles with Molecule in CI.

Scaling and safety tips
#

  • Run in --check mode to preview changes before applying.
  • Limit parallelism (-f) to avoid overloading targets.
  • Use inventories per environment (prod/staging/dev) and group vars for environment-specific data.
  • Prefer templates (Jinja2) for config files to keep things DRY and parameterized.

When to combine tools
#

Use Terraform for provisioning cloud resources and Ansible for configuration; connect them in CI/CD pipelines. For continuous enforcement at scale, combine Ansible with orchestration or a management agent only where needed.

Conclusion
#

Start by automating one repeatable task: package install, user creation, or config templating. Keep playbooks idempotent, secrets encrypted, and code in Git. As your needs grow, adopt roles, CI testing, and integrate with provisioning tools like Terraform. Next step: create a role for your web app, add ansible-lint and a simple Molecule test, and iterate.

Co-authored by Vishwakarma, Deeps 2nd Brain

Deep Jiwan
Author
Deep Jiwan
Building hacky solutions that save time and make my life easier. Not too sure about yours :)

Related