79437129

Date: 2025-02-13 16:56:45
Score: 0.5
Natty:
Report link

The above Answer will resolve a single fact by using the "filter" key in module_args. If you wish to access multiple facts, or all of them within your custom Ansible module, you can omit the filter key. In the following example, the "filter" key is replaced with a notional "path" key in module_args to give the module an argument. You can then assign all Ansible facts into a variable, which will return a dict. From there, simply reference whatever fact(s) you need.

def run_module():
    module_args = dict(
        path=dict(type="str", required=False, default="/etc/hosts")
    )
    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
    all_facts = ansible_facts(module)

    ipv4_addresses = all_facts["all_ipv4_addresses"]
    hostname = all_facts["hostname"]

    module.exit_json(changed=False, ipv4=ipv4_addresses, host=hostname)


if __name__ == "__main__":
    run_module()
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: register