Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2016, Aleksei Kostiuk <[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: ipinfoio_facts
11 short_description: Retrieve IP geolocation facts of a host's IP address
12 description:
13 - Gather IP geolocation facts of a host's IP address using ipinfo.io API.
14 author: "Aleksei Kostiuk (@akostyuk)"
15 extends_documentation_fragment:
16 - community.general._attributes
17 - community.general._attributes.facts
18 - community.general._attributes.facts_module
19 options:
20 timeout:
21 description:
22 - HTTP connection timeout in seconds.
23 default: 10
24 type: int
25 http_agent:
26 description:
27 - Set http user agent.
28 default: "ansible-ipinfoio-module/0.0.1"
29 type: str
30 notes:
31 - Check U(http://ipinfo.io/) for more information.
32 """
33
34 EXAMPLES = r"""
35 # Retrieve geolocation data of a host's IP address
36 - name: Get IP geolocation data
37 community.general.ipinfoio_facts:
38 """
39
40 RETURN = r"""
41 ansible_facts:
42 description: "Dictionary of IP geolocation facts for a host's IP address."
43 returned: success
44 type: dict
45 contains:
46 ip:
47 description: "Public IP address of a host."
48 type: str
49 sample: "8.8.8.8"
50 hostname:
51 description: Domain name.
52 type: str
53 sample: "google-public-dns-a.google.com"
54 country:
55 description: ISO 3166-1 alpha-2 country code.
56 type: str
57 sample: "US"
58 region:
59 description: State or province name.
60 type: str
61 sample: "California"
62 city:
63 description: City name.
64 type: str
65 sample: "Mountain View"
66 loc:
67 description: Latitude and Longitude of the location.
68 type: str
69 sample: "37.3860,-122.0838"
70 org:
71 description: "Organization's name."
72 type: str
73 sample: "AS3356 Level 3 Communications, Inc."
74 postal:
75 description: Postal code.
76 type: str
77 sample: "94035"
78 """
79 from ansible.module_utils.basic import AnsibleModule
80 from ansible.module_utils.urls import fetch_url
81
82 USER_AGENT = "ansible-ipinfoio-module/0.0.1"
83
84
85 class IpinfoioFacts:
86 def __init__(self, module):
87 self.url = "https://ipinfo.io/json"
88 self.timeout = module.params.get("timeout")
89 self.module = module
90
91 def get_geo_data(self):
92 response, info = fetch_url(
93 self.module,
94 self.url,
95 force=True,
96 timeout=self.timeout,
97 )
98 if info["status"] != 200:
99 self.module.fail_json(msg=f"Could not get {self.url} page, check for connectivity!")
100
101 try:
102 content = response.read()
103 result = self.module.from_json(content)
104 except ValueError:
105 self.module.fail_json(msg=f"Failed to parse the ipinfo.io response: {self.url} {content}")
106 else:
107 return result
108
109
110 def main():
111 module = AnsibleModule(
112 argument_spec=dict(
113 http_agent=dict(default=USER_AGENT),
114 timeout=dict(type="int", default=10),
115 ),
116 supports_check_mode=True,
117 )
118
119 ipinfoio = IpinfoioFacts(module)
120 ipinfoio_result = dict(changed=False, ansible_facts=ipinfoio.get_geo_data())
121 module.exit_json(**ipinfoio_result)
122
123
124 if __name__ == "__main__":
125 main()