Debian Codegraph @

ansible_collections/cisco/ios/plugins/modules/ios_banner.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 DOCUMENTATION = """
23 module: ios_banner
24 author: Ricardo Carrillo Cruz (@rcarrillocruz)
25 short_description: Module to configure multiline banners.
26 description:
27 - This will configure both login and motd banners on remote devices running Cisco
28 IOS. It allows playbooks to add or remote banner text from the active running configuration.
29 version_added: 1.0.0
30 extends_documentation_fragment:
31 - cisco.ios.ios
32 notes:
33 - Tested against Cisco IOSXE Version 17.3 on CML.
34 - This module works with connection C(network_cli).
35 See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html)
36 options:
37 banner:
38 description:
39 - Specifies which banner should be configured on the remote device. In Ansible
40 2.4 and earlier only I(login) and I(motd) were supported.
41 required: true
42 choices:
43 - login
44 - motd
45 - exec
46 - incoming
47 - slip-ppp
48 type: str
49 multiline_delimiter:
50 description:
51 - Specify the delimiting character than will be used for configuration.
52 default: "@"
53 type: str
54 text:
55 description:
56 - The banner text that should be present in the remote device running configuration. This
57 argument accepts a multiline string, with no empty lines. Requires I(state=present).
58 type: str
59 state:
60 description:
61 - Specifies whether or not the configuration is present in the current devices
62 active running configuration.
63 default: present
64 type: str
65 choices:
66 - present
67 - absent
68 """
69
70 EXAMPLES = """
71 - name: Configure the login banner
72 cisco.ios.ios_banner:
73 banner: login
74 text: |
75 this is my login banner
76 that contains a multiline
77 string
78 state: present
79
80 - name: Remove the motd banner
81 cisco.ios.ios_banner:
82 banner: motd
83 state: absent
84
85 - name: Configure banner from file
86 cisco.ios.ios_banner:
87 banner: motd
88 text: "{{ lookup('file', './config_partial/raw_banner.cfg') }}" # Use unix formatted text files (LF not CRLF) to avoid idempotency issues.
89 state: present
90
91 - name: Configure the login banner using delimiter
92 cisco.ios.ios_banner:
93 banner: login
94 multiline_delimiter: x
95 text: this is my login banner
96 state: present
97 """
98
99 RETURN = """
100 commands:
101 description: The list of configuration mode commands to send to the device
102 returned: always
103 type: list
104 sample:
105 - banner login
106 - this is my login banner
107 - that contains a multiline
108 - string
109 """
110 from re import M, search
111
112 from ansible.module_utils.basic import AnsibleModule
113
114 from ansible_collections.cisco.ios.plugins.module_utils.network.ios.ios import (
115 get_config,
116 load_config,
117 )
118
119
120 def map_obj_to_commands(updates, module):
121 commands = list()
122 want, have = updates
123 state = module.params["state"]
124 multiline_delimiter = module.params.get("multiline_delimiter")
125 if state == "absent" and "text" in have.keys() and have["text"]:
126 commands.append("no banner %s" % module.params["banner"])
127 elif state == "present":
128 if have.get("text") and len(have.get("text")) > 1:
129 haved = have.get("text").rstrip("\n")
130 else:
131 haved = ""
132 if want["text"] and (want["text"].rstrip("\n") != haved):
133 banner_cmd = "banner %s" % module.params["banner"]
134 banner_cmd += " {0}\n".format(multiline_delimiter)
135 banner_cmd += want["text"].strip("\n")
136 banner_cmd += "\n{0}".format(multiline_delimiter)
137 commands.append(banner_cmd)
138 return commands
139
140
141 def map_config_to_obj(module):
142 """
143 This function gets the banner config without stripping any whitespaces,
144 and then fetches the required banner from it.
145 :param module:
146 :return: banner config dict object.
147 """
148 out = get_config(module, flags="| begin banner %s" % module.params["banner"])
149 if out:
150 regex = search("banner " + module.params["banner"] + " \\^C{1,}\n", out, M)
151 if regex:
152 regex = regex.group()
153 output = str((out.split(regex))[1].split("^C\n")[0])
154 else:
155 output = None
156 else:
157 output = None
158 obj = {"banner": module.params["banner"], "state": "absent"}
159 if output:
160 obj["text"] = output
161 obj["state"] = "present"
162 return obj
163
164
165 def map_params_to_obj(module):
166 text = module.params["text"]
167 return {
168 "banner": module.params["banner"],
169 "text": text,
170 "multiline_delimiter": module.params["multiline_delimiter"],
171 "state": module.params["state"],
172 }
173
174
175 def main():
176 """main entry point for module execution"""
177 argument_spec = dict(
178 banner=dict(required=True, choices=["login", "motd", "exec", "incoming", "slip-ppp"]),
179 multiline_delimiter=dict(default="@"),
180 text=dict(),
181 state=dict(default="present", choices=["present", "absent"]),
182 )
183 required_if = [("state", "present", ("text",))]
184 module = AnsibleModule(
185 argument_spec=argument_spec,
186 required_if=required_if,
187 supports_check_mode=True,
188 )
189 warnings = list()
190 result = {"changed": False}
191 if warnings:
192 result["warnings"] = warnings
193 want = map_params_to_obj(module)
194 have = map_config_to_obj(module)
195 commands = map_obj_to_commands((want, have), module)
196 result["commands"] = commands
197 if commands:
198 if not module.check_mode:
199 load_config(module, commands)
200 result["changed"] = True
201 module.exit_json(**result)
202
203
204 if __name__ == "__main__":
205 main()