-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Added ip2location.io for IP geolocation lookup. #11170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ip2location
wants to merge
14
commits into
ansible-collections:main
Choose a base branch
from
ip2location:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−0
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d24c912
Added ip2location.io for IP geolocation lookup.
ip2location fb351b1
Removed tab in last line.
ip2location d69c3e8
Added "ip2location" as maintainer.
ip2location 54bcad8
Update plugins/modules/ip2locationio_facts.py
ip2location d34c8eb
Update plugins/modules/ip2locationio_facts.py
ip2location 561606e
Update plugins/modules/ip2locationio_facts.py
ip2location 81c39a3
Update plugins/modules/ip2locationio_facts.py
ip2location f3a7b6c
Update plugins/modules/ip2locationio_facts.py
ip2location a6be241
Update plugins/modules/ip2locationio_facts.py
ip2location b7d3650
Update plugins/modules/ip2locationio_facts.py
ip2location 2ae9ad3
Added "typing" library.
ip2location 59647f5
Updated import position.
ip2location 1df0bc5
Merge branch 'ansible-collections:main' into main
ip2location 74f1f6c
Reformatted.
ip2location File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| #!/usr/bin/python | ||
|
|
||
| # Copyright (c) 2025, IP2Location <[email protected]> | ||
| # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
| # SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| DOCUMENTATION = r""" | ||
| module: ip2locationio_facts | ||
| short_description: Retrieve IP geolocation facts of a host's IP address | ||
| version_added: 13.1.0 | ||
| description: | ||
| - Gather IP geolocation facts of a host's IP address using the keyless U(api.ip2location.io) API. | ||
| author: "IP2Location (@ip2location)" | ||
| extends_documentation_fragment: | ||
| - community.general.attributes | ||
| - community.general.attributes.facts | ||
| - community.general.attributes.facts_module | ||
| options: | ||
| timeout: | ||
| description: | ||
| - HTTP connection timeout in seconds. | ||
| default: 10 | ||
| type: int | ||
| http_agent: | ||
| description: | ||
| - Set http user agent. | ||
| default: "ansible-ip2locationio-module/0.0.1" | ||
| type: str | ||
| notes: | ||
| - This module uses the keyless endpoint which has usage limits. | ||
| Check U(https://www.ip2location.io/ip2location-documentation) for more information. | ||
| """ | ||
|
|
||
| EXAMPLES = r""" | ||
| # Retrieve geolocation data of a host's IP address | ||
| - name: Get IP geolocation data | ||
| community.general.ip2locationio_facts: | ||
| """ | ||
|
|
||
| RETURN = r""" | ||
| ansible_facts: | ||
| description: "Dictionary of IP geolocation facts for a host's IP address." | ||
| returned: changed | ||
| type: complex | ||
| contains: | ||
| ip: | ||
| description: "Public IP address of a host." | ||
| type: str | ||
| sample: "8.8.8.8" | ||
| country_code: | ||
| description: ISO 3166-1 alpha-2 country code. | ||
| type: str | ||
| sample: "US" | ||
| country_name: | ||
| description: Country name based on ISO 3166. | ||
| type: str | ||
| sample: "United States of America" | ||
| region_name: | ||
| description: State or province name. | ||
| type: str | ||
| sample: "California" | ||
| city_name: | ||
| description: City name. | ||
| type: str | ||
| sample: "Mountain View" | ||
| latitude: | ||
| description: Latitude of the city. | ||
| type: float | ||
| sample: 37.3860 | ||
| longitude: | ||
| description: Longitude of the city. | ||
| type: float | ||
| sample: -122.0838 | ||
| zip_code: | ||
| description: ZIP/Postal code. | ||
| type: str | ||
| sample: "94035" | ||
| time_zone: | ||
| description: UTC time zone (with DST supported). | ||
| type: str | ||
| sample: "-08:00" | ||
| asn: | ||
| description: Autonomous system number (ASN). | ||
| type: str | ||
| sample: "15169" | ||
| as: | ||
| description: Autonomous system (AS) name. | ||
| type: str | ||
| sample: "Google LLC" | ||
| is_proxy: | ||
| description: Whether is a proxy or not. | ||
| type: bool | ||
| sample: false | ||
| """ | ||
|
|
||
| import typing as t | ||
|
|
||
| from ansible.module_utils.basic import AnsibleModule | ||
| from ansible.module_utils.urls import fetch_url | ||
|
|
||
| USER_AGENT = "ansible-ip2locationio-module/0.0.1" | ||
|
|
||
|
|
||
| class Ip2locationioFacts: | ||
| def __init__(self, module: AnsibleModule) -> None: | ||
| self.url = "https://api.ip2location.io/" | ||
| self.timeout = module.params.get("timeout") | ||
| self.module = module | ||
|
|
||
| def get_geo_data(self) -> dict[str, t.Any]: | ||
| response, info = fetch_url( | ||
| self.module, | ||
| self.url, | ||
| force=True, | ||
| timeout=self.timeout, | ||
| ) | ||
| if info["status"] != 200: | ||
| self.module.fail_json( | ||
| msg=f"Could not get {self.url} page, check for connectivity! HTTP Status: {info['status']}" | ||
| ) | ||
|
|
||
| try: | ||
| content = response.read() | ||
| result = self.module.from_json(content) | ||
| except Exception as exc: | ||
| self.module.fail_json( | ||
| msg=f"Failed to parse the ip2location.io response from {self.url}: {exc}", read_content=content | ||
| ) | ||
| else: | ||
| return result | ||
|
|
||
|
|
||
| def main(): | ||
| module = AnsibleModule( | ||
| argument_spec=dict( | ||
| http_agent=dict(default=USER_AGENT), | ||
| timeout=dict(type="int", default=10), | ||
| ), | ||
| supports_check_mode=True, | ||
| ) | ||
|
|
||
| ip2locationio = Ip2locationioFacts(module) | ||
| ip2locationio_result = dict(changed=False, ansible_facts=ip2locationio.get_geo_data()) | ||
| module.exit_json(**ip2locationio_result) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.