From 72eb8c7be9090929624b7f2951cca2b8a069ac08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Wed, 16 Nov 2016 08:09:14 +0100 Subject: [PATCH] ipify_facts: implement timeout (#3369) --- .../modules/extras/network/ipify_facts.py | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/ansible/modules/extras/network/ipify_facts.py b/lib/ansible/modules/extras/network/ipify_facts.py index 95bf549be9..7a07f577c2 100644 --- a/lib/ansible/modules/extras/network/ipify_facts.py +++ b/lib/ansible/modules/extras/network/ipify_facts.py @@ -33,6 +33,12 @@ options: - C(?format=json) will be appended per default. required: false default: 'https://api.ipify.org' + timeout: + description: + - HTTP connection timeout in seconds. + required: false + default: 10 + version_added: "2.3" notes: - "Visit https://www.ipify.org to get more information." ''' @@ -42,9 +48,11 @@ EXAMPLES = ''' - name: get my public IP ipify_facts: -# Gather IP facts from your own ipify service endpoint +# Gather IP facts from your own ipify service endpoint with a custom timeout - name: get my public IP - ipify_facts: api_url=http://api.example.com/ipify + ipify_facts: + api_url: http://api.example.com/ipify + timeout: 20 ''' RETURN = ''' @@ -65,27 +73,35 @@ except ImportError: # Let snippet from module_utils/basic.py return a proper error in this case pass +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.urls import fetch_url + class IpifyFacts(object): def __init__(self): self.api_url = module.params.get('api_url') + self.timeout = module.params.get('timeout') def run(self): result = { 'ipify_public_ip': None } - (response, info) = fetch_url(module, self.api_url + "?format=json" , force=True) - if response: - data = json.loads(response.read()) - result['ipify_public_ip'] = data.get('ip') + (response, info) = fetch_url(module=module, url=self.api_url + "?format=json" , force=True, timeout=self.timeout) + + if not response: + module.fail_json(msg="No valid or no response from url %s within %s seconds (timeout)" % (self.api_url, self.timeout)) + + data = json.loads(response.read()) + result['ipify_public_ip'] = data.get('ip') return result def main(): global module module = AnsibleModule( argument_spec = dict( - api_url = dict(default='https://api.ipify.org'), + api_url=dict(default='https://api.ipify.org'), + timeout=dict(type='int', default=10), ), supports_check_mode=True, ) @@ -94,7 +110,5 @@ def main(): ipify_facts_result = dict(changed=False, ansible_facts=ipify_facts) module.exit_json(**ipify_facts_result) -from ansible.module_utils.basic import * -from ansible.module_utils.urls import * if __name__ == '__main__': main()