From 16994bbdcaf8c5324f7d537a949949d175fa7512 Mon Sep 17 00:00:00 2001 From: Peter Farmer Date: Sat, 12 May 2018 08:46:49 +0100 Subject: [PATCH] cloudstack: use paging for listVirtualMachines (#40018) Paging wasn't implemented, so once a cs domain has over 500 (default page size) VMs, Ansible can no longer find newly created VM. --- contrib/inventory/cloudstack.py | 8 ++++---- lib/ansible/module_utils/cloudstack.py | 3 ++- lib/ansible/modules/cloud/cloudstack/cs_instance.py | 3 ++- lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py | 3 ++- .../cloud/cloudstack/cs_loadbalancer_rule_member.py | 3 ++- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/contrib/inventory/cloudstack.py b/contrib/inventory/cloudstack.py index d9dabbc545..0d5d67bb14 100755 --- a/contrib/inventory/cloudstack.py +++ b/contrib/inventory/cloudstack.py @@ -147,11 +147,11 @@ class CloudStackInventory(object): sys.exit(1) def get_host(self, name, project_id=None, domain_id=None, **kwargs): - hosts = self.cs.listVirtualMachines(projectid=project_id, domainid=domain_id, **kwargs) + hosts = self.cs.listVirtualMachines(projectid=project_id, domainid=domain_id, fetch_list=True, **kwargs) data = {} if not hosts: return data - for host in hosts['virtualmachine']: + for host in hosts: host_name = host['displayname'] if name == host_name: data['zone'] = host['zonename'] @@ -202,10 +202,10 @@ class CloudStackInventory(object): 'hosts': [] } - hosts = self.cs.listVirtualMachines(projectid=project_id, domainid=domain_id, **kwargs) + hosts = self.cs.listVirtualMachines(projectid=project_id, domainid=domain_id, fetch_list=True, **kwargs) if not hosts: return data - for host in hosts['virtualmachine']: + for host in hosts: host_name = host['displayname'] data['all']['hosts'].append(host_name) data['_meta']['hostvars'][host_name] = {} diff --git a/lib/ansible/module_utils/cloudstack.py b/lib/ansible/module_utils/cloudstack.py index 3706b877f2..b604957325 100644 --- a/lib/ansible/module_utils/cloudstack.py +++ b/lib/ansible/module_utils/cloudstack.py @@ -413,10 +413,11 @@ class AnsibleCloudStack: 'domainid': self.get_domain(key='id'), 'projectid': self.get_project(key='id'), 'zoneid': self.get_zone(key='id') if filter_zone else None, + 'fetch_list': True, } vms = self.query_api('listVirtualMachines', **args) if vms: - for v in vms['virtualmachine']: + for v in vms: if vm.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]: self.vm = v return self._get_by_key(key, self.vm) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_instance.py b/lib/ansible/modules/cloud/cloudstack/cs_instance.py index 308974dddd..347fcc6a82 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_instance.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_instance.py @@ -457,11 +457,12 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): 'account': self.get_account(key='name'), 'domainid': self.get_domain(key='id'), 'projectid': self.get_project(key='id'), + 'fetch_list': True, } # Do not pass zoneid, as the instance name must be unique across zones. instances = self.query_api('listVirtualMachines', **args) if instances: - for v in instances['virtualmachine']: + for v in instances: if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]: self.instance = v break diff --git a/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py b/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py index 49e8f9bb50..ed56200d07 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py @@ -206,11 +206,12 @@ class AnsibleCloudStackInstanceFacts(AnsibleCloudStack): 'account': self.get_account(key='name'), 'domainid': self.get_domain(key='id'), 'projectid': self.get_project(key='id'), + 'fetch_list': True, } # Do not pass zoneid, as the instance name must be unique across zones. instances = self.query_api('listVirtualMachines', **args) if instances: - for v in instances['virtualmachine']: + for v in instances: if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]: self.instance = v break diff --git a/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule_member.py b/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule_member.py index 34579f72e0..b19ad2021e 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule_member.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule_member.py @@ -269,10 +269,11 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack): return rule args = self._get_common_args() + args['fetch_list'] = True vms = self.query_api('listVirtualMachines', **args) to_change_ids = [] for name in to_change: - for vm in vms.get('virtualmachine', []): + for vm in vms: if vm['name'] == name: to_change_ids.append(vm['id']) break