1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/ipinfoio_facts.py

137 lines
3.6 KiB
Python
Raw Permalink Normal View History

2020-03-09 10:11:07 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2020-03-09 10:11:07 +01:00
# Copyright (c) 2016, Aleksei Kostiuk <unitoff@gmail.com>
# 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
2020-03-09 10:11:07 +01:00
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: ipinfoio_facts
short_description: Retrieve IP geolocation facts of a host's IP address
2020-03-09 10:11:07 +01:00
description:
- "Gather IP geolocation facts of a host's IP address using ipinfo.io API"
author: "Aleksei Kostiuk (@akostyuk)"
extends_documentation_fragment:
- community.general.attributes
- community.general.attributes.facts
- community.general.attributes.facts_module
2020-03-09 10:11:07 +01:00
options:
timeout:
description:
- HTTP connection timeout in seconds
required: false
default: 10
Tidy up validate-modules:no-default-for-required-parameter and other cases (#1423) * Fixed validate-modules:mutually_exclusive-unknown for plugins/modules/packaging/os/redhat_subscription.py * fixed validation-modules for plugins/modules/cloud/lxd/lxd_container.py * fixed validation-modules for plugins/modules/web_infrastructure/sophos_utm/utm_network_interface_address.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_host.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_image_info.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_image.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_service.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_vm.py * fixed validation-modules for plugins/modules/net_tools/cloudflare_dns.py * fixed validation-modules for plugins/modules/net_tools/ip_netns.py * fixed validation-modules for plugins/modules/net_tools/ipinfoio_facts.py * fixed validation-modules for plugins/modules/net_tools/netcup_dns.py * fixed validation-modules for plugins/modules/remote_management/wakeonlan.py * added types to plugins/modules/remote_management/stacki/stacki_host.py but still cannot remove ignore line * added a couple of FIXME comments * fixed validation-modules for plugins/modules/remote_management/manageiq/manageiq_provider.py * fixed validation-modules for plugins/modules/notification/rocketchat.py * fixed validation-modules for plugins/modules/monitoring/bigpanda.py * fixed validation-modules for plugins/modules/identity/keycloak/keycloak_client.py * fixed validation-modules for plugins/modules/identity/keycloak/keycloak_clienttemplate.py * fixed validation-modules for plugins/modules/cloud/univention/udm_user.py * fixed validation-modules for plugins/modules/cloud/univention/udm_group.py * fixed validation-modules for plugins/modules/cloud/spotinst/spotinst_aws_elastigroup.py * fixed validation-modules for plugins/modules/cloud/smartos/imgadm.py * fixed validation-modules for plugins/modules/cloud/profitbricks/profitbricks_nic.py * fixed validation-modules for plugins/modules/cloud/ovirt/ovirt_external_provider_facts.py * Tidy up validate-modules ignores no-default-for-required-parameter + couple of other cases * Added changelog frag * fixed validation-modules for plugins/modules/cloud/centurylink/clc_alert_policy.py * fixed validation-modules for plugins/modules/cloud/centurylink/clc_firewall_policy.py * fixed validation-modules for plugins/modules/cloud/lxd/lxd_profile.py * Typos and small fixes * fixed validation-modules for plugins/modules/net_tools/ldap/ldap_passwd.py * Typos and small fixes, part 2 * Fixes from PR comments * Update plugins/modules/cloud/profitbricks/profitbricks_nic.py Co-authored-by: Felix Fontein <felix@fontein.de> * Rolled back the mutually-exclusive-unknown in redhat_subscription * Update changelogs/fragments/1423-valmod_multiple_cases.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2020-12-01 21:28:40 +01:00
type: int
2020-03-09 10:11:07 +01:00
http_agent:
description:
- Set http user agent
required: false
default: "ansible-ipinfoio-module/0.0.1"
Tidy up validate-modules:no-default-for-required-parameter and other cases (#1423) * Fixed validate-modules:mutually_exclusive-unknown for plugins/modules/packaging/os/redhat_subscription.py * fixed validation-modules for plugins/modules/cloud/lxd/lxd_container.py * fixed validation-modules for plugins/modules/web_infrastructure/sophos_utm/utm_network_interface_address.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_host.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_image_info.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_image.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_service.py * fixed validation-modules for plugins/modules/cloud/opennebula/one_vm.py * fixed validation-modules for plugins/modules/net_tools/cloudflare_dns.py * fixed validation-modules for plugins/modules/net_tools/ip_netns.py * fixed validation-modules for plugins/modules/net_tools/ipinfoio_facts.py * fixed validation-modules for plugins/modules/net_tools/netcup_dns.py * fixed validation-modules for plugins/modules/remote_management/wakeonlan.py * added types to plugins/modules/remote_management/stacki/stacki_host.py but still cannot remove ignore line * added a couple of FIXME comments * fixed validation-modules for plugins/modules/remote_management/manageiq/manageiq_provider.py * fixed validation-modules for plugins/modules/notification/rocketchat.py * fixed validation-modules for plugins/modules/monitoring/bigpanda.py * fixed validation-modules for plugins/modules/identity/keycloak/keycloak_client.py * fixed validation-modules for plugins/modules/identity/keycloak/keycloak_clienttemplate.py * fixed validation-modules for plugins/modules/cloud/univention/udm_user.py * fixed validation-modules for plugins/modules/cloud/univention/udm_group.py * fixed validation-modules for plugins/modules/cloud/spotinst/spotinst_aws_elastigroup.py * fixed validation-modules for plugins/modules/cloud/smartos/imgadm.py * fixed validation-modules for plugins/modules/cloud/profitbricks/profitbricks_nic.py * fixed validation-modules for plugins/modules/cloud/ovirt/ovirt_external_provider_facts.py * Tidy up validate-modules ignores no-default-for-required-parameter + couple of other cases * Added changelog frag * fixed validation-modules for plugins/modules/cloud/centurylink/clc_alert_policy.py * fixed validation-modules for plugins/modules/cloud/centurylink/clc_firewall_policy.py * fixed validation-modules for plugins/modules/cloud/lxd/lxd_profile.py * Typos and small fixes * fixed validation-modules for plugins/modules/net_tools/ldap/ldap_passwd.py * Typos and small fixes, part 2 * Fixes from PR comments * Update plugins/modules/cloud/profitbricks/profitbricks_nic.py Co-authored-by: Felix Fontein <felix@fontein.de> * Rolled back the mutually-exclusive-unknown in redhat_subscription * Update changelogs/fragments/1423-valmod_multiple_cases.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2020-12-01 21:28:40 +01:00
type: str
2020-03-09 10:11:07 +01:00
notes:
- "Check http://ipinfo.io/ for more information"
'''
EXAMPLES = '''
# Retrieve geolocation data of a host's IP address
2020-05-16 15:07:51 +02:00
- name: Get IP geolocation data
community.general.ipinfoio_facts:
2020-03-09 10:11:07 +01:00
'''
RETURN = '''
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"
hostname:
description: Domain name
type: str
sample: "google-public-dns-a.google.com"
country:
description: ISO 3166-1 alpha-2 country code
type: str
sample: "US"
region:
description: State or province name
type: str
sample: "California"
city:
description: City name
type: str
sample: "Mountain View"
loc:
description: Latitude and Longitude of the location
type: str
sample: "37.3860,-122.0838"
org:
description: "organization's name"
type: str
sample: "AS3356 Level 3 Communications, Inc."
postal:
description: Postal code
type: str
sample: "94035"
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url
USER_AGENT = 'ansible-ipinfoio-module/0.0.1'
class IpinfoioFacts(object):
def __init__(self, module):
self.url = 'https://ipinfo.io/json'
self.timeout = module.params.get('timeout')
self.module = module
def get_geo_data(self):
response, info = fetch_url(self.module, self.url, force=True, # NOQA
timeout=self.timeout)
try:
info['status'] == 200
except AssertionError:
self.module.fail_json(msg='Could not get {0} page, '
'check for connectivity!'.format(self.url))
else:
try:
content = response.read()
result = self.module.from_json(content.decode('utf8'))
except ValueError:
self.module.fail_json(
msg='Failed to parse the ipinfo.io response: '
'{0} {1}'.format(self.url, content))
else:
return result
def main():
module = AnsibleModule( # NOQA
argument_spec=dict(
http_agent=dict(default=USER_AGENT),
timeout=dict(type='int', default=10),
),
supports_check_mode=True,
)
ipinfoio = IpinfoioFacts(module)
ipinfoio_result = dict(
changed=False, ansible_facts=ipinfoio.get_geo_data())
module.exit_json(**ipinfoio_result)
if __name__ == '__main__':
main()