🧩 ANSIBLE COMPLETE REFERENCE GUIDE

🧭 Ansible Complete Reference Guide

Author’s Note:
This guide is built from a real automation setup using Ansible for managing Proxmox and Terraform-created environments.
It is written in a narrative, KB-style tone to serve as both a learning and reference resource.


βš™οΈ Introduction: Why Ansible

Ansible is an open-source configuration management and automation tool developed by Red Hat.
It automates software provisioning, configuration, and orchestration across servers β€” all using simple YAML playbooks.

Key benefits:

πŸ’‘ Tip: Ansible is most powerful when combined with Terraform β€” Terraform provisions infrastructure, and Ansible configures it.


🧰 Installing Ansible

On Ubuntu / Debian

sudo apt update && sudo apt install ansible -y

Verify installation:

ansible --version
ansible-config list
ansible-doc -l

Recommended Directory Layout

/home/automation/ansible/
β”œβ”€β”€ ansible.cfg
β”œβ”€β”€ inventory.ini
β”œβ”€β”€ playbooks/
β”‚   └── site.yml
└── roles/

πŸ’‘ Keep your playbooks, inventory, and roles neatly separated. This structure scales well.


🧱 Understanding Core Components

Component Description
Inventory List of target machines
Playbook YAML file defining automation tasks
Module Unit of work (e.g., apt, copy, user)
Role Reusable, organized structure of tasks and files
Handler Executes actions after a change (e.g., restart a service)

Example Inventory:

[web]
10.0.4.10
10.0.4.11

[db]
10.0.4.20

Example Task:

- name: Update system packages
  apt:
    update_cache: yes
    upgrade: dist

βš™οΈ Ansible Configuration File (ansible.cfg)

Example configuration:

[defaults]
inventory = /home/automation/ansible/inventory.ini
remote_user = automation
host_key_checking = False
retry_files_enabled = False
forks = 10
timeout = 30

[ssh_connection]
pipelining = True
control_path = ~/.ssh/ansible-%%r@%%h:%%p

Useful commands:

ansible-config view
ansible-config dump --only-changed

πŸ’Ύ Inventory Management

Static Inventory Example

[web]
10.0.4.10 ansible_user=automation ansible_ssh_private_key_file=~/.ssh/id_rsa

[db]
10.0.4.20

Commands for Inventory

ansible-inventory --list
ansible all --list-hosts
ansible-inventory --graph

πŸ’‘ Tip: Terraform can dynamically generate inventory for Ansible using outputs.


🧰 Common Ad-Hoc Commands

Purpose Command
Test connectivity ansible all -m ping
Check uptime ansible all -a "uptime"
Copy files ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts"
Run commands as root ansible all -b -a "apt update"
Gather facts ansible all -m setup
Manage users ansible all -m user -a "name=test state=present"

🧾 Writing Playbooks

Example Playbook:

---
- name: Initialize new containers
  hosts: all
  become: yes

  tasks:
    - name: Update and upgrade system
      apt:
        update_cache: yes
        upgrade: dist

    - name: Install connectivity tools
      apt:
        name:
          - traceroute
          - curl
          - net-tools
        state: present

    - name: Create user
      user:
        name: sony
        password: "{{ 'password' | password_hash('sha512') }}"
        groups: sudo
        create_home: yes

Run the playbook:

ansible-playbook playbooks/site.yml

🧩 Roles and Reusability

Structure of a role:

roles/
└── webserver/
    β”œβ”€β”€ tasks/main.yml
    β”œβ”€β”€ handlers/main.yml
    β”œβ”€β”€ vars/main.yml
    β”œβ”€β”€ templates/index.html.j2
    └── meta/main.yml

Initialize a new role:

ansible-galaxy init roles/webserver

Install community roles:

ansible-galaxy install geerlingguy.nginx
ansible-galaxy list

βš™οΈ Variables and Templates

Define variables:

vars:
  app_port: 8080

Template example (nginx.conf.j2):

server {
    listen {{ app_port }};
}

Apply template:

ansible all -m template -a "src=nginx.conf.j2 dest=/etc/nginx/sites-available/default"

🧠 Tags, Loops, and Conditionals

Loops

- name: Install multiple packages
  apt:
    name: "{{ item }}"
    state: present
  loop:
    - curl
    - vim
    - net-tools

Tags

- name: Install NGINX
  apt:
    name: nginx
    state: present
  tags: [install, web]

Run specific tags:

ansible-playbook site.yml --tags "install"

Conditionals

- name: Restart service only if Ubuntu
  service:
    name: nginx
    state: restarted
  when: ansible_distribution == "Ubuntu"

πŸ”§ Handlers and Notifications

tasks:
  - name: Update nginx config
    template:
      src: nginx.conf.j2
      dest: /etc/nginx/nginx.conf
    notify:
      - Restart nginx

handlers:
  - name: Restart nginx
    service:
      name: nginx
      state: restarted

🧾 Ansible Vault (Secrets Management)

Encrypt sensitive files:

ansible-vault create secrets.yml
ansible-vault encrypt playbook.yml
ansible-vault decrypt playbook.yml

Run with password prompt:

ansible-playbook play.yml --ask-vault-pass

πŸ’‘ Use --vault-password-file ~/.vault_pass.txt for automation.


🧩 Facts and Filters

Collect system facts:

ansible all -m setup

Filter facts:

ansible all -m setup -a "filter=ansible_distribution"

Example Jinja2 filter:

{{ ansible_hostname | upper }}

βš™οΈ Debugging & Troubleshooting

Command Description
ansible-playbook -vvv play.yml Verbose mode
ansible-playbook --step play.yml Step-by-step run
ansible-playbook --check play.yml Dry-run mode
ansible -m ping --limit web Target limited hosts
ANSIBLE_KEEP_REMOTE_FILES=1 ansible-playbook play.yml Keep temp debug files

πŸ’‘ Add --diff to preview file changes before applying.


🧾 Ansible Command Cheat Sheet

Category Command Description
Ping ansible all -m ping Test connectivity
Run Playbook ansible-playbook site.yml Execute playbook
List Hosts ansible all --list-hosts Show hosts
Facts ansible all -m setup Gather facts
Vault ansible-vault encrypt file.yml Encrypt sensitive files
Lint ansible-lint play.yml Syntax check
Check Mode ansible-playbook play.yml --check Dry run
Tags ansible-playbook site.yml --tags install Filter tasks
Limit ansible-playbook site.yml --limit web Run specific hosts

βœ… Best Practices


🧠 Conclusion

β€œAnsible is the configuration engine that turns your servers into ready systems.”

Combined with Terraform and Proxmox, it completes your end-to-end automation cycle:
Terraform provisions β†’ Ansible configures β†’ Your environment runs seamlessly.

Use this as a reference, a command companion, and a foundation for scaling your automation environment.


End of Guide β€” Ansible Complete Reference