| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
__metaclass__ = type |
| 20 |
|
| 21 |
|
| 22 |
DOCUMENTATION = ''' |
| 23 |
--- |
| 24 |
module: hostgroup |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Hostgroups |
| 27 |
description: |
| 28 |
- Create, update, and delete Hostgroups |
| 29 |
author: |
| 30 |
- "Manisha Singhal (@Manisha15) ATIX AG" |
| 31 |
- "Baptiste Agasse (@bagasse)" |
| 32 |
options: |
| 33 |
name: |
| 34 |
description: Name of hostgroup |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
updated_name: |
| 38 |
description: New name of hostgroup. When this parameter is set, the module will not be idempotent. |
| 39 |
type: str |
| 40 |
description: |
| 41 |
description: Description of hostgroup |
| 42 |
required: false |
| 43 |
type: str |
| 44 |
parent: |
| 45 |
description: |
| 46 |
- Title of the parent Hostgroup. |
| 47 |
- Can be nested in the form "Main Group/Sub Group". |
| 48 |
required: false |
| 49 |
type: str |
| 50 |
organization: |
| 51 |
description: |
| 52 |
- Organization for scoped resources attached to the hostgroup. |
| 53 |
- Only used for Katello installations. |
| 54 |
- This organization will implicitly be added to the I(organizations) parameter if needed. |
| 55 |
required: false |
| 56 |
type: str |
| 57 |
parameters: |
| 58 |
description: |
| 59 |
- Hostgroup specific host parameters |
| 60 |
ansible_roles: |
| 61 |
description: |
| 62 |
- A list of ansible roles to associate with the hostgroup. |
| 63 |
- The foreman-ansible plugin must be installed to use this parameter. |
| 64 |
required: false |
| 65 |
type: list |
| 66 |
elements: str |
| 67 |
version_added: 2.1.0 |
| 68 |
extends_documentation_fragment: |
| 69 |
- theforeman.foreman.foreman |
| 70 |
- theforeman.foreman.foreman.entity_state |
| 71 |
- theforeman.foreman.foreman.taxonomy |
| 72 |
- theforeman.foreman.foreman.nested_parameters |
| 73 |
- theforeman.foreman.foreman.host_options |
| 74 |
- theforeman.foreman.foreman.operatingsystem |
| 75 |
''' |
| 76 |
|
| 77 |
EXAMPLES = ''' |
| 78 |
- name: "Create a Hostgroup" |
| 79 |
theforeman.foreman.hostgroup: |
| 80 |
name: "new_hostgroup" |
| 81 |
architecture: "architecture_name" |
| 82 |
operatingsystem: "operatingsystem_name" |
| 83 |
medium: "media_name" |
| 84 |
ptable: "Partition_table_name" |
| 85 |
server_url: "https://foreman.example.com" |
| 86 |
username: "admin" |
| 87 |
password: "changeme" |
| 88 |
state: present |
| 89 |
|
| 90 |
- name: "Update a Hostgroup" |
| 91 |
theforeman.foreman.hostgroup: |
| 92 |
name: "new_hostgroup" |
| 93 |
architecture: "updated_architecture_name" |
| 94 |
operatingsystem: "updated_operatingsystem_name" |
| 95 |
organizations: |
| 96 |
- Org One |
| 97 |
- Org Two |
| 98 |
locations: |
| 99 |
- Loc One |
| 100 |
- Loc Two |
| 101 |
- Loc One/Nested loc |
| 102 |
medium: "updated_media_name" |
| 103 |
ptable: "updated_Partition_table_name" |
| 104 |
root_pass: "password" |
| 105 |
server_url: "https://foreman.example.com" |
| 106 |
username: "admin" |
| 107 |
password: "changeme" |
| 108 |
state: present |
| 109 |
|
| 110 |
- name: "My nested hostgroup" |
| 111 |
theforeman.foreman.hostgroup: |
| 112 |
parent: "new_hostgroup" |
| 113 |
name: "my nested hostgroup" |
| 114 |
|
| 115 |
- name: "My hostgroup with some proxies" |
| 116 |
theforeman.foreman.hostgroup: |
| 117 |
name: "my hostgroup" |
| 118 |
environment: production |
| 119 |
puppet_proxy: puppet-proxy.example.com |
| 120 |
puppet_ca_proxy: puppet-proxy.example.com |
| 121 |
openscap_proxy: openscap-proxy.example.com |
| 122 |
|
| 123 |
- name: "My katello related hostgroup" |
| 124 |
theforeman.foreman.hostgroup: |
| 125 |
organization: "My Org" |
| 126 |
name: "kt hostgroup" |
| 127 |
content_source: capsule.example.com |
| 128 |
lifecycle_environment: "Production" |
| 129 |
content_view: "My content view" |
| 130 |
activation_keys: "my_prod_ak" |
| 131 |
|
| 132 |
- name: "Delete a Hostgroup" |
| 133 |
theforeman.foreman.hostgroup: |
| 134 |
name: "new_hostgroup" |
| 135 |
server_url: "https://foreman.example.com" |
| 136 |
username: "admin" |
| 137 |
password: "changeme" |
| 138 |
state: absent |
| 139 |
''' |
| 140 |
|
| 141 |
RETURN = ''' |
| 142 |
entity: |
| 143 |
description: Final state of the affected entities grouped by their type. |
| 144 |
returned: success |
| 145 |
type: dict |
| 146 |
contains: |
| 147 |
hostgroups: |
| 148 |
description: List of hostgroups. |
| 149 |
type: list |
| 150 |
elements: dict |
| 151 |
''' |
| 152 |
|
| 153 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ( |
| 154 |
ensure_puppetclasses, |
| 155 |
HostMixin, |
| 156 |
ForemanTaxonomicEntityAnsibleModule, |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
class ForemanHostgroupModule(HostMixin, ForemanTaxonomicEntityAnsibleModule): |
| 161 |
PARAMETERS_FLAT_NAME = 'group_parameters_attributes' |
| 162 |
|
| 163 |
|
| 164 |
def main(): |
| 165 |
module = ForemanHostgroupModule( |
| 166 |
foreman_spec=dict( |
| 167 |
name=dict(required=True), |
| 168 |
description=dict(), |
| 169 |
parent=dict(type='entity'), |
| 170 |
ansible_roles=dict(type='entity_list', ensure=False), |
| 171 |
organization=dict(type='entity', required=False, ensure=False), |
| 172 |
), |
| 173 |
argument_spec=dict( |
| 174 |
updated_name=dict(), |
| 175 |
), |
| 176 |
required_by=dict( |
| 177 |
content_source=('organization',), |
| 178 |
content_view=('organization',), |
| 179 |
lifecycle_environment=('organization',), |
| 180 |
), |
| 181 |
required_plugins=[('ansible', ['ansible_roles'])], |
| 182 |
) |
| 183 |
|
| 184 |
module_params = module.foreman_params |
| 185 |
with module.api_connection(): |
| 186 |
old_entity = module.lookup_entity('entity', params={'show_hidden_parameters': True}) |
| 187 |
if not module.desired_absent: |
| 188 |
if 'organization' in module_params: |
| 189 |
if 'organizations' in module_params: |
| 190 |
if module_params['organization'] not in module_params['organizations']: |
| 191 |
module_params['organizations'].append(module_params['organization']) |
| 192 |
else: |
| 193 |
module_params['organizations'] = [module_params['organization']] |
| 194 |
expected_puppetclasses = module_params.pop('puppetclasses', None) |
| 195 |
entity = module.run() |
| 196 |
|
| 197 |
if not module.desired_absent and 'environment_id' in entity: |
| 198 |
ensure_puppetclasses(module, 'hostgroup', entity, expected_puppetclasses) |
| 199 |
|
| 200 |
ansible_roles = module_params.get('ansible_roles') |
| 201 |
|
| 202 |
parent_ansible_role_ids = [] |
| 203 |
|
| 204 |
if not module.desired_absent and ansible_roles is not None: |
| 205 |
desired_ansible_role_ids = [item['id'] for item in ansible_roles] |
| 206 |
|
| 207 |
if entity.get('parent_id') is not None: |
| 208 |
parent_ansible_role_ids = [ |
| 209 |
item['id'] |
| 210 |
for item in module.resource_action( |
| 211 |
'hostgroups', |
| 212 |
'ansible_roles', |
| 213 |
{'id': entity['parent_id']}, |
| 214 |
ignore_check_mode=True, |
| 215 |
record_change=False, |
| 216 |
) |
| 217 |
] |
| 218 |
|
| 219 |
current_ansible_role_ids = [ |
| 220 |
item['id'] for item in module.resource_action( |
| 221 |
'hostgroups', 'ansible_roles', {'id': entity['id']}, |
| 222 |
ignore_check_mode=True, record_change=False, |
| 223 |
) |
| 224 |
] if old_entity else [] |
| 225 |
if set(current_ansible_role_ids) != set(desired_ansible_role_ids + parent_ansible_role_ids): |
| 226 |
module.resource_action( |
| 227 |
'hostgroups', 'assign_ansible_roles', |
| 228 |
{'id': entity['id'], 'ansible_role_ids': desired_ansible_role_ids}, |
| 229 |
) |
| 230 |
|
| 231 |
|
| 232 |
if __name__ == '__main__': |
| 233 |
main() |