# Ansible-inventory and --limit

**URL:** <https://forum.ansible.com/t/ansible-inventory-and-limit/45004>\
**Category:** Get Help\
**Created:** [December 16, 2025, 1:27am UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004 "2025-12-16T01:27:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toni](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Toni](https://forum.ansible.com/u/Toni)\
**Post date:** [December 16, 2025, 1:27am UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/1 "2025-12-16T01:27:53Z")

</div>

I’m trying to list a part of my inventory like this:

```auto
$ ansible-inventory --limit group --list

```

I’ve also tried

```auto
$ ansible-inventory --list group

```

But I get the entire inventory spewed out to me. This is unfortunately consistent with the help message:

```auto
$ ansible-inventory -i ./inventories/development/inventory --list wireguard --help
usage: ansible-inventory [-h] [--version] [-v] [-i INVENTORY] [-l SUBSET] [--flush-cache] [--vault-id VAULT_IDS] [-J |
                         --vault-password-file VAULT_PASSWORD_FILES] [--playbook-dir BASEDIR] [-e EXTRA_VARS] [--list]
                         [--host HOST] [--graph] [-y] [--toml] [--vars] [--export] [--output OUTPUT_FILE]
                         [group]

Show Ansible inventory information, by default it uses the inventory script JSON format

positional arguments:
  group The name of a group in the inventory, relevant when using --graph

...

Actions:
  One of following must be used on invocation, ONLY ONE!

  --graph create inventory graph, if supplying pattern it must be a valid group name. It will ignore limit
  --host HOST Output specific host info, works as inventory script. It will ignore limit
  --list Output all hosts info, works as inventory script

```

In other words, the ‘–limit’ option appears to be entirely useless.

I am using

```auto
$ ansible --version
ansible [core 2.20.1]
...

```

What can I do?

Also, if I’d select the ‘–yaml’ output, how can I prevent the host variables from being printed out?

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [December 16, 2025, 5:53am UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/2 "2025-12-16T05:53:52Z")

</div>

> [@Toni](#):
>
> I’m trying to list a part of my inventory like this:
> 
> ```auto
> $ ansible-inventory --limit group --list
> 
> ```

For me this works as expected: it only prints everything that’s part of the group `group`.

> [@Toni](#):
>
> I’ve also tried
> 
> ```auto
> $ ansible-inventory --list group
> 
> ```
> 
> But I get the entire inventory spewed out to me.

That’s to be expected, since the positional parameter only applies to `--graph`.

> [@Toni](#):
>
> Also, if I’d select the ‘–yaml’ output, how can I prevent the host variables from being printed out?

I don’t think that’s supported. The only output not listing variables is `--graph`, and that doesn’t support `--yaml`. What you could do is pass the output of `--list` through some other tool to extract the data you want, like `jq` (that will give you JSON though).

---

<div class="post-metadata">

**Author:** ![Toni](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Toni](https://forum.ansible.com/u/Toni)\
**Post date:** [December 16, 2025, 11:35am UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/3 "2025-12-16T11:35:18Z")

</div>

> > I wrote:  
> > $ ansible-inventory --limit group --list

> You wrote:  
> For me this works as expected: it only prints everything that’s part of the group group.

How can I debug the inventory problem further, please? If I run `ansible-playbook -l group`, it will only affect the hosts in the group, but if I run `ansible-inventory --limit group --list` with the same group, I still get the entire inventory. Also,

> > I wrote:  
> > Also, if I’d select the ‘–yaml’ output, how can I prevent the host variables from being printed out?

> You wrote:  
> I don’t think that’s supported. The only output not listing variables is --graph, and that doesn’t support --yaml. What you could do is pass the output of --list through some other tool to extract the data you want, like jq (that will give you JSON though).

This sounds like a kludge. I mean, if you have some variables defined eg. on the group level, they’ll get exploded into every host as a host var, which can easily turn a tiny inventory with a few dozen lines into an unmaintainable pile of JSON. Eg. in my example, turning 37 inventory hosts into a 319k JSON file. That sounds pretty wasteful, and I reckon that most inventories are much bigger than that. How about adding a `--no-vars` command line switch?

But FWIW, `jq '._meta.hostvars | keys'` would be a useful invocation of `jq`.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 16, 2025, 3:17pm UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/4 "2025-12-16T15:17:02Z")

</div>

Have you tried using `--export`?

---

<div class="post-metadata">

**Author:** ![Toni](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Toni](https://forum.ansible.com/u/Toni)\
**Post date:** [December 16, 2025, 3:36pm UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/5 "2025-12-16T15:36:39Z")

</div>

I hadn’t, but have just tried, to absolutely no effect. The output always looks the same.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [December 16, 2025, 4:27pm UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/6 "2025-12-16T16:27:27Z")

</div>

`ansible` or `ansible-playbook` with ` --list-hosts` is better for debugging `--limit`.

FYI, `--export` will just keep group variables from being flattened.

In any case `--list <group> -l <limit>` does seem to work in narrowing down the hosts for me, variables are still listed though and can add a lot of noise if what you only want is the hosts.

---

<div class="post-metadata">

**Author:** ![Toni](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Toni](https://forum.ansible.com/u/Toni)\
**Post date:** [December 16, 2025, 5:53pm UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/7 "2025-12-16T17:53:54Z")

</div>

You wrote:

> In any case --list -l does seem to work in narrowing down the hosts for me

Thank you! This is  
This produces an almost usable output. I was originally hoping for something that would emit the group and all subgroups, though, eg.:

```
$ ansible-inventory -l group --list
group:
    subgroup1:
        host1
        host2
    subgroup2:
        host3

```

But it doesn’t seem to be possible atm.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 16, 2025, 9:00pm UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/8 "2025-12-16T21:00:49Z")

</div>

If you don’t have any hyphens in your host or group names, you can get away with this:

```plaintext
$ ansible-inventory --graph group | tr '|@-' ' '

```

It produces something remarkably close to your example desired output. Behold:

```plaintext
utoddl@tango$ cat inv2
[group:children]
subgroup1
subgroup2

[subgroup1]
host1
host2

[subgroup2]
host3

utoddl@tango$ ansible-inventory -i inv2 --graph group | tr '|@-' ' '
 group:
      subgroup1:
        host1
        host2
      subgroup2:
        host3

```

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [December 22, 2025, 3:03pm UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/9 "2025-12-22T15:03:30Z")

</div>

I wrote `ansible-inventory` based on a few 3rd party tools, one I still wish to eventually integrate [GitHub - willthames/ansible-inventory-grapher](https://github.com/willthames/ansible-inventory-grapher) creates a visual representation I find very useful, the `--graph` option is the “poor man’s version” of this. See the `-q` option, it might be what the core tool is missing.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 14, 2026, 6:56pm UTC](https://forum.ansible.com/t/ansible-inventory-and-limit/45004/10 "2026-05-14T18:56:37Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
