|
|
@ -104,36 +104,40 @@ cloudstack_user_data:
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
from ansible.module_utils.facts import ansible_facts, module
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
import yaml
|
|
|
|
has_lib_yaml = True
|
|
|
|
HAS_LIB_YAML = True
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
has_lib_yaml = False
|
|
|
|
HAS_LIB_YAML = False
|
|
|
|
|
|
|
|
|
|
|
|
CS_METADATA_BASE_URL = "http://%s/latest/meta-data"
|
|
|
|
CS_METADATA_BASE_URL = "http://%s/latest/meta-data"
|
|
|
|
CS_USERDATA_BASE_URL = "http://%s/latest/user-data"
|
|
|
|
CS_USERDATA_BASE_URL = "http://%s/latest/user-data"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CloudStackFacts(object):
|
|
|
|
class CloudStackFacts(object):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
self.facts = ansible_facts(module)
|
|
|
|
self.facts = ansible_facts(module)
|
|
|
|
self.api_ip = None
|
|
|
|
self.api_ip = None
|
|
|
|
self.fact_paths = {
|
|
|
|
self.fact_paths = {
|
|
|
|
'cloudstack_service_offering': 'service-offering',
|
|
|
|
'cloudstack_service_offering': 'service-offering',
|
|
|
|
'cloudstack_availability_zone': 'availability-zone',
|
|
|
|
'cloudstack_availability_zone': 'availability-zone',
|
|
|
|
'cloudstack_public_hostname': 'public-hostname',
|
|
|
|
'cloudstack_public_hostname': 'public-hostname',
|
|
|
|
'cloudstack_public_ipv4': 'public-ipv4',
|
|
|
|
'cloudstack_public_ipv4': 'public-ipv4',
|
|
|
|
'cloudstack_local_hostname': 'local-hostname',
|
|
|
|
'cloudstack_local_hostname': 'local-hostname',
|
|
|
|
'cloudstack_local_ipv4': 'local-ipv4',
|
|
|
|
'cloudstack_local_ipv4': 'local-ipv4',
|
|
|
|
'cloudstack_instance_id': 'instance-id'
|
|
|
|
'cloudstack_instance_id': 'instance-id'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
def run(self):
|
|
|
|
result = {}
|
|
|
|
result = {}
|
|
|
|
filter = module.params.get('filter')
|
|
|
|
filter = module.params.get('filter')
|
|
|
|
if not filter:
|
|
|
|
if not filter:
|
|
|
|
for key,path in self.fact_paths.items():
|
|
|
|
for key, path in self.fact_paths.items():
|
|
|
|
result[key] = self._fetch(CS_METADATA_BASE_URL + "/" + path)
|
|
|
|
result[key] = self._fetch(CS_METADATA_BASE_URL + "/" + path)
|
|
|
|
result['cloudstack_user_data'] = self._get_user_data_json()
|
|
|
|
result['cloudstack_user_data'] = self._get_user_data_json()
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -143,7 +147,6 @@ class CloudStackFacts(object):
|
|
|
|
result[filter] = self._fetch(CS_METADATA_BASE_URL + "/" + self.fact_paths[filter])
|
|
|
|
result[filter] = self._fetch(CS_METADATA_BASE_URL + "/" + self.fact_paths[filter])
|
|
|
|
return result
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_user_data_json(self):
|
|
|
|
def _get_user_data_json(self):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
# this data come form users, we try what we can to parse it...
|
|
|
|
# this data come form users, we try what we can to parse it...
|
|
|
@ -151,7 +154,6 @@ class CloudStackFacts(object):
|
|
|
|
except:
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _fetch(self, path):
|
|
|
|
def _fetch(self, path):
|
|
|
|
api_ip = self._get_api_ip()
|
|
|
|
api_ip = self._get_api_ip()
|
|
|
|
if not api_ip:
|
|
|
|
if not api_ip:
|
|
|
@ -164,22 +166,20 @@ class CloudStackFacts(object):
|
|
|
|
data = None
|
|
|
|
data = None
|
|
|
|
return data
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_dhcp_lease_file(self):
|
|
|
|
def _get_dhcp_lease_file(self):
|
|
|
|
"""Return the path of the lease file."""
|
|
|
|
"""Return the path of the lease file."""
|
|
|
|
default_iface = self.facts['default_ipv4']['interface']
|
|
|
|
default_iface = self.facts['default_ipv4']['interface']
|
|
|
|
dhcp_lease_file_locations = [
|
|
|
|
dhcp_lease_file_locations = [
|
|
|
|
'/var/lib/dhcp/dhclient.%s.leases' % default_iface, # debian / ubuntu
|
|
|
|
'/var/lib/dhcp/dhclient.%s.leases' % default_iface, # debian / ubuntu
|
|
|
|
'/var/lib/dhclient/dhclient-%s.leases' % default_iface, # centos 6
|
|
|
|
'/var/lib/dhclient/dhclient-%s.leases' % default_iface, # centos 6
|
|
|
|
'/var/lib/dhclient/dhclient--%s.lease' % default_iface, # centos 7
|
|
|
|
'/var/lib/dhclient/dhclient--%s.lease' % default_iface, # centos 7
|
|
|
|
'/var/db/dhclient.leases.%s' % default_iface, # openbsd
|
|
|
|
'/var/db/dhclient.leases.%s' % default_iface, # openbsd
|
|
|
|
]
|
|
|
|
]
|
|
|
|
for file_path in dhcp_lease_file_locations:
|
|
|
|
for file_path in dhcp_lease_file_locations:
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
return file_path
|
|
|
|
return file_path
|
|
|
|
module.fail_json(msg="Could not find dhclient leases file.")
|
|
|
|
module.fail_json(msg="Could not find dhclient leases file.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_api_ip(self):
|
|
|
|
def _get_api_ip(self):
|
|
|
|
"""Return the IP of the DHCP server."""
|
|
|
|
"""Return the IP of the DHCP server."""
|
|
|
|
if not self.api_ip:
|
|
|
|
if not self.api_ip:
|
|
|
@ -198,8 +198,8 @@ class CloudStackFacts(object):
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
global module
|
|
|
|
global module
|
|
|
|
module = AnsibleModule(
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
argument_spec=dict(
|
|
|
|
filter = dict(default=None, choices=[
|
|
|
|
filter=dict(default=None, choices=[
|
|
|
|
'cloudstack_service_offering',
|
|
|
|
'cloudstack_service_offering',
|
|
|
|
'cloudstack_availability_zone',
|
|
|
|
'cloudstack_availability_zone',
|
|
|
|
'cloudstack_public_hostname',
|
|
|
|
'cloudstack_public_hostname',
|
|
|
@ -213,15 +213,13 @@ def main():
|
|
|
|
supports_check_mode=False
|
|
|
|
supports_check_mode=False
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if not has_lib_yaml:
|
|
|
|
if not HAS_LIB_YAML:
|
|
|
|
module.fail_json(msg="missing python library: yaml")
|
|
|
|
module.fail_json(msg="missing python library: yaml")
|
|
|
|
|
|
|
|
|
|
|
|
cs_facts = CloudStackFacts().run()
|
|
|
|
cs_facts = CloudStackFacts().run()
|
|
|
|
cs_facts_result = dict(changed=False, ansible_facts=cs_facts)
|
|
|
|
cs_facts_result = dict(changed=False, ansible_facts=cs_facts)
|
|
|
|
module.exit_json(**cs_facts_result)
|
|
|
|
module.exit_json(**cs_facts_result)
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
|
|
|
from ansible.module_utils.urls import *
|
|
|
|
|
|
|
|
from ansible.module_utils.facts import *
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|