<commit_before>#!/usr/bin/python3

from six.moves.urllib import request
import json
import socket

def main():
    google_ip = get_google_ip()

    if google_ip:
        ip = google_ip
    else:
        ip = get_ip()

    print(json.dumps({
        'changed': False,
        'ansible_facts': {
            'public_ipv4': ip
        }
    }))

def get_google_ip():
    try:
        req = request.Request('http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip')
        req.add_header('Metadata-Flavor', 'Google')
        return request.urlopen(req).read()
    except:
        return None

def get_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    return s.getsockname()[0]

if __name__ == '__main__':
    main()
<commit_msg>Public IP Support<commit_after>#!/usr/bin/python3

from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
import json
import socket

def get_google_ip():
    try:
        response = open_url('http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip',
                method='GET', headers={'Metadata-Flavor': 'Google'})
        return response.read()
    except:
        return None

def get_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    return s.getsockname()[0]

def main():
    module = AnsibleModule(
        argument_spec = dict(),
        supports_check_mode=True
    )

    google_ip = get_google_ip()

    if google_ip:
        ip = google_ip
    else:
        ip = get_ip()

    print(json.dumps({
        'changed': False,
        'ansible_facts': {
            'public_ipv4': ip
        }
    }))

if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.13573407202216067, 'fraction_numerical': 0.013296398891966758, 'mean_word_length': 2.569169960474308, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6675910', 'n_tokens_mistral': 626, 'n_tokens_neox': 609, 'n_words': 112}