Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_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 nxos_hostname
9 """
10
11 from __future__ import absolute_import, division, print_function
12
13
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 module: nxos_hostname
18 short_description: Hostname resource module.
19 description:
20 - This module manages hostname configuration on devices running Cisco NX-OS.
21 version_added: 2.9.0
22 notes:
23 - Tested against NX-OS 9.3.6.
24 - This module works with connection C(network_cli) and C(httpapi).
25 author: Nilashish Chakraborty (@NilashishC)
26 options:
27 running_config:
28 description:
29 - This option is used only with state I(parsed).
30 - The value of this option should be the output received from the NX-OS device
31 by executing the command B(show running-config | section hostname).
32 - The state I(parsed) reads the configuration from C(running_config) option and
33 transforms it into Ansible structured data as per the resource module's argspec
34 and the value is then returned in the I(parsed) key within the result.
35 type: str
36 config:
37 description: A dictionary of hostname configuration.
38 type: dict
39 suboptions:
40 hostname:
41 description: Hostname of the device.
42 type: str
43 state:
44 description:
45 - The state the configuration should be left in.
46 - The states I(merged), I(replaced) and I(overridden) have identical
47 behaviour for this module.
48 - Refer to examples for more details.
49 type: str
50 choices:
51 - merged
52 - replaced
53 - overridden
54 - deleted
55 - parsed
56 - gathered
57 - rendered
58 default: merged
59 """
60
61 EXAMPLES = """
62 # Using merged (replaced, overridden has the same behaviour)
63
64 # Before state:
65 # -------------
66 # nxos-9k-rdo# show running-config | section ^hostname
67 # nxos-9k-rdo#
68
69 - name: Merge the provided configuration with the existing running configuration
70 cisco.nxos.nxos_hostname:
71 config:
72 hostname: NXOSv-9k
73
74 # Task output
75 # -------------
76 # before: {}
77 #
78 # commands:
79 # - hostname NXOSv-9k
80 #
81 # after:
82 # hostname: NXOSv-9k
83
84 # After state:
85 # ------------
86 # nxos-9k-rdo# show running-config | section ^hostname
87 # hostname NXOSv-9k
88 #
89
90 # Using deleted
91
92 # Before state:
93 # ------------
94 # nxos-9k-rdo# show running-config | section ^hostname
95 # hostname NXOSv-9k
96
97 - name: Delete hostname from running-config
98 cisco.nxos.nxos_hostname:
99 state: deleted
100
101 # Task output
102 # -------------
103 # before:
104 # hostname: NXOSv-9k
105 #
106 # commands:
107 # - no hostname NXOSv-9k
108 #
109 # after: {}
110
111 # Using gathered
112
113 - name: Gather hostname facts using gathered
114 cisco.nxos.nxos_hostname:
115 state: gathered
116
117 # Task output (redacted)
118 # -----------------------
119 # gathered:
120 # hostname: NXOSv-9k
121
122 # Using rendered
123
124 - name: Render platform specific configuration lines (without connecting to the device)
125 cisco.nxos.nxos_hostname:
126 config:
127 hostname: NXOSv-9k
128
129 # Task Output (redacted)
130 # -----------------------
131 # rendered:
132 # - hostname NXOSv-9k
133
134 # Using parsed
135
136 # parsed.cfg
137 # ------------
138 # hostname NXOSv-9k
139
140 - name: Parse externally provided hostname config
141 cisco.nxos.nxos_hostname:
142 running_config: "{{ lookup('file', 'parsed.cfg') }}"
143 state: parsed
144
145 # Task output (redacted)
146 # -----------------------
147 # parsed:
148 # hostname: NXOSv-9k
149 """
150
151 RETURN = """
152 before:
153 description: The configuration prior to the module execution.
154 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
155 type: dict
156 sample: >
157 This output will always be in the same format as the
158 module argspec.
159 after:
160 description: The resulting configuration after module execution.
161 returned: when changed
162 type: dict
163 sample: >
164 This output will always be in the same format as the
165 module argspec.
166 commands:
167 description: The set of commands pushed to the remote device.
168 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
169 type: list
170 sample:
171 - hostname switch01
172 rendered:
173 description: The provided configuration in the task rendered in device-native format (offline).
174 returned: when I(state) is C(rendered)
175 type: list
176 sample:
177 - hostname switch01
178 gathered:
179 description: Facts about the network resource gathered from the remote device as structured data.
180 returned: when I(state) is C(gathered)
181 type: list
182 sample: >
183 This output will always be in the same format as the
184 module argspec.
185 parsed:
186 description: The device native config provided in I(running_config) option parsed into structured data as per module argspec.
187 returned: when I(state) is C(parsed)
188 type: list
189 sample: >
190 This output will always be in the same format as the
191 module argspec.
192 """
193
194 from ansible.module_utils.basic import AnsibleModule
195
196 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.argspec.hostname.hostname import (
197 HostnameArgs,
198 )
199 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.config.hostname.hostname import (
200 Hostname,
201 )
202
203
204 def main():
205 """
206 Main entry point for module execution
207
208 :returns: the result form module invocation
209 """
210 module = AnsibleModule(
211 argument_spec=HostnameArgs.argument_spec,
212 mutually_exclusive=[["config", "running_config"]],
213 required_if=[
214 ["state", "merged", ["config"]],
215 ["state", "replaced", ["config"]],
216 ["state", "overridden", ["config"]],
217 ["state", "rendered", ["config"]],
218 ["state", "parsed", ["running_config"]],
219 ],
220 supports_check_mode=True,
221 )
222
223 result = Hostname(module).execute_module()
224 module.exit_json(**result)
225
226
227 if __name__ == "__main__":
228 main()