Ansible: Access vars hosts / groups from user module

Is there a way to access host / group vars from a custom writing module ? I would like to avoid passing all the necessary parameters as module parameters.

My module is written in Python and I am using a template. I checked almost all the available vars, but they are not stored anywhere:

def main(): pprint(dir()) pprint(globals()) pprint(locals()) for name in vars().keys(): print(name) 

Now, I hope they are somehow accessible through undocumented utils modules.

I think this is not possible, since the module runs on the target machine, and probably the facts / hosts / vars groups are not transmitted along with the module ...

Edit: The utils module has now been found, and it does not look promising.

+6
source share
2 answers

I think you pretty much hit a nail on your head, thinking here:

I think this is not possible, since the module runs on the target machine, and probably the facts / hosts / vars groups are not transmitted along with the module ...

However, having said that, if you really have a need for it, then there might be a slightly dirty way to do it. Starting with Ansible 1.8, you can configure fact caching , in which redis is used to cache facts between game launches. Since redis is fairly easy to use and clients for most popular programming languages , you can request a module on the redis server for any facts that you need. This is not a completely clean way to do this, but it might work.

+1
source

Is there a way to access the host / group vars from a custom written module?

Not built in.

You will have to pass them yourself anyway:

  • The arguments of the module.
  • Serialize on the local file system (with pickle or yaml.dump() or json or ...) and send the file.
  • any other innovative ideas you can come up with.

Unfortunately, you cannot just forward all host / groupvar files as they are, because you have to implement a variable resolution / resolution priority algorithm that is undefined (this is not Zen's philosophy to define such little things: P).

+3
source

Source: https://habr.com/ru/post/983731/


All Articles