Debian Codegraph @

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