Skip to content

Commit

Permalink
Add ansible roles parameter to hostgroup module
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Mar 16, 2021
1 parent dbd8ab2 commit 34bf70d
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 652 deletions.
24 changes: 24 additions & 0 deletions plugins/modules/hostgroup.py
Expand Up @@ -55,6 +55,12 @@
parameters:
description:
- Hostgroup specific host parameters
ansible_roles:
description:
- A list of ansible roles to associate with the hostgroup.
required: false
type: list
elements: str
extends_documentation_fragment:
- theforeman.foreman.foreman
- theforeman.foreman.foreman.entity_state
Expand Down Expand Up @@ -159,6 +165,7 @@ def main():
name=dict(required=True),
description=dict(),
parent=dict(type='entity'),
ansible_roles=dict(type='entity_list', ensure=False),
organization=dict(type='entity', required=False, ensure=False),
),
argument_spec=dict(
Expand All @@ -173,6 +180,7 @@ def main():

module_params = module.foreman_params
with module.api_connection():
old_entity = module.lookup_entity('entity')
if not module.desired_absent:
if 'organization' in module_params:
if 'organizations' in module_params:
Expand All @@ -182,9 +190,25 @@ def main():
module_params['organizations'] = [module_params['organization']]
expected_puppetclasses = module_params.pop('puppetclasses', None)
entity = module.run()

if not module.desired_absent and 'environment_id' in entity:
ensure_puppetclasses(module, 'hostgroup', entity, expected_puppetclasses)

ansible_roles = module_params.get('ansible_roles')
if not module.desired_absent and ansible_roles is not None:
desired_ansible_role_ids = [item['id'] for item in ansible_roles]
current_ansible_role_ids = [
item['id'] for item in module.resource_action(
'hostgroups', 'ansible_roles', {'id': entity['id']},
ignore_check_mode=True, record_change=False,
)
] if old_entity else []
if set(current_ansible_role_ids) != set(desired_ansible_role_ids):
module.resource_action(
'hostgroups', 'assign_ansible_roles',
{'id': entity['id'], 'ansible_role_ids': desired_ansible_role_ids},
)


if __name__ == '__main__':
main()

0 comments on commit 34bf70d

Please sign in to comment.