Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_gir_profile_management.py

raw
1 #!/usr/bin/python
2 #
3 # This file is part of Ansible
4 #
5 # Ansible 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 # Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
17 #
18 from __future__ import absolute_import, division, print_function
19
20
21 __metaclass__ = type
22
23
24 DOCUMENTATION = """
25 module: nxos_gir_profile_management
26 extends_documentation_fragment:
27 - cisco.nxos.nxos
28 short_description: Create a maintenance-mode or normal-mode profile for GIR.
29 description:
30 - Manage a maintenance-mode or normal-mode profile with configuration commands that
31 can be applied during graceful removal or graceful insertion.
32 version_added: 1.0.0
33 author:
34 - Gabriele Gerbino (@GGabriele)
35 notes:
36 - Tested against NXOSv 7.3.(0)D1(1) on VIRL
37 - Unsupported for Cisco MDS
38 - C(state=absent) removes the whole profile.
39 options:
40 commands:
41 description:
42 - List of commands to be included into the profile.
43 type: list
44 elements: str
45 mode:
46 description:
47 - Configure the profile as Maintenance or Normal mode.
48 required: true
49 choices:
50 - maintenance
51 - normal
52 type: str
53 state:
54 description:
55 - Specify desired state of the resource.
56 default: present
57 choices:
58 - present
59 - absent
60 type: str
61 """
62
63 EXAMPLES = """
64 # Create a maintenance-mode profile
65 - cisco.nxos.nxos_gir_profile_management:
66 mode: maintenance
67 commands:
68 - router eigrp 11
69 - isolate
70
71 # Remove the maintenance-mode profile
72 - cisco.nxos.nxos_gir_profile_management:
73 mode: maintenance
74 state: absent
75 """
76
77 RETURN = """
78 proposed:
79 description: list of commands passed into module.
80 returned: verbose mode
81 type: list
82 sample: ["router eigrp 11", "isolate"]
83 existing:
84 description: list of existing profile commands.
85 returned: verbose mode
86 type: list
87 sample: ["router bgp 65535","isolate","router eigrp 10","isolate",
88 "diagnostic bootup level complete"]
89 end_state:
90 description: list of profile entries after module execution.
91 returned: verbose mode
92 type: list
93 sample: ["router bgp 65535","isolate","router eigrp 10","isolate",
94 "diagnostic bootup level complete","router eigrp 11", "isolate"]
95 updates:
96 description: commands sent to the device
97 returned: always
98 type: list
99 sample: ["configure maintenance profile maintenance-mode",
100 "router eigrp 11","isolate"]
101 changed:
102 description: check to see if a change was made on the device
103 returned: always
104 type: bool
105 sample: true
106 """
107
108
109 from ansible.module_utils.basic import AnsibleModule
110 from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import (
111 CustomNetworkConfig,
112 )
113
114 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import (
115 get_config,
116 load_config,
117 )
118
119
120 def get_existing(module):
121 existing = []
122 netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
123
124 if module.params["mode"] == "maintenance":
125 parents = ["configure maintenance profile maintenance-mode"]
126 else:
127 parents = ["configure maintenance profile normal-mode"]
128
129 config = netcfg.get_section(parents)
130 if config:
131 existing = config.splitlines()
132 existing = [cmd.strip() for cmd in existing]
133 existing.pop(0)
134
135 return existing
136
137
138 def state_present(module, existing, commands):
139 cmds = list()
140 if existing == commands:
141 # Idempotent case
142 return cmds
143 cmds.extend(commands)
144 if module.params["mode"] == "maintenance":
145 cmds.insert(0, "configure maintenance profile maintenance-mode")
146 else:
147 cmds.insert(0, "configure maintenance profile normal-mode")
148
149 return cmds
150
151
152 def state_absent(module, existing, commands):
153 if module.params["mode"] == "maintenance":
154 cmds = ["no configure maintenance profile maintenance-mode"]
155 else:
156 cmds = ["no configure maintenance profile normal-mode"]
157 return cmds
158
159
160 def invoke(name, *args, **kwargs):
161 func = globals().get(name)
162 if func:
163 return func(*args, **kwargs)
164
165
166 def main():
167 argument_spec = dict(
168 commands=dict(required=False, type="list", elements="str"),
169 mode=dict(required=True, choices=["maintenance", "normal"]),
170 state=dict(choices=["absent", "present"], default="present"),
171 )
172
173 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
174
175 warnings = list()
176
177 state = module.params["state"]
178 commands = module.params["commands"] or []
179
180 if state == "absent" and commands:
181 module.fail_json(msg="when state is absent, no command can be used.")
182
183 existing = invoke("get_existing", module)
184 end_state = existing
185 changed = False
186
187 result = {}
188 cmds = []
189 if state == "present" or (state == "absent" and existing):
190 cmds = invoke("state_%s" % state, module, existing, commands)
191
192 if module.check_mode:
193 module.exit_json(changed=True, commands=cmds)
194 else:
195 if cmds:
196 load_config(module, cmds)
197 changed = True
198 end_state = invoke("get_existing", module)
199
200 result["changed"] = changed
201 if module._verbosity > 0:
202 end_state = invoke("get_existing", module)
203 result["end_state"] = end_state
204 result["existing"] = existing
205 result["proposed"] = commands
206 result["updates"] = cmds
207
208 result["warnings"] = warnings
209
210 module.exit_json(**result)
211
212
213 if __name__ == "__main__":
214 main()