Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2017 Matthias Dellweg & Bernhard Hopfenmüller (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: partition_table
25 version_added: 1.0.0
26 short_description: Manage Partition Table Templates
27 description:
28 - Manage Partition Table Templates
29 author:
30 - "Bernhard Hopfenmueller (@Fobhep) ATIX AG"
31 - "Matthias Dellweg (@mdellweg) ATIX AG"
32 options:
33 file_name:
34 description:
35 - The path of a template file, that shall be imported.
36 - Either this or I(layout) is required as a source for the Partition Template "content".
37 required: false
38 type: path
39 layout:
40 description:
41 - The content of the Partitioning Table Template
42 - Either this or I(file_name) is required as a source for the Partition Template "content".
43 required: false
44 type: str
45 locked:
46 description:
47 - Determines whether the template shall be locked
48 required: false
49 type: bool
50 name:
51 description:
52 - The name of the Partition Table.
53 - If omited, will be determined from the C(name) header of the template or the filename (in that order).
54 - The special value "*" can be used to perform bulk actions (modify, delete) on all existing Partition Tables.
55 required: false
56 type: str
57 updated_name:
58 description: New name of the template. When this parameter is set, the module will not be idempotent.
59 required: false
60 type: str
61 os_family:
62 description:
63 - The OS family the template shall be assigned with.
64 extends_documentation_fragment:
65 - theforeman.foreman.foreman
66 - theforeman.foreman.foreman.entity_state_with_defaults
67 - theforeman.foreman.foreman.taxonomy
68 - theforeman.foreman.foreman.os_family
69 '''
70
71 EXAMPLES = '''
72
73 # Keep in mind, that in this case, the inline parameters will be overwritten
74 - name: "Create a Partition Table inline"
75 theforeman.foreman.partition_table:
76 username: "admin"
77 password: "changeme"
78 server_url: "https://foreman.example.com"
79 name: A New Partition Template
80 state: present
81 layout: |
82 <%#
83 name: A Partition Template
84 %>
85 zerombr
86 clearpart --all --initlabel
87 autopart
88 locations:
89 - Gallifrey
90 organizations:
91 - TARDIS INC
92
93 - name: "Create a Partition Template from a file"
94 theforeman.foreman.partition_table:
95 username: "admin"
96 password: "changeme"
97 server_url: "https://foreman.example.com"
98 file_name: timeywimey_template.erb
99 state: present
100 locations:
101 - Gallifrey
102 organizations:
103 - TARDIS INC
104
105 - name: "Delete a Partition Template"
106 theforeman.foreman.partition_table:
107 username: "admin"
108 password: "changeme"
109 server_url: "https://foreman.example.com"
110 name: timeywimey
111 layout: |
112 <%#
113 dummy:
114 %>
115 state: absent
116
117 - name: "Create a Partition Template from a file and modify with parameter(s)"
118 theforeman.foreman.partition_table:
119 username: "admin"
120 password: "changeme"
121 server_url: "https://foreman.example.com"
122 file_name: timeywimey_template.erb
123 name: Wibbly Wobbly Template
124 state: present
125 locations:
126 - Gallifrey
127 organizations:
128 - TARDIS INC
129
130 # Providing a name in this case wouldn't be very sensible.
131 # Alternatively make use of with_filetree to parse recursively with filter.
132 - name: "Parsing a directory of partition templates"
133 theforeman.foreman.partition_table:
134 username: "admin"
135 password: "changeme"
136 server_url: "https://foreman.example.com"
137 file_name: "{{ item }}"
138 state: present
139 locations:
140 - SKARO
141 organizations:
142 - DALEK INC
143 with_fileglob:
144 - "./arsenal_templates/*.erb"
145
146 # If the templates are stored locally and the ansible module is executed on a remote host
147 - name: Ensure latest version of all Ptable Community Templates
148 theforeman.foreman.partition_table:
149 server_url: "https://foreman.example.com"
150 username: "admin"
151 password: "changeme"
152 state: present
153 layout: '{{ lookup("file", item.src) }}'
154 with_filetree: '/path/to/partition/tables'
155 when: item.state == 'file'
156
157
158 # with name set to "*" bulk actions can be performed
159 - name: "Delete *ALL* partition tables"
160 theforeman.foreman.partition_table:
161 username: "admin"
162 password: "changeme"
163 server_url: "https://foreman.example.com"
164 name: "*"
165 state: absent
166
167 - name: "Assign all partition tables to the same organization(s)"
168 theforeman.foreman.partition_table:
169 username: "admin"
170 password: "changeme"
171 server_url: "https://foreman.example.com"
172 name: "*"
173 state: present
174 organizations:
175 - DALEK INC
176 - sky.net
177 - Doc Brown's garage
178 '''
179
180 RETURN = '''
181 entity:
182 description: Final state of the affected entities grouped by their type.
183 returned: success
184 type: dict
185 contains:
186 ptables:
187 description: List of partition tables.
188 type: list
189 elements: dict
190 '''
191
192
193 import os
194
195 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import (
196 ForemanTaxonomicEntityAnsibleModule,
197 parse_template,
198 parse_template_from_file,
199 OS_LIST,
200 )
201
202
203 class ForemanPtableModule(ForemanTaxonomicEntityAnsibleModule):
204 pass
205
206
207 def main():
208 module = ForemanPtableModule(
209 argument_spec=dict(
210 file_name=dict(type='path'),
211 state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']),
212 updated_name=dict(),
213 ),
214 foreman_spec=dict(
215 layout=dict(),
216 locked=dict(type='bool'),
217 name=dict(),
218 os_family=dict(choices=OS_LIST),
219 ),
220 mutually_exclusive=[
221 ['file_name', 'layout'],
222 ],
223 required_one_of=[
224 ['name', 'file_name', 'layout'],
225 ],
226 )
227
228 # We do not want a layout text for bulk operations
229 if module.foreman_params.get('name') == '*':
230 if module.foreman_params.get('file_name') or module.foreman_params.get('layout') or module.foreman_params.get('updated_name'):
231 module.fail_json(
232 msg="Neither file_name nor layout nor updated_name allowed if 'name: *'!")
233
234 entity = None
235 file_name = module.foreman_params.pop('file_name', None)
236
237 if file_name or 'layout' in module.foreman_params:
238 if file_name:
239 parsed_dict = parse_template_from_file(file_name, module)
240 else:
241 parsed_dict = parse_template(module.foreman_params['layout'], module)
242 parsed_dict['layout'] = parsed_dict.pop('template')
243 if 'oses' in parsed_dict:
244 parsed_dict['os_family'] = parsed_dict.pop('oses')
245 # sanitize name from template data
246 # The following condition can actually be hit, when someone is trying to import a
247 # template with the name set to '*'.
248 # Besides not being sensible, this would go horribly wrong in this module.
249 if parsed_dict.get('name') == '*':
250 module.fail_json(msg="Cannot use '*' as a partition table name!")
251 # module params are priorized
252 parsed_dict.update(module.foreman_params)
253 module.foreman_params = parsed_dict
254
255 # make sure, we have a name
256 if 'name' not in module.foreman_params:
257 if file_name:
258 module.foreman_params['name'] = os.path.splitext(
259 os.path.basename(file_name))[0]
260 else:
261 module.fail_json(
262 msg='No name specified and no filename to infer it.')
263
264 affects_multiple = module.foreman_params['name'] == '*'
265 # sanitize user input, filter unuseful configuration combinations with 'name: *'
266 if affects_multiple:
267 if module.state == 'present_with_defaults':
268 module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together")
269 if module.desired_absent:
270 further_params = set(module.foreman_params.keys()) - {'name', 'entity'}
271 if further_params:
272 module.fail_json(msg='When deleting all partition tables, there is no need to specify further parameters: %s ' % further_params)
273
274 with module.api_connection():
275 if affects_multiple:
276 module.set_entity('entity', None) # prevent lookup
277 entities = module.list_resource('ptables')
278 if not entities:
279 # Nothing to do; shortcut to exit
280 module.exit_json()
281 if not module.desired_absent: # not 'thin'
282 entities = [module.show_resource('ptables', entity['id']) for entity in entities]
283 module.auto_lookup_entities()
284 module.foreman_params.pop('name')
285 for entity in entities:
286 module.ensure_entity('ptables', module.foreman_params, entity)
287 else:
288 # The name could have been determined to late, so copy it again
289 module.foreman_params['entity'] = module.foreman_params['name']
290
291 module.run()
292
293
294 if __name__ == '__main__':
295 main()