From e8f1747a88e9e8f75bf92f963f3750e65bb034ac Mon Sep 17 00:00:00 2001 From: Khachatur Nazaretyan Date: Sun, 9 Apr 2017 11:59:41 +0200 Subject: [PATCH] Adds attached volumes to instance facts (#23132) * Adds attached volumes to instance facts * Fixed identation and modified coding style * includes revisions after maintainers review * Coding style modified * simplifies getting items from dict --- .../cloud/cloudstack/cs_instance_facts.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py b/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py index 467e4013fbc..ecae30cc202 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py @@ -180,6 +180,11 @@ cloudstack_instance.instance_name: returned: success type: string sample: i-44-3992-VM +cloudstack_instance.volumes: + description: List of dictionaries of the volumes attached to the instance. + returned: success + type: list + sample: '[ { name: "ROOT-1369", type: "ROOT", size: 10737418240 }, { name: "data01, type: "DATADISK", size: 10737418240 } ]' ''' import base64 @@ -227,6 +232,20 @@ class AnsibleCloudStackInstanceFacts(AnsibleCloudStack): break return self.instance + def get_volumes(self, instance): + volume_details = [] + if instance: + args = {} + args['account'] = instance.get('account') + args['domainid'] = instance.get('domainid') + args['projectid'] = instance.get('projectid') + args['virtualmachineid'] = instance['id'] + + volumes = self.cs.listVolumes(**args) + if volumes: + for vol in volumes['volume']: + volume_details.append({'size': vol['size'], 'type': vol['type'], 'name': vol['name']}) + return volume_details def run(self): instance = self.get_instance() @@ -253,6 +272,9 @@ class AnsibleCloudStackInstanceFacts(AnsibleCloudStack): for nic in instance['nic']: if nic['isdefault'] and 'ipaddress' in nic: self.result['default_ip'] = nic['ipaddress'] + volumes = self.get_volumes(instance) + if volumes: + self.result['volumes'] = volumes return self.result