Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2017 Matthias M Dellweg (ATIX AG)
4 # (c) 2017 Bernhard Hopfenmüller (ATIX AG)
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 from __future__ import absolute_import, division, print_function
20 __metaclass__ = type
21
22
23 DOCUMENTATION = '''
24 ---
25 module: operatingsystem
26 version_added: 1.0.0
27 short_description: Manage Operating Systems
28 description:
29 - Manage Operating Systems
30 author:
31 - "Matthias M Dellweg (@mdellweg) ATIX AG"
32 - "Bernhard Hopfenmüller (@Fobhep) ATIX AG"
33 options:
34 name:
35 description:
36 - Name of the Operating System
37 required: true
38 type: str
39 updated_name:
40 description: New operating system name. When this parameter is set, the module will not be idempotent.
41 type: str
42 release_name:
43 description:
44 - Release name of the operating system (recommended for debian)
45 type: str
46 description:
47 description:
48 - Description of the Operating System
49 required: false
50 type: str
51 os_family:
52 description:
53 - Distribution family of the Operating System
54 aliases:
55 - family
56 major:
57 description:
58 - major version of the Operating System
59 required: false
60 type: str
61 minor:
62 description:
63 - minor version of the Operating System
64 required: false
65 type: str
66 architectures:
67 description:
68 - architectures, the operating system can be installed on
69 required: false
70 type: list
71 elements: str
72 media:
73 description:
74 - list of installation media
75 required: false
76 type: list
77 elements: str
78 ptables:
79 description:
80 - list of partitioning tables
81 required: false
82 type: list
83 elements: str
84 provisioning_templates:
85 description:
86 - List of provisioning templates that are associated with the operating system.
87 - Specify the full list of template names you want to associate with your OS.
88 - For example ["Kickstart default", "Kickstart default finish", "Kickstart default iPXE", "custom"].
89 - After specifying the template associations, you can set the default association in
90 - the M(theforeman.foreman.os_default_template) module.
91 required: false
92 type: list
93 elements: str
94 password_hash:
95 description:
96 - hashing algorithm for passwd
97 required: false
98 choices:
99 - MD5
100 - SHA256
101 - SHA512
102 - Base64
103 - Base64-Windows
104 type: str
105 parameters:
106 description:
107 - Operating System specific host parameters
108 extends_documentation_fragment:
109 - theforeman.foreman.foreman
110 - theforeman.foreman.foreman.entity_state_with_defaults
111 - theforeman.foreman.foreman.nested_parameters
112 - theforeman.foreman.foreman.os_family
113 '''
114
115 EXAMPLES = '''
116 - name: "Create an Operating System"
117 theforeman.foreman.operatingsystem:
118 username: "admin"
119 password: "changeme"
120 server_url: "https://foreman.example.com"
121 name: Debian
122 release_name: stretch
123 family: Debian
124 major: 9
125 parameters:
126 - name: additional-packages
127 value: python vim
128 state: present
129
130 - name: "Ensure existence of an Operating System (provide default values)"
131 theforeman.foreman.operatingsystem:
132 username: "admin"
133 password: "changeme"
134 server_url: "https://foreman.example.com"
135 name: Centos
136 family: Redhat
137 major: 7
138 password_hash: SHA256
139 state: present_with_defaults
140
141 - name: "Delete an Operating System"
142 theforeman.foreman.operatingsystem:
143 username: "admin"
144 password: "changeme"
145 server_url: "https://foreman.example.com"
146 name: Debian
147 family: Debian
148 major: 9
149 state: absent
150 '''
151
152 RETURN = '''
153 entity:
154 description: Final state of the affected entities grouped by their type.
155 returned: success
156 type: dict
157 contains:
158 operatinsystems:
159 description: List of operatinsystems.
160 type: list
161 elements: dict
162 '''
163
164
165 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import (
166 ForemanEntityAnsibleModule,
167 ParametersMixin,
168 OS_LIST,
169 )
170
171
172 class ForemanOperatingsystemModule(ParametersMixin, ForemanEntityAnsibleModule):
173 PARAMETERS_FLAT_NAME = 'os_parameters_attributes'
174
175
176 def main():
177 module = ForemanOperatingsystemModule(
178 foreman_spec=dict(
179 name=dict(required=True),
180 release_name=dict(),
181 description=dict(),
182 os_family=dict(choices=OS_LIST, flat_name='family', aliases=['family']),
183 major=dict(),
184 minor=dict(),
185 architectures=dict(type='entity_list'),
186 media=dict(type='entity_list', flat_name='medium_ids', resource_type='media'),
187 ptables=dict(type='entity_list'),
188 provisioning_templates=dict(type='entity_list'),
189 password_hash=dict(choices=['MD5', 'SHA256', 'SHA512', 'Base64', 'Base64-Windows'], no_log=False),
190 ),
191 argument_spec=dict(
192 state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']),
193 updated_name=dict(),
194 ),
195 required_if=[
196 ['state', 'present', ['name', 'major', 'os_family']],
197 ['state', 'present_with_defaults', ['name', 'major', 'os_family']],
198 ],
199 required_one_of=[
200 ['description', 'name'],
201 ['description', 'major'],
202 ],
203 )
204
205 module_params = module.foreman_params
206
207 with module.api_connection():
208
209 # Try to find the Operating System to work on
210 # name is however not unique, but description is, as well as "<name> <major>[.<minor>]"
211 entity = None
212 # If we have a description, search for it
213 if 'description' in module_params and module_params['description'] != '':
214 search_string = 'description="{0}" or title="{0}"'.format(module_params['description'])
215 entity = module.find_resource('operatingsystems', search_string, failsafe=True)
216 # If we did not yet find a unique OS, search by name & version
217 # In case of state == absent, those information might be missing, we assume that we did not find an operatingsytem to delete then
218 if entity is None and 'name' in module_params and 'major' in module_params:
219 search_string = ','.join('{0}="{1}"'.format(key, module_params.get(key, '')) for key in ('name', 'major', 'minor'))
220 entity = module.find_resource('operatingsystems', search_string, failsafe=True)
221
222 if not entity and (module.state == 'present' or module.state == 'present_with_defaults'):
223 # we actually attempt to create a new one...
224 for param_name in ['major', 'os_family']:
225 if param_name not in module_params.keys():
226 module.fail_json(msg='{0} is a required parameter to create a new operating system.'.format(param_name))
227
228 module.set_entity('entity', entity)
229 module.run()
230
231
232 if __name__ == '__main__':
233 main()