Using all:vars in script inventories

I have a use case for setting some global vars via the inventory (rather than group_vars/all for instance). This works as expected when using an inventory file ([all:vars]). However when using an inventory script these global variables are not surfacing (to templates et al).

I see group.set_variable being called with the proper var for my script inventories all vars. However, I believe inventory.script.__parse is not setup to handle/expect an ‘all’ group user defined. When it encounters an all group defined in the inventory it creates a new Group rather than using the default all group, and then adds that user defined all group as a child of the default all group (which means the user defined all vars are effectively meaningless)

This could be fixed as easily with something like:

if ‘vars’ in data:
for k, v in data[‘vars’].iteritems():
if group.name == all.name:
all.set_variable(k, v)
else:
group.set_variable(k, v)

Is this worthy of a pull request?

If you can’t set variables on the ‘all’ group in your inventory script then yes, that ought to be fixed up.

Can you paste the output of what your inventory script returns for --list for the all group just so I can make sure it’s right first?

This is the output I am testing with.

{
“all”: {
“vars”: {
“testing”: “FromScriptInventory!”
}
},
“mysql”: {
“hosts”: [
“c0004189”
],
“vars”: {
“innodb_buffer_size”: “3G”
}
},
“varnish”: {
“hosts”: [
“c0004186”
],
“vars”: {
“storage-size”: “3G”
}
},
“webservers”: {
“hosts”: [
“c0004187”,
“c0004188”
],
“vars”: {
“port”: 80
}
}
}

Ok, yes, looks right to me.

Send me a pull request and we’ll get that added.