| 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: templates_import |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Sync Templates from a repository |
| 27 |
description: |
| 28 |
- Sync provisioning templates, report_templates, partition tables and job templates from external git repository or file system. |
| 29 |
- Based on foreman_templates plugin U(https://github.com/theforeman/foreman_templates). |
| 30 |
author: |
| 31 |
- "Anton Nesterov (@nesanton)" |
| 32 |
notes: |
| 33 |
- Due to a bug in the foreman_templates plugin, this module won't report C(changed=true) |
| 34 |
when the only change is the Organization/Location association of the imported templates. |
| 35 |
Please see U(https://projects.theforeman.org/issues/29534) for details. |
| 36 |
- Default values for all module options can be set using M(theforeman.foreman.setting) for TemplateSync category or on the settings page in WebUI. |
| 37 |
options: |
| 38 |
prefix: |
| 39 |
description: |
| 40 |
- Adds specified string to beginning of all imported templates that do not yet have that prefix. |
| 41 |
required: false |
| 42 |
type: str |
| 43 |
associate: |
| 44 |
description: |
| 45 |
- Associate to Operatingsystems, Locations and Organizations based on metadata. |
| 46 |
required: false |
| 47 |
type: str |
| 48 |
choices: |
| 49 |
- always |
| 50 |
- new |
| 51 |
- never |
| 52 |
verbose: |
| 53 |
description: |
| 54 |
- Add template reports to the output. |
| 55 |
required: false |
| 56 |
type: bool |
| 57 |
force: |
| 58 |
description: |
| 59 |
- Update templates that are locked. |
| 60 |
required: false |
| 61 |
type: bool |
| 62 |
lock: |
| 63 |
description: |
| 64 |
- Lock imported templates. |
| 65 |
required: false |
| 66 |
type: bool |
| 67 |
branch: |
| 68 |
description: |
| 69 |
- Branch of the I(repo). Only for git-based repositories. |
| 70 |
required: false |
| 71 |
type: str |
| 72 |
repo: |
| 73 |
description: |
| 74 |
- Filesystem path or repo (with protocol), for example /tmp/dir or git://example.com/repo.git or https://example.com/repo.git. |
| 75 |
required: false |
| 76 |
type: str |
| 77 |
filter: |
| 78 |
description: |
| 79 |
- Sync only templates with name matching this regular expression, after I(prefix) was applied. |
| 80 |
- Case-insensitive, snippets are not filtered. |
| 81 |
required: false |
| 82 |
type: str |
| 83 |
negate: |
| 84 |
description: |
| 85 |
- Negate the filter condition. |
| 86 |
required: false |
| 87 |
type: bool |
| 88 |
dirname: |
| 89 |
description: |
| 90 |
- The directory within Git repo containing the templates. |
| 91 |
required: false |
| 92 |
type: str |
| 93 |
http_proxy_policy: |
| 94 |
description: |
| 95 |
- HTTP proxy policy for template sync. |
| 96 |
- You can choose no HTTP proxy, global HTTP proxy, or a custom HTTP proxy (C(selected)) |
| 97 |
- If you choose 'selected', provide the C(http_proxy) parameter. |
| 98 |
required: false |
| 99 |
type: str |
| 100 |
choices: |
| 101 |
- none |
| 102 |
- global |
| 103 |
- selected |
| 104 |
http_proxy: |
| 105 |
description: |
| 106 |
- HTTP proxy to use for template sync. |
| 107 |
- Use this parameter together with C(http_proxy_policy=selected) |
| 108 |
required: false |
| 109 |
type: str |
| 110 |
attributes: |
| 111 |
check_mode: |
| 112 |
support: none |
| 113 |
diff_mode: |
| 114 |
support: none |
| 115 |
extends_documentation_fragment: |
| 116 |
- theforeman.foreman.foreman |
| 117 |
- theforeman.foreman.foreman.taxonomy |
| 118 |
''' |
| 119 |
|
| 120 |
EXAMPLES = ''' |
| 121 |
- name: Sync templates from git repo |
| 122 |
theforeman.foreman.templates_import: |
| 123 |
repo: https://github.com/theforeman/community-templates.git |
| 124 |
branch: 1.24-stable |
| 125 |
associate: new |
| 126 |
server_url: "https://foreman.example.com" |
| 127 |
username: "admin" |
| 128 |
password: "changeme" |
| 129 |
''' |
| 130 |
|
| 131 |
RETURN = ''' |
| 132 |
message: |
| 133 |
description: Information about the import. |
| 134 |
returned: success |
| 135 |
type: dict |
| 136 |
contains: |
| 137 |
repo: |
| 138 |
description: Repository, the templates were imported from. |
| 139 |
type: str |
| 140 |
branch: |
| 141 |
description: Branch used in the repository. |
| 142 |
type: str |
| 143 |
report: |
| 144 |
description: Report of the import. |
| 145 |
returned: success |
| 146 |
type: dict |
| 147 |
contains: |
| 148 |
changed: |
| 149 |
description: List of templates that have been updated. |
| 150 |
type: list |
| 151 |
new: |
| 152 |
description: List of templates that have been created. |
| 153 |
type: list |
| 154 |
templates: |
| 155 |
description: Final state of the templates. |
| 156 |
returned: success |
| 157 |
type: dict |
| 158 |
''' |
| 159 |
|
| 160 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanTaxonomicAnsibleModule, _flatten_entity |
| 161 |
|
| 162 |
|
| 163 |
def main(): |
| 164 |
module = ForemanTaxonomicAnsibleModule( |
| 165 |
foreman_spec=dict( |
| 166 |
associate=dict(choices=['always', 'new', 'never']), |
| 167 |
prefix=dict(), |
| 168 |
branch=dict(), |
| 169 |
repo=dict(), |
| 170 |
filter=dict(), |
| 171 |
dirname=dict(), |
| 172 |
verbose=dict(type='bool'), |
| 173 |
force=dict(type='bool'), |
| 174 |
lock=dict(type='bool'), |
| 175 |
negate=dict(type='bool'), |
| 176 |
http_proxy_policy=dict(choices=['global', 'none', 'selected']), |
| 177 |
http_proxy=dict(type='entity'), |
| 178 |
), |
| 179 |
supports_check_mode=False, |
| 180 |
required_plugins=[('templates', ['*'])], |
| 181 |
required_if=[('http_proxy_policy', 'selected', ['http_proxy'])], |
| 182 |
) |
| 183 |
|
| 184 |
with module.api_connection(): |
| 185 |
|
| 186 |
module.auto_lookup_entities() |
| 187 |
|
| 188 |
|
| 189 |
template_report = [] |
| 190 |
|
| 191 |
template_types = ['provisioning_templates', 'report_templates', 'ptables'] |
| 192 |
if 'job_templates' in module.foremanapi.resources: |
| 193 |
template_types.append('job_templates') |
| 194 |
|
| 195 |
for template_type in template_types: |
| 196 |
template_report += [(resource['name'], resource['id']) for resource in module.list_resource(template_type)] |
| 197 |
|
| 198 |
result = module.resource_action('templates', 'import', record_change=False, params=_flatten_entity(module.foreman_params, module.foreman_spec)) |
| 199 |
msg_templates = result['message'].pop('templates', []) |
| 200 |
|
| 201 |
report = {'changed': [], 'new': []} |
| 202 |
templates = {} |
| 203 |
|
| 204 |
for template in msg_templates: |
| 205 |
if template['changed']: |
| 206 |
report['changed'].append(template['name']) |
| 207 |
module.set_changed() |
| 208 |
elif template['imported']: |
| 209 |
if (template['name'], template['id']) not in template_report: |
| 210 |
report['new'].append(template['name']) |
| 211 |
module.set_changed() |
| 212 |
templates[template.pop('name')] = template |
| 213 |
|
| 214 |
module.exit_json(templates=templates, message=result['message'], report=report) |
| 215 |
|
| 216 |
|
| 217 |
if __name__ == '__main__': |
| 218 |
main() |