Last active
September 6, 2017 10:13
-
-
Save Meroje/2df03fd3c7752e886bb8c44c685929fc to your computer and use it in GitHub Desktop.
Ansible Scaleway custom facts, based on https://github.com/br0ziliy/ansible-module-gce-facts
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
DOCUMENTATION = ''' | |
--- | |
module: scw_facts | |
''' | |
EXAMPLES = ''' | |
# To see the list of all the facts use command below: | |
$ ansible -m scw_facts all | |
# Conditional playbook example | |
- name: Gather instance SCW facts | |
action: scw_facts | |
- name: Conditional | |
action: debug msg="This instance has dynamic public ip" | |
when: ansible_scw.public_ip.dynamic | |
''' | |
import os, subprocess | |
from ansible.module_utils.basic import AnsibleModule | |
from ansible.module_utils.urls import fetch_url, url_argument_spec | |
SCW_METADATA_BASE_URL = "http://169.254.42.42/conf" | |
SCW_USERDATA_BASE_URL = "http://169.254.42.42/user_data" | |
class ScwMetadata(object): | |
def __init__(self): | |
self._data = { 'ansible_scw': {} } | |
def _fetch(self, url): | |
(response, info) = fetch_url(module, url+'?format=json', headers={}, force=True) | |
if response: | |
data = response.read() | |
else: | |
data = None | |
self._data['ansible_scw'] = module.from_json(data) | |
def _fetch_userdata(self, url): | |
response = subprocess.check_output(["curl", "--local-port", "1-1024", "--noproxy", "*", "--silent", url+"?format=json"]) | |
if response: | |
data = module.from_json(response) | |
else: | |
data = { 'user_data': {} } | |
self._data['ansible_scw']['user_data'] = {} | |
for key in data['user_data']: | |
response = subprocess.check_output(["curl", "--local-port", "1-1024", "--noproxy", "*", "--silent", url+"/"+key]) | |
if response: | |
userdata = response | |
else: | |
userdata = None | |
self._data['ansible_scw']['user_data'][key] = userdata | |
if self._data['ansible_scw']['user_data']['ssh-host-fingerprints']: | |
self._data['ansible_scw']['user_data']['ssh-host-fingerprints'] = self._data['ansible_scw']['user_data']['ssh-host-fingerprints'].split('\n') | |
def run(self): | |
self._fetch(SCW_METADATA_BASE_URL) | |
self._fetch_userdata(SCW_USERDATA_BASE_URL) | |
return self._data | |
def main(): | |
argument_spec = url_argument_spec() | |
global module | |
module = AnsibleModule( | |
argument_spec = argument_spec, | |
supports_check_mode = True, | |
) | |
scw_facts = ScwMetadata().run() | |
scw_facts_result = dict(changed=False, ansible_facts=scw_facts) | |
module.exit_json(**scw_facts_result) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment