Debian Codegraph @

ansible_collections/theforeman/foreman/plugins/modules/os_default_template.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2017 Matthias M Dellweg (ATIX AG)
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import absolute_import, division, print_function
19 __metaclass__ = type
20
21
22 DOCUMENTATION = '''
23 ---
24 module: os_default_template
25 version_added: 1.0.0
26 short_description: Manage Default Template Associations To Operating Systems
27 description:
28 - Manage OSDefaultTemplate Entities
29 author:
30 - "Matthias M Dellweg (@mdellweg) ATIX AG"
31 options:
32 operatingsystem:
33 required: true
34 template_kind:
35 description:
36 - name of the template kind
37 required: true
38 type: str
39 choices:
40 - Bootdisk
41 - cloud-init
42 - finish
43 - host_init_config
44 - iPXE
45 - job_template
46 - kexec
47 - POAP
48 - provision
49 - PXEGrub
50 - PXEGrub2
51 - PXELinux
52 - registration
53 - script
54 - user_data
55 - ZTP
56 provisioning_template:
57 description:
58 - name of provisioning template
59 required: false
60 type: str
61 extends_documentation_fragment:
62 - theforeman.foreman.foreman
63 - theforeman.foreman.foreman.entity_state_with_defaults
64 - theforeman.foreman.foreman.operatingsystem
65 '''
66
67 EXAMPLES = '''
68 - name: "Create an Association"
69 theforeman.foreman.os_default_template:
70 username: "admin"
71 password: "changeme"
72 server_url: "https://foreman.example.com"
73 operatingsystem: "CoolOS"
74 template_kind: "finish"
75 provisioning_template: "CoolOS finish"
76 state: present
77
78 - name: "Delete an Association"
79 theforeman.foreman.os_default_template:
80 username: "admin"
81 password: "changeme"
82 server_url: "https://foreman.example.com"
83 operatingsystem: "CoolOS"
84 template_kind: "finish"
85 state: absent
86 '''
87
88 RETURN = '''
89 entity:
90 description: Final state of the affected entities grouped by their type.
91 returned: success
92 type: dict
93 contains:
94 os_default_templates:
95 description: List of operatingsystem default templates.
96 type: list
97 elements: dict
98 '''
99
100
101 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanEntityAnsibleModule, TEMPLATE_KIND_LIST
102
103
104 class ForemanOsDefaultTemplateModule(ForemanEntityAnsibleModule):
105 pass
106
107
108 def main():
109 module = ForemanOsDefaultTemplateModule(
110 argument_spec=dict(
111 state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']),
112 ),
113 foreman_spec=dict(
114 operatingsystem=dict(required=True, type='entity'),
115 template_kind=dict(required=True, choices=TEMPLATE_KIND_LIST, type='entity'),
116 provisioning_template=dict(type='entity', thin=False),
117 ),
118 required_if=[
119 ['state', 'present', ['provisioning_template']],
120 ['state', 'present_with_defaults', ['provisioning_template']],
121 ],
122 entity_opts={'scope': ['operatingsystem']},
123 )
124
125 if 'provisioning_template' in module.foreman_params and module.desired_absent:
126 module.fail_json(msg='Provisioning template must not be specified for deletion.')
127
128 with module.api_connection():
129 template_kind_id = module.lookup_entity('template_kind')['id']
130 if not module.desired_absent:
131 if module.lookup_entity('provisioning_template')['template_kind_id'] != template_kind_id:
132 module.fail_json(msg='Provisioning template kind mismatching.')
133
134 scope = module.scope_for('operatingsystem')
135 # Default templates do not support a scoped search
136 # see: https://projects.theforeman.org/issues/27722
137 entities = module.list_resource('os_default_templates', params=scope)
138 entity = next((item for item in entities if item['template_kind_id'] == template_kind_id), None)
139 module.set_entity('entity', entity)
140
141 module.run()
142
143
144 if __name__ == '__main__':
145 main()