mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
cloud: ovirt: support add/remove tag from vms/hosts (#25900)
This commit is contained in:
parent
170fc42e80
commit
5f1da6809a
1 changed files with 51 additions and 25 deletions
|
@ -40,8 +40,9 @@ options:
|
||||||
required: true
|
required: true
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- "Should the tag be present or absent."
|
- "Should the tag be present/absent/attached/detached."
|
||||||
choices: ['present', 'absent']
|
- "C(Note): I(attached) and I(detached) states are supported since version 2.4."
|
||||||
|
choices: ['present', 'absent', 'attached', 'detached']
|
||||||
default: present
|
default: present
|
||||||
description:
|
description:
|
||||||
description:
|
description:
|
||||||
|
@ -69,6 +70,20 @@ EXAMPLES = '''
|
||||||
- vm1
|
- vm1
|
||||||
- vm2
|
- vm2
|
||||||
|
|
||||||
|
# Attach a tag to VM 'vm1', keeping the rest already attached tags on VM:
|
||||||
|
- ovirt_tags:
|
||||||
|
name: mytag
|
||||||
|
state: attached
|
||||||
|
vms:
|
||||||
|
- vm3
|
||||||
|
|
||||||
|
# Detach a tag from VM 'vm1', keeping the rest already attached tags on VM:
|
||||||
|
- ovirt_tags:
|
||||||
|
name: mytag
|
||||||
|
state: detached
|
||||||
|
vms:
|
||||||
|
- vm3
|
||||||
|
|
||||||
# To detach all VMs from tag:
|
# To detach all VMs from tag:
|
||||||
- ovirt_tags:
|
- ovirt_tags:
|
||||||
name: mytag
|
name: mytag
|
||||||
|
@ -106,8 +121,8 @@ from ansible.module_utils.ovirt import (
|
||||||
check_sdk,
|
check_sdk,
|
||||||
create_connection,
|
create_connection,
|
||||||
equal,
|
equal,
|
||||||
|
get_id_by_name,
|
||||||
ovirt_full_argument_spec,
|
ovirt_full_argument_spec,
|
||||||
search_by_name,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -129,17 +144,20 @@ class TagsModule(BaseModule):
|
||||||
if self._module.params[name] is None:
|
if self._module.params[name] is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
state = self.param('state')
|
||||||
entities_service = getattr(self._connection.system_service(), '%s_service' % name)()
|
entities_service = getattr(self._connection.system_service(), '%s_service' % name)()
|
||||||
current_vms = [
|
current_vms = [
|
||||||
vm.name
|
vm.name
|
||||||
for vm in entities_service.list(search='tag=%s' % self._module.params['name'])
|
for vm in entities_service.list(search='tag=%s' % self._module.params['name'])
|
||||||
]
|
]
|
||||||
# Assign tags:
|
# Assign tags:
|
||||||
|
if state in ['present', 'attached', 'detached']:
|
||||||
for entity_name in self._module.params[name]:
|
for entity_name in self._module.params[name]:
|
||||||
entity = search_by_name(entities_service, entity_name)
|
entity_id = get_id_by_name(entities_service, entity_name)
|
||||||
tags_service = entities_service.service(entity.id).tags_service()
|
tags_service = entities_service.service(entity_id).tags_service()
|
||||||
current_tags = [tag.name for tag in tags_service.list()]
|
current_tags = [tag.name for tag in tags_service.list()]
|
||||||
# Assign the tag:
|
# Assign the tag:
|
||||||
|
if state in ['attached', 'present']:
|
||||||
if self._module.params['name'] not in current_tags:
|
if self._module.params['name'] not in current_tags:
|
||||||
if not self._module.check_mode:
|
if not self._module.check_mode:
|
||||||
tags_service.add(
|
tags_service.add(
|
||||||
|
@ -148,13 +166,21 @@ class TagsModule(BaseModule):
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
self.changed = True
|
self.changed = True
|
||||||
|
# Detach the tag:
|
||||||
|
elif state == 'detached':
|
||||||
|
if self._module.params['name'] in current_tags:
|
||||||
|
tag_id = get_id_by_name(tags_service, self.param('name'))
|
||||||
|
if not self._module.check_mode:
|
||||||
|
tags_service.tag_service(tag_id).remove()
|
||||||
|
self.changed = True
|
||||||
|
|
||||||
# Unassign tags:
|
# Unassign tags:
|
||||||
|
if state == 'present':
|
||||||
for entity_name in [e for e in current_vms if e not in self._module.params[name]]:
|
for entity_name in [e for e in current_vms if e not in self._module.params[name]]:
|
||||||
if not self._module.check_mode:
|
if not self._module.check_mode:
|
||||||
entity = search_by_name(entities_service, entity_name)
|
entity_id = get_id_by_name(entities_service, entity_name)
|
||||||
tags_service = entities_service.service(entity.id).tags_service()
|
tags_service = entities_service.service(entity_id).tags_service()
|
||||||
tag_id = [tag.id for tag in tags_service.list() if tag.name == self._module.params['name']][0]
|
tag_id = get_id_by_name(tags_service, self.param('name'))
|
||||||
tags_service.tag_service(tag_id).remove()
|
tags_service.tag_service(tag_id).remove()
|
||||||
self.changed = True
|
self.changed = True
|
||||||
|
|
||||||
|
@ -176,7 +202,7 @@ class TagsModule(BaseModule):
|
||||||
def main():
|
def main():
|
||||||
argument_spec = ovirt_full_argument_spec(
|
argument_spec = ovirt_full_argument_spec(
|
||||||
state=dict(
|
state=dict(
|
||||||
choices=['present', 'absent'],
|
choices=['present', 'absent', 'attached', 'detached'],
|
||||||
default='present',
|
default='present',
|
||||||
),
|
),
|
||||||
name=dict(default=None, required=True),
|
name=dict(default=None, required=True),
|
||||||
|
@ -202,7 +228,7 @@ def main():
|
||||||
)
|
)
|
||||||
|
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
if state == 'present':
|
if state in ['present', 'attached', 'detached']:
|
||||||
ret = tags_module.create()
|
ret = tags_module.create()
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
ret = tags_module.remove()
|
ret = tags_module.remove()
|
||||||
|
|
Loading…
Reference in a new issue