I’m trying to list a part of my inventory like this:
$ ansible-inventory --limit group --list
I’ve also tried
$ ansible-inventory --list group
But I get the entire inventory spewed out to me. This is unfortunately consistent with the help message:
$ 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
$ 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?
For me this works as expected: it only prints everything that’s part of the group group.
That’s to be expected, since the positional parameter only applies to --graph.
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).
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.