From d11182b80b90ae8d01b6b7fdb34a617e91ce9989 Mon Sep 17 00:00:00 2001 From: Darren Worrall Date: Tue, 11 Aug 2015 14:20:22 +0100 Subject: [PATCH 1/4] Add iptonetwork parameter --- cloud/cloudstack/cs_instance.py | 38 +++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/cloud/cloudstack/cs_instance.py b/cloud/cloudstack/cs_instance.py index f17269848e6..b55fc1b97ee 100644 --- a/cloud/cloudstack/cs_instance.py +++ b/cloud/cloudstack/cs_instance.py @@ -97,6 +97,12 @@ options: - IPv6 address for default instance's network. required: false default: null + iptonetwork: + description: + - List of mappings in the form {'network': NetworkName, 'ip': 1.2.3.4} + - Mutually exclusive with C(networks) option. + required: false + default: null disk_offering: description: - Name of the disk offering to be used. @@ -209,6 +215,16 @@ EXAMPLES = ''' - { key: admin, value: john } - { key: foo, value: bar } +# Create an instance with multiple interfaces specifying the IP addresses +- local_action: + module: cs_instance + name: web-vm-1 + template: Linux Debian 7 64-bit + service_offering: Tiny + iptonetwork: + - {'network': NetworkA, 'ip': '10.1.1.1'} + - {'network': NetworkB, 'ip': '192.168.1.1'} + # Ensure a instance has stopped - local_action: cs_instance name=web-vm-1 state=stopped @@ -448,9 +464,25 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): break return self.instance + def get_iptonetwork_mappings(self): + network_mappings = self.module.params.get('iptonetwork') + if network_mappings is None: + return + + if network_mappings and self.module.params.get('networks'): + self.module.fail_json(msg="networks and iptonetwork are mutually exclusive.") + + network_names = [n['network'] for n in network_mappings] + ids = self.get_network_ids(network_names).split(',') + res = [] + for i, data in enumerate(network_mappings): + res.append({'networkid': ids[i], 'ip': data['ip']}) + return res + + def get_network_ids(self, network_names=None): + if network_names is None: + network_names = self.module.params.get('networks') - def get_network_ids(self): - network_names = self.module.params.get('networks') if not network_names: return None @@ -512,6 +544,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): args['projectid'] = self.get_project(key='id') args['diskofferingid'] = self.get_disk_offering_id() args['networkids'] = self.get_network_ids() + args['iptonetworklist'] = self.get_iptonetwork_mappings() args['userdata'] = self.get_user_data() args['keyboard'] = self.module.params.get('keyboard') args['ipaddress'] = self.module.params.get('ip_address') @@ -785,6 +818,7 @@ def main(): template = dict(default=None), iso = dict(default=None), networks = dict(type='list', aliases=[ 'network' ], default=None), + iptonetwork = dict(type='list', default=None), ip_address = dict(defaul=None), ip6_address = dict(defaul=None), disk_offering = dict(default=None), From ecfd18a94185d032b2383f0c638a95a73a58a258 Mon Sep 17 00:00:00 2001 From: Darren Worrall Date: Tue, 11 Aug 2015 15:33:20 +0100 Subject: [PATCH 2/4] Rename param to ip_to_networks --- cloud/cloudstack/cs_instance.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cloud/cloudstack/cs_instance.py b/cloud/cloudstack/cs_instance.py index b55fc1b97ee..89882108c47 100644 --- a/cloud/cloudstack/cs_instance.py +++ b/cloud/cloudstack/cs_instance.py @@ -97,7 +97,7 @@ options: - IPv6 address for default instance's network. required: false default: null - iptonetwork: + ip_to_networks: description: - List of mappings in the form {'network': NetworkName, 'ip': 1.2.3.4} - Mutually exclusive with C(networks) option. @@ -221,7 +221,7 @@ EXAMPLES = ''' name: web-vm-1 template: Linux Debian 7 64-bit service_offering: Tiny - iptonetwork: + ip_to_networks: - {'network': NetworkA, 'ip': '10.1.1.1'} - {'network': NetworkB, 'ip': '192.168.1.1'} @@ -465,12 +465,12 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): return self.instance def get_iptonetwork_mappings(self): - network_mappings = self.module.params.get('iptonetwork') + network_mappings = self.module.params.get('ip_to_networks') if network_mappings is None: return if network_mappings and self.module.params.get('networks'): - self.module.fail_json(msg="networks and iptonetwork are mutually exclusive.") + self.module.fail_json(msg="networks and ip_to_networks are mutually exclusive.") network_names = [n['network'] for n in network_mappings] ids = self.get_network_ids(network_names).split(',') @@ -818,7 +818,7 @@ def main(): template = dict(default=None), iso = dict(default=None), networks = dict(type='list', aliases=[ 'network' ], default=None), - iptonetwork = dict(type='list', default=None), + ip_to_networks = dict(type='list', default=None), ip_address = dict(defaul=None), ip6_address = dict(defaul=None), disk_offering = dict(default=None), From 53e447e38e0cc8451265b81189d13f084163cb6d Mon Sep 17 00:00:00 2001 From: Darren Worrall Date: Tue, 11 Aug 2015 15:41:07 +0100 Subject: [PATCH 3/4] Api tidy up --- cloud/cloudstack/cs_instance.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cloud/cloudstack/cs_instance.py b/cloud/cloudstack/cs_instance.py index 89882108c47..6ca3a9f3b38 100644 --- a/cloud/cloudstack/cs_instance.py +++ b/cloud/cloudstack/cs_instance.py @@ -473,7 +473,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): self.module.fail_json(msg="networks and ip_to_networks are mutually exclusive.") network_names = [n['network'] for n in network_mappings] - ids = self.get_network_ids(network_names).split(',') + ids = self.get_network_ids(network_names) res = [] for i, data in enumerate(network_mappings): res.append({'networkid': ids[i], 'ip': data['ip']}) @@ -508,7 +508,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): if len(network_ids) != len(network_names): self.module.fail_json(msg="Could not find all networks, networks list found: %s" % network_displaytexts) - return ','.join(network_ids) + return network_ids def present_instance(self): @@ -534,6 +534,9 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): def deploy_instance(self): self.result['changed'] = True + networkids = self.get_network_ids() + if networkids is not None: + networkids = ','.join(networkids) args = {} args['templateid'] = self.get_template_or_iso(key='id') @@ -543,7 +546,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): args['domainid'] = self.get_domain(key='id') args['projectid'] = self.get_project(key='id') args['diskofferingid'] = self.get_disk_offering_id() - args['networkids'] = self.get_network_ids() + args['networkids'] = networkids args['iptonetworklist'] = self.get_iptonetwork_mappings() args['userdata'] = self.get_user_data() args['keyboard'] = self.module.params.get('keyboard') From 2be506dbdf64b1d4367d5d3f9aec7fae822963fc Mon Sep 17 00:00:00 2001 From: Darren Worrall Date: Tue, 11 Aug 2015 15:42:55 +0100 Subject: [PATCH 4/4] Add alias --- cloud/cloudstack/cs_instance.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cloud/cloudstack/cs_instance.py b/cloud/cloudstack/cs_instance.py index 6ca3a9f3b38..7bd06a797d9 100644 --- a/cloud/cloudstack/cs_instance.py +++ b/cloud/cloudstack/cs_instance.py @@ -103,6 +103,7 @@ options: - Mutually exclusive with C(networks) option. required: false default: null + aliases: [ 'ip_to_network' ] disk_offering: description: - Name of the disk offering to be used. @@ -821,7 +822,7 @@ def main(): template = dict(default=None), iso = dict(default=None), networks = dict(type='list', aliases=[ 'network' ], default=None), - ip_to_networks = dict(type='list', default=None), + ip_to_networks = dict(type='list', aliases=['ip_to_network'], default=None), ip_address = dict(defaul=None), ip6_address = dict(defaul=None), disk_offering = dict(default=None),