简体   繁体   中英

Ansible: get all variables in own module?

Now I use my own module this way:

- get_my_data:
    hostvars: '{{ hostvars }}'

It uses configuration for all hosts and collects some data.
Can I get hostvars inside this module without explicit actions?

module = AnsibleModule(
    argument_spec=dict(
        hostvars=dict(type='dict', required=True),
    ),
    supports_check_mode=True
)
hostvars = module.params['hostvars']
# some exports
result = {'changed': False, 'ansible_facts': {'my_data': 'test'}}
module.exit_json(**result)

Also I export new variables using ansible_facts dictionary. How can I export them via register ?

Update: Jinja {{ variables }} are not replaced inside hostvars dict. How can I fix it to give actual vars to module?

Update 2:
hostvars: '{{ hostvars | to_json }}' hostvars: '{{ hostvars | to_json }}' helped me to evaluate all the variables

Ansible 2.7.1

Can I get hostvars inside this module without explicit actions?

No. You need to explicitly pass in any variables to which you want the module to have access.

How can I export them via register?

Anything returned via the exit_json method will be available in your registered variable. That is, if you were to have...

result = {'changed': False, 'my_data': 'test'}
module.exit_json(**result)

...you would find a my_data key in your registered variable.

Jinja {{ variables }} are not replaced inside hostvars dict. How can I fix it to give actual vars to module?

I don't think you're going to be able to do that within your module. With a more complete example in your question, showing exactly what you're trying to do, we might be able to suggest alternative ways of approaching the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM