`--no-vars` option for `ansible-inventory(1)`?

Hi!

I’m using ansible-inventory --list to get a parsed view of the inventory¹.

Unfortunately, it does too much: it also outputs a huge (8 MB compared to maybe a dozen KB for the inventory) key called _meta which I have to immediately throw away again because I’m not interested.

I looked into whether I could use the Python3 API directly, but the docs say it’s internal and paws away.

So, this would not be a problem (I can just throw away that subkey)… except, to output that part, it needs the vault password.

I didn’t even notice because I have a thing that decrypts the vault password using ssh-agent automatically when needed (blogpost (in English) on that coming), but others have not yet set that up and did notice, so I added --ask-vault-pass for now.

Could please an option like --no-vars or --no-meta be added to the ansible-inventory(1) tool so that it just… doesn’t do that?

Having looked at the source, that would be an addition of… maybe three lines or so (plus indenting change).


① This is for a verification tool that checks things like, every host has exactly one host playbook xor one group playbook; every host playbook handles exactly one host; every host is in the inventory; every group playbook handles exactly one group; warn if hosts in the inventory have neither host nor group playbook; check host and group names against an RE; generate a “master playbook” that imports all others; etc. — it’s probably very specific to one certain (possibly suboptimal but historically grown) repo layout, but if there is interest, I could probably share it, I’d have to ask but experience tells me chances are good.

1 Like

(on that matter, it would be nice if the format was actually documented in the manpage and documented as stable, and if other tools outputting similar informations, like ansible-playbook --list-hosts, also had stable, documented, script-parseable output, at least with an option like --batch or so, no need to change the existing output if such an option is not given)

1 Like

How about following the basics of Unix philosophy, using one tool for one task, resulting into

ansible-inventory --inventory hosts.ini --list | jq 'del(._meta)'

and an example output of

{
  "all": {
    "children": [
      "ungrouped",
      "webservers"
    ]
  },
  "webservers": {
    "hosts": [
      "dev"
    ]
  }
}

It is also possible to remove the Default groups from the output by using

| jq 'del(._meta, .all)'

Would that be a feasible approach for you?

Have you considered using ansible-inventory --graph instead of --list? It does not show variables by default.

(on that matter, it would be nice if the format was actually documented in the manpage and documented as stable, …

The --list output is stable, it’s the same format emitted by inventory scripts.

3 Likes

Another option is to create a Jinja2 template that dumps exactly the structure you want and run it from ansible or ansible-playbook via the template action.

That’s what I’m doing now, as you can see from the first post, and that’s what causes the problems because it accesses the vault.

Yes. It is the very counter-example of parsable, though. Good for a human quick overview (though what is @ungrouped?).

Hmh, the --list output minus _meta is pretty much exactly what I want… and it goes to stdout instead of needing tempfiles…