BacklogZero®

Ansible in a Minute · Ansible Core ·

Use loop over inventory hosts

Short answer

Use loop with groups['all'] or ansible_play_batch so one task iterates inventory hostnames.

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

Request

print every hostname in groups all with loop

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
- name: Show all hosts in the inventory
  hosts: all
  gather_facts: false
  tasks:
    - name: Show all the hosts in the inventory
      ansible.builtin.debug:
        msg: "{{ item }}"
      loop: "{{ groups['all'] }}"

How it works

The play already targets hosts. Use loop with groups['all'] when one task must walk inventory names. Use ansible_play_batch for hosts in the current play batch only.

When to use it

  • Building a list of peer hostnames from inventory
  • Running one task against a host pattern that is not the play target

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-09-22

Notes

This task runs on every play host and loops the inventory each time unless you add run_once. Use loop: "{{ ansible_play_batch }}" for the current batch. Use query('inventory_hostnames', 'all:!www') when you need a host pattern.

Reference

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today

Related guides

Back to Ansible in a Minute

Use loop over inventory hosts · Level Up Labs