Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
997 views
in Technique[技术] by (71.8m points)

python - Ansible: Access host/group vars from within custom module

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 module is written in Python and I use the boilerplate. I checked pretty much all available vars but they are not stored anywhere:

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

Now my only hope is they are somehow accessible through the undocumented module utils.

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...

Edit: Found the module utils now and it doesn't look promising.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...