From c53172cb960d16f50f4e6367492c4248442a31bf Mon Sep 17 00:00:00 2001 From: milkmix Date: Mon, 24 Jun 2019 12:16:59 +0200 Subject: [PATCH] standalone cloudstack zones module --- library/cloudstack_zones.py | 110 +++++++++++++++++++++++ roles/cloud-cloudstack/tasks/prompts.yml | 12 ++- 2 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 library/cloudstack_zones.py diff --git a/library/cloudstack_zones.py b/library/cloudstack_zones.py new file mode 100644 index 0000000..eba1b2b --- /dev/null +++ b/library/cloudstack_zones.py @@ -0,0 +1,110 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.cloudstack import ( + AnsibleCloudStack, + cs_argument_spec, + cs_required_together, +) + +DOCUMENTATION = ''' +--- +module: cloudstack_zones +short_description: List zones on Apache CloudStack based clouds. +description: + - List zones. +version_added: '0.1' +author: Julien Bachmann (@0xmilkmix) +extends_documentation_fragment: cloudstack +''' + +EXAMPLES = ''' +- name: List zones + cloudstack_zones: + register: _cs_zones +''' + +RETURN = ''' +--- +zone: + description: List of zones. + returned: success + type: list + sample: + [ + { + "allocationstate": "Enabled", + "dhcpprovider": "VirtualRouter", + "id": "1128bd56-b4d9-4ac6-a7b9-c715b187ce11", + "localstorageenabled": true, + "name": "ch-gva-2", + "networktype": "Basic", + "securitygroupsenabled": true, + "tags": [], + "zonetoken": "token" + }, + { + "allocationstate": "Enabled", + "dhcpprovider": "VirtualRouter", + "id": "91e5e9e4-c9ed-4b76-bee4-427004b3baf9", + "localstorageenabled": true, + "name": "ch-dk-2", + "networktype": "Basic", + "securitygroupsenabled": true, + "tags": [], + "zonetoken": "token" + }, + { + "allocationstate": "Enabled", + "dhcpprovider": "VirtualRouter", + "id": "4da1b188-dcd6-4ff5-b7fd-bde984055548", + "localstorageenabled": true, + "name": "at-vie-1", + "networktype": "Basic", + "securitygroupsenabled": true, + "tags": [], + "zonetoken": "token" + }, + { + "allocationstate": "Enabled", + "dhcpprovider": "VirtualRouter", + "id": "35eb7739-d19e-45f7-a581-4687c54d6d02", + "localstorageenabled": true, + "name": "de-fra-1", + "networktype": "Basic", + "securitygroupsenabled": true, + "tags": [], + "zonetoken": "token" + } + ] +''' + +class AnsibleCloudStackZones(AnsibleCloudStack): + + def __init__(self, module): + super(AnsibleCloudStackZones, self).__init__(module) + self.zones = None + + def get_zones(self): + args = {} + if not self.zones: + zones = self.query_api('listZones', **args) + if zones: + self.zones = zones + return self.zones + +def main(): + module = AnsibleModule(argument_spec={}) + acs_zones = AnsibleCloudStackZones(module) + result = acs_zones.get_zones() + module.exit_json(**result) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/roles/cloud-cloudstack/tasks/prompts.yml b/roles/cloud-cloudstack/tasks/prompts.yml index f17c6bd..547fbd7 100644 --- a/roles/cloud-cloudstack/tasks/prompts.yml +++ b/roles/cloud-cloudstack/tasks/prompts.yml @@ -17,13 +17,17 @@ [exoscale] register: _cs_region - - name: Parse zones from output - set_fact: - _cs_zones: "{{ _cloudstack_zones | from_json }}" + # - name: Parse zones from output + # set_fact: + # _cs_zones: "{{ _cloudstack_zones | from_json }}" + + - name: Get zones on cloud + cloudstack_zones: + register: _cs_zones - name: Extract zones from output set_fact: - cs_zones: "{{ _cs_zones | sort(attribute='name') }}" + cs_zones: "{{ _cs_zones['zone'] | sort(attribute='name') }}" - name: Set the default zone set_fact: