Debian Codegraph @

ansible_collections/cisco/ios/plugins/modules/ios_hostname.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2022 Red Hat
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 """
8 The module file for ios_hostname
9 """
10
11 from __future__ import absolute_import, division, print_function
12
13
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 module: ios_hostname
18 short_description: Resource module to configure hostname.
19 description:
20 - This module provides declarative management of hostname on Cisco IOS devices.
21 version_added: 2.7.0
22 author:
23 - Sagar Paul (@KB-perByte)
24 notes:
25 - Tested against Cisco IOSXE Version 17.3 on CML.
26 - This module works with connection C(network_cli).
27 options:
28 config:
29 description: A dictionary of hostname options
30 type: dict
31 suboptions:
32 hostname:
33 description: set hostname for IOS
34 type: str
35 running_config:
36 description:
37 - This option is used only with state I(parsed).
38 - The value of this option should be the output received from the IOS device
39 by executing the command B(show running-config | section ^hostname).
40 - The state I(parsed) reads the configuration from C(running_config) option and
41 transforms it into Ansible structured data as per the resource module's argspec
42 and the value is then returned in the I(parsed) key within the result.
43 type: str
44 state:
45 choices:
46 - merged
47 - replaced
48 - overridden
49 - deleted
50 - rendered
51 - gathered
52 - parsed
53 default: merged
54 description:
55 - The state the configuration should be left in
56 - The states I(rendered), I(gathered) and I(parsed) does not perform any change
57 on the device.
58 - The state I(rendered) will transform the configuration in C(config) option to
59 platform specific CLI commands which will be returned in the I(rendered) key
60 within the result. For state I(rendered) active connection to remote host is
61 not required.
62 - The states I(merged), I(replaced) and I(overridden) have identical
63 behaviour for this module.
64 - The state I(gathered) will fetch the running configuration from device and transform
65 it into structured data in the format as per the resource module argspec and
66 the value is returned in the I(gathered) key within the result.
67 - The state I(parsed) reads the configuration from C(running_config) option and
68 transforms it into JSON format as per the resource module parameters and the
69 value is returned in the I(parsed) key within the result. The value of C(running_config)
70 option should be the same format as the output of command
71 I(show running-config | section ^hostname) executed on device. For state I(parsed) active
72 connection to remote host is not required.
73 type: str
74 """
75
76 EXAMPLES = """
77 # Using state: merged
78
79 # Before state:
80 # -------------
81
82 # router-ios#show running-config | section ^hostname
83 # hostname Router
84
85 # Merged play:
86 # ------------
87
88 - name: Apply the provided configuration
89 cisco.ios.ios_hostname:
90 config:
91 hostname: Router1
92 state: merged
93
94 # Commands Fired:
95 # ---------------
96
97 # "commands": [
98 # "hostname Router1",
99 # ],
100
101 # After state:
102 # ------------
103
104 # router-ios#show running-config | section ^hostname
105 # hostname Router1
106
107 # Using state: deleted
108
109 # Before state:
110 # -------------
111
112 # router-ios#show running-config | section ^hostname
113 # hostname RouterTest
114
115 # Deleted play:
116 # -------------
117
118 - name: Remove all existing configuration
119 cisco.ios.ios_hostname:
120 state: deleted
121
122 # Commands Fired:
123 # ---------------
124
125 # "commands": [
126 # "no hostname RouterTest",
127 # ],
128
129 # After state:
130 # ------------
131
132 # router-ios#show running-config | section ^hostname
133 # hostname Router
134
135 # Using state: overridden
136
137 # Before state:
138 # -------------
139
140 # router-ios#show running-config | section ^hostname
141 # hostname Router
142
143 # Overridden play:
144 # ----------------
145
146 - name: Override commands with provided configuration
147 cisco.ios.ios_hostname:
148 config:
149 hostname: RouterTest
150 state: overridden
151
152 # Commands Fired:
153 # ---------------
154 # "commands": [
155 # "hostname RouterTest",
156 # ],
157
158 # After state:
159 # ------------
160
161 # router-ios#show running-config | section ^hostname
162 # hostname RouterTest
163
164 # Using state: replaced
165
166 # Before state:
167 # -------------
168
169 # router-ios#show running-config | section ^hostname
170 # hostname RouterTest
171
172 # Replaced play:
173 # --------------
174
175 - name: Replace commands with provided configuration
176 cisco.ios.ios_hostname:
177 config:
178 hostname: RouterTest
179 state: replaced
180
181 # Commands Fired:
182 # ---------------
183
184 # "commands": [],
185
186 # After state:
187 # ------------
188
189 # router-ios#show running-config | section ^hostname
190 # hostname RouterTest
191
192 # Using state: gathered
193
194 # Before state:
195 # -------------
196
197 # router-ios#show running-config | section ^hostname
198 # hostname RouterTest
199
200 # Gathered play:
201 # --------------
202
203 - name: Gather listed hostname config
204 cisco.ios.ios_hostname:
205 state: gathered
206
207 # Module Execution Result:
208 # ------------------------
209
210 # "gathered": {
211 # "hostname": "RouterTest"
212 # },
213
214 # Using state: rendered
215
216 # Rendered play:
217 # --------------
218
219 - name: Render the commands for provided configuration
220 cisco.ios.ios_hostname:
221 config:
222 hostname: RouterTest
223 state: rendered
224
225 # Module Execution Result:
226 # ------------------------
227
228 # "rendered": [
229 # "hostname RouterTest",
230 # ]
231
232 # Using state: parsed
233
234 # File: parsed.cfg
235 # ----------------
236
237 # hostname RouterTest
238
239 # Parsed play:
240 # ------------
241
242 - name: Parse the provided configuration with the existing running configuration
243 cisco.ios.ios_hostname:
244 running_config: "{{ lookup('file', 'parsed.cfg') }}"
245 state: parsed
246
247 # Module Execution Result:
248 # ------------------------
249
250 # "parsed": {
251 # "hostname": "RouterTest"
252 # }
253 """
254
255 RETURN = """
256 before:
257 description: The configuration prior to the module execution.
258 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
259 type: dict
260 sample: >
261 This output will always be in the same format as the
262 module argspec.
263 after:
264 description: The resulting configuration after module execution.
265 returned: when changed
266 type: dict
267 sample: >
268 This output will always be in the same format as the
269 module argspec.
270 commands:
271 description: The set of commands pushed to the remote device.
272 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
273 type: list
274 sample:
275 - hostname Router1
276 rendered:
277 description: The provided configuration in the task rendered in device-native format (offline).
278 returned: when I(state) is C(rendered)
279 type: list
280 sample:
281 - hostname Switch1
282 gathered:
283 description: Facts about the network resource gathered from the remote device as structured data.
284 returned: when I(state) is C(gathered)
285 type: list
286 sample: >
287 This output will always be in the same format as the
288 module argspec.
289 parsed:
290 description: The device native config provided in I(running_config) option parsed into structured data as per module argspec.
291 returned: when I(state) is C(parsed)
292 type: list
293 sample: >
294 This output will always be in the same format as the
295 module argspec.
296 """
297
298 from ansible.module_utils.basic import AnsibleModule
299
300 from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.hostname.hostname import (
301 HostnameArgs,
302 )
303 from ansible_collections.cisco.ios.plugins.module_utils.network.ios.config.hostname.hostname import (
304 Hostname,
305 )
306
307
308 def main():
309 """
310 Main entry point for module execution
311
312 :returns: the result form module invocation
313 """
314 module = AnsibleModule(
315 argument_spec=HostnameArgs.argument_spec,
316 mutually_exclusive=[["config", "running_config"]],
317 required_if=[
318 ["state", "merged", ["config"]],
319 ["state", "replaced", ["config"]],
320 ["state", "overridden", ["config"]],
321 ["state", "rendered", ["config"]],
322 ["state", "parsed", ["running_config"]],
323 ],
324 supports_check_mode=True,
325 )
326
327 result = Hostname(module).execute_module()
328 module.exit_json(**result)
329
330
331 if __name__ == "__main__":
332 main()