ansible.builtin.inventory_hostnames lookup – list of inventory hosts matching a host pattern

Note

This lookup plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name inventory_hostnames even without specifying the collections: keyword. However, we recommend you use the FQCN for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same lookup plugin name.

New in Ansible 1.3

Synopsis

  • This lookup understands ‘host patterns’ as used by the hosts: keyword in plays and can return a list of matching hosts from inventory

Notes

Note

  • this is only worth for ‘hostname patterns’ it is easier to loop over the group/group_names variables otherwise.

Examples

- name: show all the hosts matching the pattern, i.e. all but the group www
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_inventory_hostnames:
    - all:!www

Return Value

Key

Description

Return value

list / elements=string

list of hostnames that matched the host pattern in inventory

Returned: success

Authors

  • Michael DeHaan

  • Steven Dossett

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.