| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = """ |
| 12 |
--- |
| 13 |
module: sonic_config |
| 14 |
version_added: 1.0.0 |
| 15 |
notes: |
| 16 |
- Tested against Enterprise SONiC Distribution by Dell Technologies. |
| 17 |
- Supports C(check_mode). |
| 18 |
author: Abirami N (@abirami-n) |
| 19 |
short_description: Manages configuration sections on devices running Enterprise SONiC |
| 20 |
description: |
| 21 |
- Manages configuration sections of Enterprise SONiC Distribution |
| 22 |
by Dell Technologies. SONiC configurations use a simple block indent |
| 23 |
file syntax for segmenting configuration into sections. This module |
| 24 |
provides an implementation for working with SONiC configuration |
| 25 |
sections in a deterministic way. |
| 26 |
options: |
| 27 |
lines: |
| 28 |
description: |
| 29 |
- The ordered set of commands that should be configured in the |
| 30 |
section. The commands must be the exact same commands as found |
| 31 |
in the device running-configuration. Be sure to note the configuration |
| 32 |
command syntax as some commands are automatically modified by the |
| 33 |
device configuration parser. This argument is mutually exclusive |
| 34 |
with I(src). |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
aliases: ['commands'] |
| 38 |
parents: |
| 39 |
description: |
| 40 |
- The ordered set of parents that uniquely identify the section or hierarchy |
| 41 |
the commands should be checked against. If the parents argument |
| 42 |
is omitted, the commands are checked against the set of top |
| 43 |
level or global commands. |
| 44 |
type: list |
| 45 |
elements: str |
| 46 |
src: |
| 47 |
description: |
| 48 |
- Specifies the source path to the file that contains the configuration |
| 49 |
or configuration template to load. The path to the source file can |
| 50 |
either be the full path on the Ansible control host, or a relative |
| 51 |
path from the playbook or role root directory. This argument is |
| 52 |
mutually exclusive with I(lines). |
| 53 |
type: path |
| 54 |
before: |
| 55 |
description: |
| 56 |
- The ordered set of commands to push on to the command stack if |
| 57 |
a change needs to be made. This allows the playbook designer |
| 58 |
the opportunity to perform configuration commands prior to pushing |
| 59 |
any changes without affecting how the set of commands are matched |
| 60 |
against the system. |
| 61 |
type: list |
| 62 |
elements: str |
| 63 |
after: |
| 64 |
description: |
| 65 |
- The ordered set of commands to append to the end of the command |
| 66 |
stack if a change needs to be made. Just like with I(before), this |
| 67 |
allows the playbook designer to append a set of commands to be |
| 68 |
executed after the command set. |
| 69 |
type: list |
| 70 |
elements: str |
| 71 |
save: |
| 72 |
description: |
| 73 |
- The C(save) argument instructs the module to save the running- |
| 74 |
configuration to the startup-configuration at the conclusion of |
| 75 |
the module running. If check mode is specified, this argument |
| 76 |
is ignored. |
| 77 |
type: bool |
| 78 |
default: 'false' |
| 79 |
match: |
| 80 |
description: |
| 81 |
- Instructs the module on the way to perform the matching of |
| 82 |
the set of commands against the current device configuration. |
| 83 |
If match is set to I(line), commands are matched line by line. |
| 84 |
If match is set to I(strict), command lines are matched with respect |
| 85 |
to position. If match is set to I(exact), command lines |
| 86 |
must be an equal match. If match is set to I(none), the |
| 87 |
module does not attempt to compare the source configuration with |
| 88 |
the running-configuration on the remote device. |
| 89 |
type: str |
| 90 |
default: line |
| 91 |
choices: ['line', 'strict', 'exact', 'none'] |
| 92 |
replace: |
| 93 |
description: |
| 94 |
- Instructs the module how to perform a configuration |
| 95 |
on the device. If the replace argument is set to I(line), then |
| 96 |
the modified lines are pushed to the device in configuration |
| 97 |
mode. If the replace argument is set to I(block), then the entire |
| 98 |
command block is pushed to the device in configuration mode if any |
| 99 |
line is not correct. |
| 100 |
type: str |
| 101 |
default: line |
| 102 |
choices: ['line', 'block'] |
| 103 |
update: |
| 104 |
description: |
| 105 |
- The I(update) argument controls how the configuration statements |
| 106 |
are processed on the remote device. Valid choices for the I(update) |
| 107 |
argument are I(merge) and I(check). When you set this argument to |
| 108 |
I(merge), the configuration changes merge with the current |
| 109 |
device running-configuration. When you set this argument to I(check), |
| 110 |
the configuration updates are determined but not configured |
| 111 |
on the remote device. |
| 112 |
type: str |
| 113 |
default: merge |
| 114 |
choices: ['merge', 'check'] |
| 115 |
config: |
| 116 |
description: |
| 117 |
- The module, by default, connects to the remote device and |
| 118 |
retrieves the current running-configuration to use as a base for |
| 119 |
comparing against the contents of source. There are times when |
| 120 |
it is not desirable to have the task get the current |
| 121 |
running-configuration for every task in a playbook. The I(config) |
| 122 |
argument allows the implementer to pass in the configuration to |
| 123 |
use as the base configuration for comparison. |
| 124 |
type: str |
| 125 |
backup: |
| 126 |
description: |
| 127 |
- This argument causes the module to create a full backup of |
| 128 |
the current C(running-configuration) from the remote device before any |
| 129 |
changes are made. If the C(backup_options) value is not given, |
| 130 |
the backup file is written to the C(backup) folder in the playbook |
| 131 |
root directory. If the directory does not exist, it is created. |
| 132 |
type: bool |
| 133 |
default: 'no' |
| 134 |
backup_options: |
| 135 |
description: |
| 136 |
- This is a dictionary object containing configurable options related to backup file path. |
| 137 |
The value of this option is read only when C(backup) is set to I(yes), if C(backup) is set |
| 138 |
to I(no) this option is ignored. |
| 139 |
suboptions: |
| 140 |
filename: |
| 141 |
description: |
| 142 |
- The filename to be used to store the backup configuration. If the filename |
| 143 |
is not given, it is generated based on the hostname, current time, and date |
| 144 |
in the format defined by <hostname>_config.<current-date>@<current-time>. |
| 145 |
type: str |
| 146 |
dir_path: |
| 147 |
description: |
| 148 |
- This option provides the path ending with directory name in which the backup |
| 149 |
configuration file is stored. If the directory does not exist it is first |
| 150 |
created, and the filename is either the value of C(filename) or default filename |
| 151 |
as described in C(filename) options description. If the path value is not given, |
| 152 |
an I(backup) directory is created in the current working directory |
| 153 |
and backup configuration is copied in C(filename) within the I(backup) directory. |
| 154 |
type: path |
| 155 |
type: dict |
| 156 |
""" |
| 157 |
|
| 158 |
EXAMPLES = """ |
| 159 |
- dellemc.enterprise_sonic.sonic_config: |
| 160 |
lines: ['username {{ user_name }} password {{ user_password }} role {{ user_role }}'] |
| 161 |
|
| 162 |
- dellemc.enterprise_sonic.sonic_config: |
| 163 |
lines: |
| 164 |
- description 'SONiC' |
| 165 |
parents: ['interface Eth1/10'] |
| 166 |
|
| 167 |
- dellemc.enterprise_sonic.sonic_config: |
| 168 |
lines: |
| 169 |
- seq 2 permit udp any any |
| 170 |
- seq 3 deny icmp any any |
| 171 |
parents: ['ip access-list test'] |
| 172 |
before: ['no ip access-list test'] |
| 173 |
""" |
| 174 |
|
| 175 |
RETURN = """ |
| 176 |
updates: |
| 177 |
description: The set of commands that is pushed to the remote device. |
| 178 |
returned: always |
| 179 |
type: list |
| 180 |
sample: ['username foo password foo role admin', 'router bgp 1', 'router-id 1.1.1.1'] |
| 181 |
commands: |
| 182 |
description: The set of commands that is pushed to the remote device. |
| 183 |
returned: always |
| 184 |
type: list |
| 185 |
sample: ['username foo password foo role admin', 'router bgp 1', 'router-id 1.1.1.1'] |
| 186 |
saved: |
| 187 |
description: Returns whether the configuration is saved to the startup |
| 188 |
configuration or not. |
| 189 |
returned: When not check_mode. |
| 190 |
type: bool |
| 191 |
sample: True |
| 192 |
""" |
| 193 |
|
| 194 |
from ansible.module_utils.connection import ConnectionError |
| 195 |
|
| 196 |
from ansible.module_utils._text import to_text |
| 197 |
from ansible.module_utils.basic import AnsibleModule |
| 198 |
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.sonic import get_config, get_sublevel_config |
| 199 |
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.sonic import edit_config, run_commands |
| 200 |
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.utils.utils import command_list_str_to_dict |
| 201 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import NetworkConfig, dumps |
| 202 |
|
| 203 |
|
| 204 |
def get_candidate(module): |
| 205 |
candidate = NetworkConfig(indent=1) |
| 206 |
if module.params['src']: |
| 207 |
candidate.load(module.params['src']) |
| 208 |
elif module.params['lines']: |
| 209 |
parents = module.params['parents'] or list() |
| 210 |
commands = module.params['lines'][0] |
| 211 |
if (isinstance(commands, dict)) and (isinstance((commands['command']), list)): |
| 212 |
candidate.add(commands['command'], parents=parents) |
| 213 |
elif (isinstance(commands, dict)) and (isinstance((commands['command']), str)): |
| 214 |
candidate.add([commands['command']], parents=parents) |
| 215 |
else: |
| 216 |
candidate.add(module.params['lines'], parents=parents) |
| 217 |
return candidate |
| 218 |
|
| 219 |
|
| 220 |
def get_running_config(module): |
| 221 |
contents = module.params['config'] |
| 222 |
if not contents: |
| 223 |
contents = get_config(module) |
| 224 |
return contents |
| 225 |
|
| 226 |
|
| 227 |
def main(): |
| 228 |
|
| 229 |
backup_spec = dict( |
| 230 |
filename=dict(), |
| 231 |
dir_path=dict(type='path') |
| 232 |
) |
| 233 |
|
| 234 |
argument_spec = dict( |
| 235 |
lines=dict(aliases=['commands'], type='list', elements="str"), |
| 236 |
parents=dict(type='list', elements="str"), |
| 237 |
|
| 238 |
src=dict(type='path'), |
| 239 |
|
| 240 |
before=dict(type='list', elements="str"), |
| 241 |
after=dict(type='list', elements="str"), |
| 242 |
save=dict(type='bool', default=False), |
| 243 |
match=dict(default='line', |
| 244 |
choices=['line', 'strict', 'exact', 'none']), |
| 245 |
replace=dict(default='line', choices=['line', 'block']), |
| 246 |
|
| 247 |
update=dict(choices=['merge', 'check'], default='merge'), |
| 248 |
config=dict(), |
| 249 |
backup=dict(type='bool', default=False), |
| 250 |
backup_options=dict(type='dict', options=backup_spec) |
| 251 |
|
| 252 |
) |
| 253 |
|
| 254 |
mutually_exclusive = [('lines', 'src')] |
| 255 |
|
| 256 |
module = AnsibleModule(argument_spec=argument_spec, |
| 257 |
mutually_exclusive=mutually_exclusive, |
| 258 |
supports_check_mode=True) |
| 259 |
parents = module.params['parents'] or list() |
| 260 |
match = module.params['match'] |
| 261 |
replace = module.params['replace'] |
| 262 |
|
| 263 |
warnings = list() |
| 264 |
|
| 265 |
|
| 266 |
result = dict(changed=False, saved=False, warnings=warnings) |
| 267 |
if module.params['backup']: |
| 268 |
if not module.check_mode: |
| 269 |
result['__backup__'] = get_config(module) |
| 270 |
|
| 271 |
commands = list() |
| 272 |
candidate = get_candidate(module) |
| 273 |
if any((module.params['lines'], module.params['src'])): |
| 274 |
if match != 'none': |
| 275 |
config = get_running_config(module) |
| 276 |
if parents: |
| 277 |
contents = get_sublevel_config(config, module) |
| 278 |
config = NetworkConfig(contents=contents, indent=1) |
| 279 |
else: |
| 280 |
config = NetworkConfig(contents=config, indent=1) |
| 281 |
configobjs = candidate.difference(config, match=match, replace=replace) |
| 282 |
else: |
| 283 |
|
| 284 |
configobjs = candidate.items |
| 285 |
if configobjs: |
| 286 |
commands = dumps(configobjs, 'commands') |
| 287 |
if ((isinstance((module.params['lines']), list)) and |
| 288 |
(isinstance((module.params['lines'][0]), dict)) and |
| 289 |
(set(['prompt', 'answer']).issubset(module.params['lines'][0]))): |
| 290 |
|
| 291 |
cmd = {'command': commands, |
| 292 |
'prompt': module.params['lines'][0]['prompt'], |
| 293 |
'answer': module.params['lines'][0]['answer']} |
| 294 |
commands = [cmd] |
| 295 |
else: |
| 296 |
commands = commands.split('\n') |
| 297 |
cmd_list_out = command_list_str_to_dict(module, warnings, commands) |
| 298 |
if cmd_list_out and cmd_list_out != []: |
| 299 |
commands = cmd_list_out |
| 300 |
|
| 301 |
if module.params['before']: |
| 302 |
commands[:0] = module.params['before'] |
| 303 |
|
| 304 |
if module.params['after']: |
| 305 |
commands.extend(module.params['after']) |
| 306 |
|
| 307 |
if not module.check_mode and module.params['update'] == 'merge': |
| 308 |
try: |
| 309 |
edit_config(module, commands) |
| 310 |
except ConnectionError as exc: |
| 311 |
module.fail_json(msg=to_text(exc)) |
| 312 |
|
| 313 |
result['changed'] = True |
| 314 |
result['commands'] = commands |
| 315 |
result['updates'] = commands |
| 316 |
|
| 317 |
if module.params['save']: |
| 318 |
result['changed'] = True |
| 319 |
if not module.check_mode: |
| 320 |
cmd = {r'command': 'write memory'} |
| 321 |
run_commands(module, [cmd]) |
| 322 |
result['saved'] = True |
| 323 |
|
| 324 |
module.exit_json(**result) |
| 325 |
|
| 326 |
|
| 327 |
if __name__ == '__main__': |
| 328 |
main() |