{"record":{"id":"7dd61392ea4e4299","repo":"ansible/ansible","slug":"pattern-must-be-valid-group-name-when-using-grap","errorCode":null,"errorMessage":"Pattern must be valid group name when using --graph","messagePattern":"Pattern must be valid group name when using --graph","errorType":"exception","errorClass":"AnsibleOptionsError","httpStatus":null,"severity":"error","filePath":"lib/ansible/cli/inventory.py","lineNumber":262,"sourceCode":"\n        if group.name != 'all':\n            for host in group.hosts:\n                result.append(self._graph_name(host.name, depth))\n                if context.CLIARGS['show_vars']:\n                    result.extend(self._show_vars(self._get_host_variables(host), depth + 1))\n\n        if context.CLIARGS['show_vars']:\n            result.extend(self._show_vars(self._get_group_variables(group), depth))\n\n        return result\n\n    def inventory_graph(self):\n\n        start_at = self._get_group(context.CLIARGS['pattern'])\n        if start_at:\n            return '\\n'.join(self._graph_group(start_at))\n        else:\n            raise AnsibleOptionsError(\"Pattern must be valid group name when using --graph\")\n\n    def json_inventory(self, top):\n\n        seen_groups = set()\n\n        def format_group(group, available_hosts):\n            results = {}\n            results[group.name] = {}\n            if group.name != 'all':\n                results[group.name]['hosts'] = [h.name for h in group.hosts if h.name in available_hosts]\n            results[group.name]['children'] = []\n            for subgroup in group.child_groups:\n                results[group.name]['children'].append(subgroup.name)\n                if subgroup.name not in seen_groups:\n                    results.update(format_group(subgroup, available_hosts))\n                    seen_groups.add(subgroup.name)\n            if context.CLIARGS['export']:\n                results[group.name]['vars'] = self._get_group_variables(group)","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/ansible/ansible/blob/9cf16a4aca7898481c257f1e17ad28d0b67b1f85/lib/ansible/cli/inventory.py#L244-L280","documentation":"`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.","triggerScenarios":"`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.","commonSituations":"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.","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)."],"exampleFix":"# before\nansible-inventory -i inv.yml --graph web1\n\n# after\nansible-inventory -i inv.yml --graph webservers","handlingStrategy":"validation","validationCode":"from ansible.inventory.manager import InventoryManager\ninv = InventoryManager(loader, sources='inv.yml')\nassert 'webservers' in inv.groups, f'{pattern!r} is not a group in this inventory'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use exact group names with --graph; host names belong to --host.","Run --list once to enumerate valid groups when scripting graph output."],"tags":["ansible-inventory","inventory","graph","groups"],"backgroundTag":null,"analyzedSha":"9cf16a4aca7898481c257f1e17ad28d0b67b1f85","analyzedAt":"2026-08-15T00:15:47.100Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}