ansible/ansible · error · AnsibleOptionsError
Pattern must be valid group name when using --graph
Error message
Pattern must be valid group name when using --graph
What it means
`inventory_graph` looks up `context.CLIARGS['pattern']` (positional args, defaulting to 'all') via `self._get_group(...)`. If the pattern does not resolve to an existing inventory group, it raises `AnsibleOptionsError` because `--graph` can only draw a tree rooted at a group.
Source
Thrown at lib/ansible/cli/inventory.py:262
if group.name != 'all':
for host in group.hosts:
result.append(self._graph_name(host.name, depth))
if context.CLIARGS['show_vars']:
result.extend(self._show_vars(self._get_host_variables(host), depth + 1))
if context.CLIARGS['show_vars']:
result.extend(self._show_vars(self._get_group_variables(group), depth))
return result
def inventory_graph(self):
start_at = self._get_group(context.CLIARGS['pattern'])
if start_at:
return '\n'.join(self._graph_group(start_at))
else:
raise AnsibleOptionsError("Pattern must be valid group name when using --graph")
def json_inventory(self, top):
seen_groups = set()
def format_group(group, available_hosts):
results = {}
results[group.name] = {}
if group.name != 'all':
results[group.name]['hosts'] = [h.name for h in group.hosts if h.name in available_hosts]
results[group.name]['children'] = []
for subgroup in group.child_groups:
results[group.name]['children'].append(subgroup.name)
if subgroup.name not in seen_groups:
results.update(format_group(subgroup, available_hosts))
seen_groups.add(subgroup.name)
if context.CLIARGS['export']:
results[group.name]['vars'] = self._get_group_variables(group)View on GitHub (pinned to 9cf16a4aca)
Solutions
- Run `ansible-inventory -i inv.yml --list` to see valid group names, then use one of them as the pattern.
- Use a host name only with `--host`, never with `--graph`; for the full tree run `ansible-inventory --graph` with no pattern (defaults to 'all').
- Confirm the inventory source actually defines the group (check `-i` argument and inventory plugin output).
Example fix
# before ansible-inventory -i inv.yml --graph web1 # after ansible-inventory -i inv.yml --graph webservers
Defensive patterns
Strategy: validation
Validate before calling
from ansible.inventory.manager import InventoryManager
inv = InventoryManager(loader, sources='inv.yml')
assert 'webservers' in inv.groups, f'{pattern!r} is not a group in this inventory' Prevention
- Use exact group names with --graph; host names belong to --host.
- Run --list once to enumerate valid groups when scripting graph output.
When it happens
Trigger: `ansible-inventory --graph -i inv.yml nonexistent_group`; passing a host name as the pattern (`--graph web1`); a pattern with wildcards, since `_get_group` does exact group-name matching.
Common situations: Assuming `--graph` accepts host names or patterns like `--host` does; group names that differ from what is defined (e.g. expecting `ungrouped` variants or dynamically-named groups); empty inventory where even 'all' has no group object.
Related errors
- The group named '%s' has a recursive dependency loop.
- You must pass a single valid host to --host parameter
- Could not find group %s in inventory
- %s is not a known host nor group
- %s is not a known group
AI-assisted analysis of ansible/ansible@9cf16a4aca (2026-08-15).
Data as JSON: /api/errors/7dd61392ea4e4299.
Report an issue: GitHub.