DevOps Classroom notes 15/Jun/2026

Inventories

  • Official Docs
  • Inventory contains a list of servers/systems to connect to.
  • Inventory can be written in two formats:
    • INI format (hosts file)
    • YAML format
  • Inventory allows us to group servers.
  • By default, Ansible uses all to represent all hosts in the inventory file.
  • The default inventory file location is /etc/ansible/hosts.

Example 1 — Basic Inventory (No Groups)

INI format:

10.100.0.11
10.100.0.12
10.100.0.13
10.100.0.14
10.100.0.15
10.100.0.16
10.100.0.17
10.100.0.18
10.100.0.19
10.100.0.10

YAML format:

all:
  hosts:
    10.100.0.11:
    10.100.0.12:
    10.100.0.13:
    10.100.0.14:
    10.100.0.15:
    10.100.0.16:
    10.100.0.17:
    10.100.0.18:
    10.100.0.19:
    10.100.0.10:

Example 2 — Inventory with Groups

INI format:

[webservers]
10.100.0.11
10.100.0.12
10.100.0.13
10.100.0.14
10.100.0.15
10.100.0.16

[dbservers]
10.100.0.17
10.100.0.18
10.100.0.19
10.100.0.10

YAML format:

all:
  children:
    webservers:
      hosts:
        10.100.0.11:
        10.100.0.12:
        10.100.0.13:
        10.100.0.14:
        10.100.0.15:
        10.100.0.16:
    dbservers:
      hosts:
        10.100.0.17:
        10.100.0.18:
        10.100.0.19:
        10.100.0.10:

Example 3 — Range Pattern

[webservers]
www[01:06].example.com

Static vs Dynamic Inventories

  • The above examples are static inventories.
  • Inventories can also be dynamic, which means they point to:
    • An executable (script) that generates a list of servers in a specific JSON format.
    • Ansible plugins to fetch servers from cloud providers like AWS, Azure, GCP.

Ansible Debug Messages

Simple Playbook (without debug)

---
- name: sample playbook
  hosts: all
  become: yes
  tasks:
    - name: update packages and install tree
      ansible.builtin.apt:
        name: tree
        update_cache: yes
        state: present

Playbook with Explicit Debug Messages

Using the debug module:

---
- name: sample playbook
  hosts: all
  become: yes
  tasks:
    - ansible.builtin.debug:
        msg: "Installing tree package"
    - name: update packages and install tree
      ansible.builtin.apt:
        name: tree
        update_cache: yes
        state: present
    - ansible.builtin.debug:
        msg: "Installed tree package"

Ansible also supports printing verbose information — use the -v, -vv, -vvv, or -vvvv flags with ansible-playbook for increasing levels of verbosity.


Bailout / Failing an Ansible Playbook

  • To intentionally fail a playbook, use the fail module.
---
- name: sample bailout playbook
  hosts: all
  become: yes
  tasks:
    - ansible.builtin.fail:
        msg: "Under development"

Leave a comment

Turn on JavaScript to leave a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company
Exit mobile version
%%footer%%