| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
from __future__ import absolute_import, division, print_function |
| 4 |
|
| 5 |
|
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
# (c) 2017, Ansible by Red Hat, inc |
| 9 |
# |
| 10 |
# This file is part of Ansible by Red Hat |
| 11 |
# |
| 12 |
# Ansible is free software: you can redistribute it and/or modify |
| 13 |
# it under the terms of the GNU General Public License as published by |
| 14 |
# the Free Software Foundation, either version 3 of the License, or |
| 15 |
# (at your option) any later version. |
| 16 |
# |
| 17 |
# Ansible is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
# GNU General Public License for more details. |
| 21 |
# |
| 22 |
# You should have received a copy of the GNU General Public License |
| 23 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 24 |
# |
| 25 |
|
| 26 |
|
| 27 |
DOCUMENTATION = """ |
| 28 |
module: nxos_banner |
| 29 |
author: Trishna Guha (@trishnaguha) |
| 30 |
short_description: Manage multiline banners on Cisco NXOS devices |
| 31 |
description: |
| 32 |
- This will configure both exec and motd banners on remote devices running Cisco NXOS. |
| 33 |
It allows playbooks to add or remove banner text from the active running configuration. |
| 34 |
notes: |
| 35 |
- Since responses from the device are always read with surrounding whitespaces stripped, |
| 36 |
tasks that configure banners with preceeding or trailing whitespaces will not be idempotent. |
| 37 |
- Limited Support for Cisco MDS |
| 38 |
version_added: 1.0.0 |
| 39 |
options: |
| 40 |
banner: |
| 41 |
description: |
| 42 |
- Specifies which banner that should be configured on the remote device. |
| 43 |
required: true |
| 44 |
choices: |
| 45 |
- exec |
| 46 |
- motd |
| 47 |
type: str |
| 48 |
multiline_delimiter: |
| 49 |
description: Specify the delimiting character than will be used for configuration. |
| 50 |
default: "@" |
| 51 |
type: str |
| 52 |
text: |
| 53 |
description: |
| 54 |
- The banner text that should be present in the remote device running configuration. |
| 55 |
This argument accepts a multiline string, with no empty lines. Requires I(state=present). |
| 56 |
type: str |
| 57 |
state: |
| 58 |
description: |
| 59 |
- Specifies whether or not the configuration is present in the current devices |
| 60 |
active running configuration. |
| 61 |
default: present |
| 62 |
choices: |
| 63 |
- present |
| 64 |
- absent |
| 65 |
type: str |
| 66 |
extends_documentation_fragment: |
| 67 |
- cisco.nxos.nxos |
| 68 |
""" |
| 69 |
|
| 70 |
EXAMPLES = """ |
| 71 |
- name: configure the exec banner |
| 72 |
cisco.nxos.nxos_banner: |
| 73 |
banner: exec |
| 74 |
text: | |
| 75 |
this is my exec banner |
| 76 |
that contains a multiline |
| 77 |
string |
| 78 |
state: present |
| 79 |
- name: remove the motd banner |
| 80 |
cisco.nxos.nxos_banner: |
| 81 |
banner: motd |
| 82 |
state: absent |
| 83 |
- name: Configure banner from file |
| 84 |
cisco.nxos.nxos_banner: |
| 85 |
banner: motd |
| 86 |
text: "{{ lookup('file', './config_partial/raw_banner.cfg') }}" |
| 87 |
state: present |
| 88 |
""" |
| 89 |
|
| 90 |
RETURN = """ |
| 91 |
commands: |
| 92 |
description: The list of configuration mode commands to send to the device |
| 93 |
returned: always |
| 94 |
type: list |
| 95 |
sample: |
| 96 |
- banner exec |
| 97 |
- this is my exec banner |
| 98 |
- that contains a multiline |
| 99 |
- string |
| 100 |
""" |
| 101 |
|
| 102 |
import re |
| 103 |
|
| 104 |
from ansible.module_utils._text import to_text |
| 105 |
from ansible.module_utils.basic import AnsibleModule |
| 106 |
|
| 107 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 108 |
load_config, |
| 109 |
run_commands, |
| 110 |
) |
| 111 |
|
| 112 |
|
| 113 |
def execute_show_command(module, command): |
| 114 |
format = "text" |
| 115 |
cmds = [{"command": command, "output": format}] |
| 116 |
output = run_commands(module, cmds) |
| 117 |
return output |
| 118 |
|
| 119 |
|
| 120 |
def map_obj_to_commands(want, have, module): |
| 121 |
commands = list() |
| 122 |
state = module.params["state"] |
| 123 |
platform_regex = "Nexus.*Switch" |
| 124 |
|
| 125 |
if state == "absent": |
| 126 |
if have.get("text") and not ( |
| 127 |
(have.get("text") == "User Access Verification") |
| 128 |
or re.match(platform_regex, have.get("text")) |
| 129 |
): |
| 130 |
commands.append("no banner %s" % module.params["banner"]) |
| 131 |
|
| 132 |
elif state == "present" and want.get("text") != have.get("text"): |
| 133 |
banner_cmd = "banner %s %s\n%s\n%s" % ( |
| 134 |
module.params["banner"], |
| 135 |
module.params["multiline_delimiter"], |
| 136 |
want["text"], |
| 137 |
module.params["multiline_delimiter"], |
| 138 |
) |
| 139 |
commands.append(banner_cmd) |
| 140 |
|
| 141 |
return commands |
| 142 |
|
| 143 |
|
| 144 |
def map_config_to_obj(module): |
| 145 |
command = "show banner %s" % module.params["banner"] |
| 146 |
output = execute_show_command(module, command)[0] |
| 147 |
|
| 148 |
if "Invalid command" in output: |
| 149 |
module.fail_json( |
| 150 |
msg="banner: %s may not be supported on this platform. Possible values are : exec | motd" |
| 151 |
% module.params["banner"], |
| 152 |
) |
| 153 |
|
| 154 |
if isinstance(output, dict): |
| 155 |
output = list(output.values()) |
| 156 |
if output != []: |
| 157 |
output = output[0] |
| 158 |
else: |
| 159 |
output = "" |
| 160 |
if isinstance(output, dict): |
| 161 |
output = list(output.values()) |
| 162 |
if output != []: |
| 163 |
output = output[0] |
| 164 |
else: |
| 165 |
output = "" |
| 166 |
else: |
| 167 |
output = output.rstrip() |
| 168 |
|
| 169 |
obj = {"banner": module.params["banner"], "state": "absent"} |
| 170 |
if output: |
| 171 |
obj["text"] = output |
| 172 |
obj["state"] = "present" |
| 173 |
return obj |
| 174 |
|
| 175 |
|
| 176 |
def map_params_to_obj(module): |
| 177 |
text = module.params["text"] |
| 178 |
return { |
| 179 |
"banner": module.params["banner"], |
| 180 |
"text": to_text(text) if text else None, |
| 181 |
"state": module.params["state"], |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
def main(): |
| 186 |
"""main entry point for module execution""" |
| 187 |
argument_spec = dict( |
| 188 |
banner=dict(required=True, choices=["exec", "motd"]), |
| 189 |
text=dict(), |
| 190 |
multiline_delimiter=dict(default="@"), |
| 191 |
state=dict(default="present", choices=["present", "absent"]), |
| 192 |
) |
| 193 |
|
| 194 |
required_if = [("state", "present", ("text",))] |
| 195 |
|
| 196 |
module = AnsibleModule( |
| 197 |
argument_spec=argument_spec, |
| 198 |
required_if=required_if, |
| 199 |
supports_check_mode=True, |
| 200 |
) |
| 201 |
|
| 202 |
warnings = list() |
| 203 |
|
| 204 |
result = {"changed": False} |
| 205 |
if warnings: |
| 206 |
result["warnings"] = warnings |
| 207 |
want = map_params_to_obj(module) |
| 208 |
have = map_config_to_obj(module) |
| 209 |
commands = map_obj_to_commands(want, have, module) |
| 210 |
result["commands"] = commands |
| 211 |
|
| 212 |
if commands: |
| 213 |
if not module.check_mode: |
| 214 |
msgs = load_config(module, commands, True) |
| 215 |
if msgs: |
| 216 |
for item in msgs: |
| 217 |
if item: |
| 218 |
if isinstance(item, dict): |
| 219 |
err_str = item["clierror"] |
| 220 |
else: |
| 221 |
err_str = item |
| 222 |
if "more than 40 lines" in err_str or "buffer overflowed" in err_str: |
| 223 |
load_config(module, commands) |
| 224 |
|
| 225 |
result["changed"] = True |
| 226 |
|
| 227 |
module.exit_json(**result) |
| 228 |
|
| 229 |
|
| 230 |
if __name__ == "__main__": |
| 231 |
main() |