Ansible: Access host/group vars from within custom module

前端 未结 2 906
面向向阳花
面向向阳花 2020-12-18 23:39

Is there a way how one can access host/group vars from within a custom written module? I would like to avoid to pass all required vars as module parameters.

My modul

相关标签:
2条回答
  • 2020-12-19 00:23

    Is there a way how one can access host/group vars from within a custom written module?

    Not built-in.

    You will have to pass them yourself one way or the other:

    • Module args.
    • Serialize to local file system (with pickle or yaml.dump() or json or ...) and send the file over.
    • any other innovative ideas you can come up with.

    Unfortunately you can't just send over whole host/groupvar files as-it-is because you would have to implement the variable scope/precedence resolution algorithm of ansible which is undefined (it's not the Zen philosophy of ansible to define such petty things :P ).

    0 讨论(0)
  • 2020-12-19 00:30

    I think you pretty much hit the nail on the head with your thinking here:

    I guess it is not possible, since the module runs on the target machine and probably the facts/host/group vars are not transferred along with the module...

    However, having said that, if you really have a need for this then there might be a slightly messy way of doing it. As of Ansible 1.8 you can set up fact caching, which uses redis to cache facts between runs of plays. Since redis is pretty easy to use and has clients for most popular programming languages, you could have your module query the redis server for any facts you need. It's not exactly the cleanest way to do it, but it just might work.

    0 讨论(0)
提交回复
热议问题