Debian Codegraph @

ansible_collections/community/general/plugins/modules/ipify_facts.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2015, René Moser <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: ipify_facts
11 short_description: Retrieve the public IP of your internet gateway
12 description:
13 - If behind NAT and need to know the public IP of your internet gateway.
14 author:
15 - René Moser (@resmo)
16 extends_documentation_fragment:
17 - community.general._attributes
18 - community.general._attributes.facts
19 - community.general._attributes.facts_module
20 options:
21 api_url:
22 description:
23 - URL of the ipify.org API service.
24 - C(?format=json) is appended by default.
25 type: str
26 default: https://api.ipify.org/
27 timeout:
28 description:
29 - HTTP connection timeout in seconds.
30 type: int
31 default: 10
32 validate_certs:
33 description:
34 - When set to V(false), SSL certificates are not validated.
35 type: bool
36 default: true
37 notes:
38 - Visit https://www.ipify.org to get more information.
39 """
40
41 EXAMPLES = r"""
42 # Gather IP facts from ipify.org
43 - name: Get my public IP
44 community.general.ipify_facts:
45
46 # Gather IP facts from your own ipify service endpoint with a custom timeout
47 - name: Get my public IP
48 community.general.ipify_facts:
49 api_url: http://api.example.com/ipify
50 timeout: 20
51 """
52
53 RETURN = r"""
54 ansible_facts:
55 description: The returned facts.
56 returned: success
57 type: dict
58 contains:
59 ipify_public_ip:
60 description: Public IP of the internet gateway.
61 returned: success
62 type: str
63 sample: 1.2.3.4
64 """
65
66 import json
67
68 from ansible.module_utils.basic import AnsibleModule
69 from ansible.module_utils.common.text.converters import to_text
70 from ansible.module_utils.urls import fetch_url
71
72
73 class IpifyFacts:
74 def __init__(self):
75 self.api_url = module.params.get("api_url")
76 self.timeout = module.params.get("timeout")
77
78 def run(self):
79 result = {"ipify_public_ip": None}
80 (response, info) = fetch_url(module=module, url=f"{self.api_url}?format=json", force=True, timeout=self.timeout)
81
82 if not response:
83 module.fail_json(
84 msg=f"No valid or no response from url {self.api_url} within {self.timeout} seconds (timeout)"
85 )
86
87 data = json.loads(to_text(response.read()))
88 result["ipify_public_ip"] = data.get("ip")
89 return result
90
91
92 def main():
93 global module
94 module = AnsibleModule(
95 argument_spec=dict(
96 api_url=dict(type="str", default="https://api.ipify.org/"),
97 timeout=dict(type="int", default=10),
98 validate_certs=dict(type="bool", default=True),
99 ),
100 supports_check_mode=True,
101 )
102
103 ipify_facts = IpifyFacts().run()
104 ipify_facts_result = dict(changed=False, ansible_facts=ipify_facts)
105 module.exit_json(**ipify_facts_result)
106
107
108 if __name__ == "__main__":
109 main()