From 88bb555ab83ad6872d9fde9a1c9913710c13577f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ne=C4=8Das?= Date: Thu, 17 Jan 2019 20:24:22 +0100 Subject: [PATCH] ovirt hosts facts add cluster version filtration (#48664) * ovirt hosts facts add cluster version filtration * update filtering of ovirt hosts and input of cluster version * ovirt host facts change compat version to string --- .../modules/cloud/ovirt/ovirt_host_facts.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_host_facts.py b/lib/ansible/modules/cloud/ovirt/ovirt_host_facts.py index e8a5d683679..2da2e9ba280 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_host_facts.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_host_facts.py @@ -33,6 +33,12 @@ options: default: False version_added: "2.7" type: bool + cluster_version: + description: + - "Filter the hosts based on the cluster version." + type: str + version_added: "2.8" + extends_documentation_fragment: ovirt_facts ''' @@ -46,6 +52,12 @@ EXAMPLES = ''' pattern: name=host* and datacenter=west - debug: var: ovirt_hosts +# All hosts with cluster version 4.2: +- ovirt_host_facts: + pattern: name=host* + cluster_version: "4.2" +- debug: + var: ovirt_hosts ''' RETURN = ''' @@ -67,10 +79,22 @@ from ansible.module_utils.ovirt import ( ) +def get_filtered_hosts(cluster_version, hosts): + # Filtering by cluster version returns only those which have same cluster version as input + filtered_hosts = [] + for host in hosts: + cluster = host.cluster + cluster_version_host = str(cluster.version.major) + '.' + str(cluster.version.minor) + if cluster_version_host == cluster_version: + filtered_hosts.append(host) + return filtered_hosts + + def main(): argument_spec = ovirt_facts_full_argument_spec( pattern=dict(default='', required=False), all_content=dict(default=False, type='bool'), + cluster_version=dict(default=None, type='str'), ) module = AnsibleModule(argument_spec) @@ -83,7 +107,11 @@ def main(): hosts = hosts_service.list( search=module.params['pattern'], all_content=module.params['all_content'], + follow='cluster', ) + cluster_version = module.params.get('cluster_version') + if cluster_version is not None: + hosts = get_filtered_hosts(cluster_version, hosts) module.exit_json( changed=False, ansible_facts=dict(