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 18, 2021
1 parent dbd8ab2 commit e0122d2
Show file tree
Hide file tree
Showing 11 changed files with 1,643 additions and 605 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1123-ansible-roles-for-hostgroups.yml
@@ -0,0 +1,2 @@
minor_changes:
- hostgroup - add a ``ansible_roles parameter`` (https://github.com/theforeman/foreman-ansible-modules/issues/1123)
1 change: 1 addition & 0 deletions plugins/module_utils/foreman_helper.py
Expand Up @@ -56,6 +56,7 @@
parameter_ansible_spec = {k: v for (k, v) in parameter_foreman_spec.items() if k != 'id'}

_PLUGIN_RESOURCES = {
'ansible': 'ansible_roles',
'discovery': 'discovery_rules',
'katello': 'subscriptions',
'openscap': 'scap_contents',
Expand Down
27 changes: 27 additions & 0 deletions plugins/modules/hostgroup.py
Expand Up @@ -55,6 +55,14 @@
parameters:
description:
- Hostgroup specific host parameters
ansible_roles:
description:
- A list of ansible roles to associate with the hostgroup.
- The foreman-ansible plugin must be installed to use this parameter.
required: false
type: list
elements: str
version_added: 2.1.0
extends_documentation_fragment:
- theforeman.foreman.foreman
- theforeman.foreman.foreman.entity_state
Expand Down Expand Up @@ -159,6 +167,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 @@ -169,10 +178,12 @@ def main():
content_view=('organization',),
lifecycle_environment=('organization',),
),
required_plugins=[('ansible', ['ansible_roles'])],
)

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 +193,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 e0122d2

Please sign in to comment.