1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#!/usr/bin/python
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos import VyOSModule
DOCUMENTATION = r"""
---
module: vyos_banner
short_description: Manage login banners on VyOS devices using REST API
description:
- Configure pre-login and post-login banners on VyOS devices via REST API.
- Supports idempotent configuration using structured data.
- Multiline banner text is supported.
- Uses REST API (C(connection=httpapi)) instead of CLI.
version_added: "1.0.0"
author:
- Your Name (@yourhandle)
options:
config:
description:
- Banner configuration.
type: dict
required: true
suboptions:
banner:
description:
- Banner type to configure.
type: str
required: true
choices:
- pre-login
- post-login
text:
description:
- Banner text (supports multiline string).
type: str
state:
description:
- Desired state of the configuration.
type: str
default: merged
choices:
- merged
- replaced
- overridden
- deleted
- gathered
notes:
- This module requires C(ansible_connection=httpapi).
- Banner text comparison is whitespace-normalized for idempotency.
"""
EXAMPLES = r"""
- name: Configure pre-login banner
vyos.rest.vyos_banner:
config:
banner: pre-login
text: |
Unauthorized access is prohibited
Disconnect immediately
state: merged
- name: Replace post-login banner
vyos.rest.vyos_banner:
config:
banner: post-login
text: |
Welcome to VyOS
state: replaced
- name: Remove pre-login banner
vyos.rest.vyos_banner:
config:
banner: pre-login
state: deleted
- name: Gather banner configuration
vyos.rest.vyos_banner:
config:
banner: pre-login
state: gathered
"""
RETURN = r"""
before:
description: Configuration before changes.
returned: always
type: dict
after:
description: Configuration after changes.
returned: when changed
type: dict
commands:
description: List of commands sent to the device.
returned: when changes are required
type: list
gathered:
description: Current device configuration.
returned: when state is gathered
type: dict
response:
description: Raw response from VyOS REST API.
returned: when changes are applied
type: dict
"""
# ------------------------------------------------------------
# Helpers
# ------------------------------------------------------------
def normalize_text(text):
if text is None:
return None
return text.strip()
def get_running_config(vyos, banner):
try:
raw = vyos.get_config(["system", "login", "banner"])
except Exception as e:
if "Configuration under specified path is empty" in str(e):
return {"banner": banner, "text": None}
raise
if not raw or not isinstance(raw, dict):
return {"banner": banner, "text": None}
val = raw.get(banner)
if not val:
return {"banner": banner, "text": None}
if isinstance(val, str):
return {
"banner": banner,
"text": val.strip(),
}
if isinstance(val, list):
lines = [line.strip() for line in val if line.strip()]
return {
"banner": banner,
"text": "\n".join(lines) if lines else None,
}
return {"banner": banner, "text": None}
def build_commands(want, have, state):
"""
Build list of VyOS REST API commands to apply `want` configuration
based on current `have` configuration and desired `state`.
Handles:
- missing paths
- idempotency
- multiline banners
- merged, replaced, overridden, deleted
"""
commands = []
banner = want["banner"]
want_text = want.get("text")
have_text = have.get("text")
base_path = ["system", "login", "banner", banner]
want_lines = want_text.splitlines() if want_text else []
have_lines = have_text.splitlines() if have_text else []
banner_exists = have_text is not None
# --------------------------------------------------------
# deleted
# --------------------------------------------------------
if state == "deleted":
if banner_exists or have_text is not None:
commands.append(
{
"op": "delete",
"path": base_path,
},
)
return commands
# --------------------------------------------------------
# merged
# --------------------------------------------------------
if state == "merged":
# normalize both
want_lines = want_text.splitlines() if want_text else []
have_lines = have_text.splitlines() if have_text else []
# if identical → idempotent
if want_lines == have_lines:
return []
# if device empty → just push all
if not have_lines:
return [{"op": "set", "path": base_path + [line]} for line in want_lines]
# append missing lines only
commands = []
for line in want_lines:
if line not in have_lines:
commands.append(
{
"op": "set",
"path": base_path + [line],
},
)
return commands
# if state == "merged":
# # create parent path if missing
# for line in want_lines:
# if line not in have_lines:
# commands.append({
# "op": "set",
# "path": base_path + [line]
# })
# return commands
# --------------------------------------------------------
# replaced / overridden
# --------------------------------------------------------
if state in ["replaced", "overridden"]:
# if existing lines differ from desired lines, delete first
if banner_exists and want_lines != have_lines:
commands.append(
{
"op": "delete",
"path": base_path,
},
)
# create parent path again before setting lines
if want_lines:
commands.append(
{
"op": "set",
"path": base_path,
"value": "",
},
)
# set all desired lines
for line in want_lines:
commands.append(
{
"op": "set",
"path": base_path + [line],
},
)
return commands
return commands
# ------------------------------------------------------------
# Main
# ------------------------------------------------------------
def main():
argument_spec = dict(
config=dict(
type="dict",
required=True,
options=dict(
banner=dict(
type="str",
required=True,
choices=["pre-login", "post-login"],
),
text=dict(type="str"),
),
),
state=dict(
default="merged",
choices=[
"merged",
"replaced",
"overridden",
"deleted",
"gathered",
],
),
)
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
)
vyos = VyOSModule(module)
state = module.params["state"]
config = module.params.get("config") or {}
banner = config.get("banner")
if not banner:
module.fail_json(msg="banner is required in config")
have = get_running_config(vyos, banner)
# --------------------------------------------------------
# gathered
# --------------------------------------------------------
if state == "gathered":
module.exit_json(
changed=False,
gathered=have,
)
want = {
"banner": banner,
"text": config.get("text"),
}
commands = build_commands(want, have, state)
if module.check_mode:
module.exit_json(
changed=bool(commands),
commands=commands,
)
if commands:
response = vyos.apply_commands(commands)
saved = vyos.save_config()
module.exit_json(
changed=True,
before=have,
after=want,
commands=commands,
saved=saved,
response=response,
)
module.exit_json(
changed=False,
before=have,
after=have,
)
if __name__ == "__main__":
main()
|