| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2025, IP2Location <[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: ip2location_info |
| 11 |
short_description: Retrieve IP geolocation information of a host's IP address |
| 12 |
version_added: 12.2.0 |
| 13 |
description: |
| 14 |
- Gather IP geolocation information of a host's IP address using the keyless U(api.ip2location.io) API. |
| 15 |
author: "IP2Location (@ip2location)" |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._attributes |
| 18 |
- community.general._attributes.info_module |
| 19 |
options: |
| 20 |
ip: |
| 21 |
description: |
| 22 |
- IP address to retrieve geolocation information. |
| 23 |
type: str |
| 24 |
timeout: |
| 25 |
description: |
| 26 |
- HTTP connection timeout in seconds. |
| 27 |
default: 10 |
| 28 |
type: int |
| 29 |
http_agent: |
| 30 |
description: |
| 31 |
- Set HTTP user agent. |
| 32 |
default: "ansible-ip2location-info-module/0.0.1" |
| 33 |
type: str |
| 34 |
notes: |
| 35 |
- This module uses the keyless endpoint which has usage limits. |
| 36 |
Check U(https://www.ip2location.io/ip2location-documentation) for more information. |
| 37 |
""" |
| 38 |
|
| 39 |
EXAMPLES = r""" |
| 40 |
# Retrieve geolocation data of a host's IP address |
| 41 |
- name: Get IP geolocation data |
| 42 |
community.general.ip2location_info: |
| 43 |
register: result |
| 44 |
|
| 45 |
- name: Show some information |
| 46 |
ansible.builtin.debug: |
| 47 |
msg: "{{ result.ip }} is located in {{ result.country_code }} ({{ result.country_name }})" |
| 48 |
""" |
| 49 |
|
| 50 |
RETURN = r""" |
| 51 |
ip: |
| 52 |
description: "Public IP address of a host." |
| 53 |
type: str |
| 54 |
returned: success |
| 55 |
sample: "8.8.8.8" |
| 56 |
country_code: |
| 57 |
description: ISO 3166-1 alpha-2 country code. |
| 58 |
type: str |
| 59 |
returned: success |
| 60 |
sample: "US" |
| 61 |
country_name: |
| 62 |
description: Country name based on ISO 3166. |
| 63 |
type: str |
| 64 |
returned: success |
| 65 |
sample: "United States of America" |
| 66 |
region_name: |
| 67 |
description: State or province name. |
| 68 |
type: str |
| 69 |
returned: success |
| 70 |
sample: "California" |
| 71 |
city_name: |
| 72 |
description: City name. |
| 73 |
type: str |
| 74 |
returned: success |
| 75 |
sample: "Mountain View" |
| 76 |
latitude: |
| 77 |
description: Latitude of the city. |
| 78 |
type: float |
| 79 |
returned: success |
| 80 |
sample: 37.3860 |
| 81 |
longitude: |
| 82 |
description: Longitude of the city. |
| 83 |
type: float |
| 84 |
returned: success |
| 85 |
sample: -122.0838 |
| 86 |
zip_code: |
| 87 |
description: ZIP/Postal code. |
| 88 |
type: str |
| 89 |
returned: success |
| 90 |
sample: "94035" |
| 91 |
time_zone: |
| 92 |
description: UTC time zone (with DST supported). |
| 93 |
type: str |
| 94 |
returned: success |
| 95 |
sample: "-08:00" |
| 96 |
asn: |
| 97 |
description: Autonomous system number (ASN). |
| 98 |
type: str |
| 99 |
returned: success |
| 100 |
sample: "15169" |
| 101 |
as: |
| 102 |
description: Autonomous system (AS) name. |
| 103 |
type: str |
| 104 |
returned: success |
| 105 |
sample: "Google LLC" |
| 106 |
is_proxy: |
| 107 |
description: Whether is a proxy or not. |
| 108 |
type: bool |
| 109 |
returned: success |
| 110 |
sample: false |
| 111 |
""" |
| 112 |
|
| 113 |
import typing as t |
| 114 |
|
| 115 |
from ansible.module_utils.basic import AnsibleModule |
| 116 |
from ansible.module_utils.urls import fetch_url |
| 117 |
|
| 118 |
USER_AGENT = "ansible-ip2location-info-module/0.0.1" |
| 119 |
|
| 120 |
|
| 121 |
class Ip2LocationInfo: |
| 122 |
def __init__(self, module: AnsibleModule) -> None: |
| 123 |
self.url = "https://api.ip2location.io/" |
| 124 |
self.timeout = module.params.get("timeout") |
| 125 |
self.ip = module.params.get("ip") |
| 126 |
self.module = module |
| 127 |
|
| 128 |
def get_geo_data(self) -> dict[str, t.Any]: |
| 129 |
url = self.url |
| 130 |
if self.ip: |
| 131 |
url += f"?ip={self.ip}" |
| 132 |
|
| 133 |
response, info = fetch_url( |
| 134 |
self.module, |
| 135 |
url, |
| 136 |
timeout=self.timeout, |
| 137 |
) |
| 138 |
if info["status"] != 200: |
| 139 |
self.module.fail_json( |
| 140 |
msg=f"Could not get {url} page, check for connectivity! HTTP Status: {info['status']}" |
| 141 |
) |
| 142 |
|
| 143 |
try: |
| 144 |
content = response.read() |
| 145 |
result = self.module.from_json(content) |
| 146 |
except Exception as exc: |
| 147 |
self.module.fail_json( |
| 148 |
msg=f"Failed to parse the ip2location.io response from {url}: {exc}", read_content=content |
| 149 |
) |
| 150 |
else: |
| 151 |
return result |
| 152 |
|
| 153 |
|
| 154 |
def main(): |
| 155 |
module = AnsibleModule( |
| 156 |
argument_spec=dict( |
| 157 |
http_agent=dict(default=USER_AGENT), |
| 158 |
timeout=dict(type="int", default=10), |
| 159 |
ip=dict(type="str"), |
| 160 |
), |
| 161 |
supports_check_mode=True, |
| 162 |
) |
| 163 |
|
| 164 |
ip2location_info = Ip2LocationInfo(module) |
| 165 |
ip2location_info_result = {"changed": False} |
| 166 |
ip2location_info_result.update(ip2location_info.get_geo_data()) |
| 167 |
module.exit_json(**ip2location_info_result) |
| 168 |
|
| 169 |
|
| 170 |
if __name__ == "__main__": |
| 171 |
main() |