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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# GNU General Public License v3.0+
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r"""
module: vyos_ha
short_description: Manage VRRP and load balancer configuration on VyOS via REST API
description:
- Manages VRRP groups, global VRRP parameters, sync-groups, virtual servers, and LVS
real servers on VyOS devices via the REST API.
- Uses REST API (C(connection=httpapi)) instead of CLI.
- Targets VyOS 1.4+.
version_added: 1.0.0
author:
- Evgeny Molotkov (@omnom62)
options:
config:
description: High-availability configuration.
type: dict
suboptions:
disable:
description: Disable all high-availability configuration.
type: bool
default: false
virtual_servers:
description: List of load balancer virtual server definitions.
type: list
elements: dict
suboptions:
name:
type: str
required: true
description: Name.
address:
type: str
description: Address.
algorithm:
type: str
description: Algorithm.
delay_loop:
type: int
description: Delay loop.
forward_method:
type: str
choices:
- direct
- nat
description: Forward method.
fwmark:
type: int
description: Fwmark.
persistence_timeout:
type: int
description: Persistence timeout.
port:
type: int
description: Port.
protocol:
type: str
choices:
- tcp
- udp
description: Protocol.
real_server:
type: list
elements: dict
suboptions:
address:
type: str
required: true
description: Address.
port:
type: int
description: Port.
connection_timeout:
type: int
description: Connection timeout.
health_check_script:
type: str
description: Health check script.
description: Real server.
vrrp:
description: VRRP configuration.
type: dict
suboptions:
global_parameters:
type: dict
suboptions:
garp:
type: dict
suboptions:
interval:
type: int
description: Interval.
master_delay:
type: int
description: Master delay.
master_refresh:
type: int
description: Master refresh.
master_refresh_repeat:
type: int
description: Master refresh repeat.
master_repeat:
type: int
description: Master repeat.
description: Garp.
startup_delay:
type: int
description: Startup delay.
version:
type: str
description: Version.
description: Global parameters.
groups:
type: list
elements: dict
suboptions:
name:
type: str
required: true
description: Name.
address:
type: list
elements: str
description: Address.
advertise_interval:
type: int
description: Advertise interval.
authentication:
type: dict
suboptions:
password:
type: str
description: Password.
type:
type: str
description: Type.
description: Authentication.
description:
type: str
description: Description.
disable:
type: bool
default: false
description: Disable.
excluded_address:
type: list
elements: str
description: Excluded address.
garp:
type: dict
suboptions:
interval:
type: int
description: Interval.
master_delay:
type: int
description: Master delay.
master_refresh:
type: int
description: Master refresh.
master_refresh_repeat:
type: int
description: Master refresh repeat.
master_repeat:
type: int
description: Master repeat.
description: Garp.
health_check:
type: dict
suboptions:
failure_count:
type: int
description: Failure count.
interval:
type: int
description: Interval.
ping:
type: str
description: Ping.
script:
type: str
description: Script.
description: Health check.
hello_source_address:
type: str
description: Hello source address.
interface:
type: str
description: Interface.
no_preempt:
type: bool
default: false
description: No preempt.
peer_address:
type: str
description: Peer address.
preempt_delay:
type: int
description: Preempt delay.
priority:
type: int
description: Priority.
rfc3768_compatibility:
type: bool
default: false
description: Rfc3768 compatibility.
track:
type: dict
suboptions:
exclude_vrrp_interface:
type: bool
description: Exclude vrrp interface.
interface:
type: list
elements: str
description: Interface.
description: Track.
transition_script:
type: dict
suboptions:
backup:
type: str
description: Backup.
fault:
type: str
description: Fault.
master:
type: str
description: Master.
stop:
type: str
description: Stop.
description: Transition script.
vrid:
type: int
description: Vrid.
description: Groups.
snmp:
type: str
choices:
- enabled
- disabled
description: Snmp.
sync_groups:
type: list
elements: dict
suboptions:
name:
type: str
required: true
description: Name.
health_check:
type: dict
suboptions:
failure_count:
type: int
description: Failure count.
interval:
type: int
description: Interval.
ping:
type: str
description: Ping.
script:
type: str
description: Script.
description: Health check.
member:
type: list
elements: str
description: Member.
transition_script:
type: dict
suboptions:
backup:
type: str
description: Backup.
fault:
type: str
description: Fault.
master:
type: str
description: Master.
stop:
type: str
description: Stop.
description: Transition script.
description: Sync groups.
state:
description: Desired end state of the configuration.
type: str
choices:
- merged
- replaced
- overridden
- deleted
- gathered
default: merged
"""
EXAMPLES = r"""
- name: Merge VRRP configuration
vyos.rest.vyos_ha:
config:
vrrp:
global_parameters:
startup_delay: 30
groups:
- name: g1
interface: eth0
vrid: 20
priority: 100
address:
- 192.168.1.100/24
sync_groups:
- name: sg1
member: [g1]
snmp: enabled
state: merged
- name: Delete all HA configuration
vyos.rest.vyos_ha:
state: deleted
- name: Gather current HA configuration
vyos.rest.vyos_ha:
state: gathered
"""
RETURN = r"""
before:
description: HA configuration before this module ran.
returned: always
type: dict
after:
description: HA configuration after this module ran.
returned: when changed
type: dict
commands:
description: List of API commands sent to the device.
returned: always
type: list
gathered:
description: Current HA configuration as structured data.
returned: when state is gathered
type: dict
saved:
description: Whether the config was saved after changes.
returned: when changed
type: bool
"""
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos import (
VyOSModule,
autoclean,
cast_by_spec,
dict_op,
from_device,
normalize_have,
to_tag_dict,
)
_BASE = ["high-availability"]
ARGUMENT_SPEC = dict(
config=dict(
type="dict",
options=dict(
disable=dict(type="bool", default=False),
virtual_servers=dict(
type="list",
elements="dict",
options=dict(
name=dict(type="str", required=True),
address=dict(type="str"),
algorithm=dict(type="str"),
delay_loop=dict(type="int"),
forward_method=dict(type="str", choices=["direct", "nat"]),
fwmark=dict(type="int"),
persistence_timeout=dict(type="int"),
port=dict(type="int"),
protocol=dict(type="str", choices=["tcp", "udp"]),
real_server=dict(
type="list",
elements="dict",
options=dict(
address=dict(type="str", required=True),
port=dict(type="int"),
connection_timeout=dict(type="int"),
health_check_script=dict(type="str"),
),
),
),
),
vrrp=dict(
type="dict",
options=dict(
global_parameters=dict(
type="dict",
options=dict(
garp=dict(
type="dict",
options=dict(
interval=dict(type="int"),
master_delay=dict(type="int"),
master_refresh=dict(type="int"),
master_refresh_repeat=dict(type="int"),
master_repeat=dict(type="int"),
),
),
startup_delay=dict(type="int"),
version=dict(type="str"),
),
),
groups=dict(
type="list",
elements="dict",
options=dict(
name=dict(type="str", required=True),
address=dict(type="list", elements="str"),
advertise_interval=dict(type="int"),
authentication=dict(
type="dict",
options=dict(
password=dict(type="str", no_log=True),
type=dict(type="str"),
),
),
description=dict(type="str"),
disable=dict(type="bool", default=False),
excluded_address=dict(type="list", elements="str"),
garp=dict(
type="dict",
options=dict(
interval=dict(type="int"),
master_delay=dict(type="int"),
master_refresh=dict(type="int"),
master_refresh_repeat=dict(type="int"),
master_repeat=dict(type="int"),
),
),
health_check=dict(
type="dict",
options=dict(
failure_count=dict(type="int"),
interval=dict(type="int"),
ping=dict(type="str"),
script=dict(type="str"),
),
),
hello_source_address=dict(type="str"),
interface=dict(type="str"),
no_preempt=dict(type="bool", default=False),
peer_address=dict(type="str"),
preempt_delay=dict(type="int"),
priority=dict(type="int"),
rfc3768_compatibility=dict(type="bool", default=False),
track=dict(
type="dict",
options=dict(
exclude_vrrp_interface=dict(type="bool"),
interface=dict(type="list", elements="str"),
),
),
transition_script=dict(
type="dict",
options=dict(
backup=dict(type="str"),
fault=dict(type="str"),
master=dict(type="str"),
stop=dict(type="str"),
),
),
vrid=dict(type="int"),
),
),
snmp=dict(type="str", choices=["enabled", "disabled"]),
sync_groups=dict(
type="list",
elements="dict",
options=dict(
name=dict(type="str", required=True),
health_check=dict(
type="dict",
options=dict(
failure_count=dict(type="int"),
interval=dict(type="int"),
ping=dict(type="str"),
script=dict(type="str"),
),
),
member=dict(type="list", elements="str"),
transition_script=dict(
type="dict",
options=dict(
backup=dict(type="str"),
fault=dict(type="str"),
master=dict(type="str"),
stop=dict(type="str"),
),
),
),
),
),
),
),
),
state=dict(
default="merged",
choices=["merged", "replaced", "overridden", "deleted", "gathered"],
),
)
_TOP_OPTIONS = ARGUMENT_SPEC["config"]["options"]
_VS_OPTIONS = _TOP_OPTIONS["virtual_servers"]["options"]
_RS_OPTIONS = _VS_OPTIONS["real_server"]["options"]
_VRRP_OPTIONS = _TOP_OPTIONS["vrrp"]["options"]
_GROUP_OPTIONS = _VRRP_OPTIONS["groups"]["options"]
_SYNC_GROUP_OPTIONS = _VRRP_OPTIONS["sync_groups"]["options"]
# Tag nodes VyOS's REST API can collapse to a bare string/list for a
# single entry with no other config. Split by section because "address"
# means two different things depending on where it appears -- confirmed
# against vyos-1x: vrrp.group.<name>.address is a genuine tagNode (VRRP
# virtual IPs, each with real child structure), but virtual-server.
# <name>.address is a flat scalar string (the load-balancer's own bind
# address). A single blanket key-name-based coercion across the whole
# raw tree would wrongly reshape the latter into a tag-node dict --
# exactly the class of bug this split avoids.
_VS_TAG_KEYS = {"virtual-server", "real-server"}
_VRRP_TAG_KEYS = {"group", "sync-group", "address", "excluded-address"}
# track.interface and sync-group.member are NOT included above --
# confirmed <leafNode><multi/>, i.e. plain multi-value leaves, not tag
# nodes; dict_op's own native list handling (which already corrects for
# the same single-value-collapse quirk) applies to them directly, no
# reshaping needed.
# ---------------------------------------------------------------------------
# Structural adapters — the only genuine exceptions, confirmed against
# vyos-1x schema, not assumed:
# 1. Named-object lists (virtual_servers, real_server, groups,
# sync_groups): argspec uses [{name: "x", ...}], device uses
# {"x": {...}}.
# 2. address / excluded_address (VRRP group virtual IPs): genuine
# tagNodes (each has real child structure) -> {"a": {}, "b": {}}.
# 3. snmp: argspec "enabled"/"disabled" string <-> device presence node
# (present) / absent. "disabled" has no device-side representation at
# all -- see the explicit delete in build_commands().
# 4. health_check_script: argspec flat field <-> device nested under
# health-check.script.
#
# Everything else -- including track.interface and sync_group.member,
# both plain multi-value leaves despite superficially looking like the
# same shape as address/excluded_address -- flows through autoclean/
# from_device untouched.
# ---------------------------------------------------------------------------
def _real_server_to_device(rs):
entry = autoclean(
{k: v for k, v in rs.items() if k not in ("address", "health_check_script")},
)
if rs.get("health_check_script"):
entry["health-check"] = {"script": rs["health_check_script"]}
return entry
def _real_server_from_device(addr, data):
data = dict(data or {})
hc = data.pop("health-check", None) or {}
entry = {"address": addr, **from_device(data)}
if hc.get("script"):
entry["health_check_script"] = hc["script"]
cast_by_spec(entry, _RS_OPTIONS)
return entry
def _virtual_server_to_device(vs):
entry = autoclean({k: v for k, v in vs.items() if k not in ("name", "real_server")})
if vs.get("real_server"):
entry["real-server"] = {
rs["address"]: _real_server_to_device(rs) for rs in vs["real_server"]
}
return entry
def _virtual_server_from_device(name, data):
data = dict(data or {})
rs_raw = data.pop("real-server", None) or {}
entry = {"name": name, **from_device(data)}
cast_by_spec(entry, _VS_OPTIONS)
if rs_raw:
entry["real_server"] = [
_real_server_from_device(addr, rdata) for addr, rdata in sorted(rs_raw.items())
]
return entry
def _group_to_device(grp):
entry = autoclean(
{k: v for k, v in grp.items() if k not in ("name", "address", "excluded_address")},
)
if grp.get("address"):
entry["address"] = {a: {} for a in grp["address"]}
if grp.get("excluded_address"):
entry["excluded-address"] = {a: {} for a in grp["excluded_address"]}
return entry
def _group_from_device(name, data):
data = dict(data or {})
addr_raw = data.pop("address", None)
excl_raw = data.pop("excluded-address", None)
entry = {"name": name, **from_device(data)}
cast_by_spec(entry, _GROUP_OPTIONS)
if addr_raw:
entry["address"] = sorted(to_tag_dict(addr_raw).keys())
if excl_raw:
entry["excluded_address"] = sorted(to_tag_dict(excl_raw).keys())
return entry
def _sync_group_from_device(name, data):
entry = {"name": name, **from_device(data or {})}
cast_by_spec(entry, _SYNC_GROUP_OPTIONS)
return entry
def _want_to_device(config):
if not config:
return {}
want = autoclean({k: v for k, v in config.items() if k not in ("virtual_servers", "vrrp")})
if config.get("virtual_servers"):
want["virtual-server"] = {
vs["name"]: _virtual_server_to_device(vs) for vs in config["virtual_servers"]
}
vrrp = config.get("vrrp") or {}
if vrrp:
vrrp_dev = autoclean(
{k: v for k, v in vrrp.items() if k not in ("groups", "sync_groups", "snmp")},
)
# snmp: "enabled" -> presence node; "disabled" has no device-side
# form at all (handled via an explicit delete in build_commands).
if vrrp.get("snmp") == "enabled":
vrrp_dev["snmp"] = {}
if vrrp.get("groups"):
vrrp_dev["group"] = {g["name"]: _group_to_device(g) for g in vrrp["groups"]}
if vrrp.get("sync_groups"):
vrrp_dev["sync-group"] = {
sg["name"]: autoclean({k: v for k, v in sg.items() if k != "name"})
for sg in vrrp["sync_groups"]
}
if vrrp_dev:
want["vrrp"] = vrrp_dev
return want
def get_running_config(vyos):
return vyos.get_config(_BASE) or {}
def _device_to_argspec(raw):
if not raw:
return {}
result = from_device({k: v for k, v in raw.items() if k not in ("virtual-server", "vrrp")})
cast_by_spec(result, _TOP_OPTIONS)
vs_raw = raw.get("virtual-server") or {}
if vs_raw:
result["virtual_servers"] = [
_virtual_server_from_device(name, data) for name, data in sorted(vs_raw.items())
]
vrrp_raw = raw.get("vrrp") or {}
if vrrp_raw:
vrrp_arg = from_device(
{k: v for k, v in vrrp_raw.items() if k not in ("group", "sync-group", "snmp")},
)
cast_by_spec(vrrp_arg, _VRRP_OPTIONS)
if "snmp" in vrrp_raw:
vrrp_arg["snmp"] = "enabled"
grp_raw = vrrp_raw.get("group") or {}
if grp_raw:
vrrp_arg["groups"] = [
_group_from_device(name, data) for name, data in sorted(grp_raw.items())
]
sg_raw = vrrp_raw.get("sync-group") or {}
if sg_raw:
vrrp_arg["sync_groups"] = [
_sync_group_from_device(name, data) for name, data in sorted(sg_raw.items())
]
if vrrp_arg:
result["vrrp"] = vrrp_arg
return result
def build_commands(config, raw_have, state):
raw_have = raw_have or {}
config = config or {}
if state == "deleted":
return [("delete", _BASE)] if raw_have else []
want = _want_to_device(config)
norm_have = {k: v for k, v in raw_have.items() if k not in ("virtual-server", "vrrp")}
if raw_have.get("virtual-server"):
norm_have["virtual-server"] = normalize_have(raw_have, _VS_TAG_KEYS)["virtual-server"]
if raw_have.get("vrrp"):
norm_have["vrrp"] = normalize_have(raw_have, _VRRP_TAG_KEYS)["vrrp"]
commands = []
if state == "overridden":
commands += dict_op(want, norm_have, _BASE, op="purge")
elif state == "replaced":
for section, section_want in want.items():
section_have = norm_have.get(section, {})
commands += dict_op(section_want, section_have, _BASE + [section], op="purge")
commands += dict_op(want, norm_have, _BASE, op="set")
# snmp "disabled" has no device-side value to compare against --
# it's the absence of the presence node, which dict_op's set/purge
# logic can't express as a "delete" on its own. Handled explicitly.
if (config.get("vrrp") or {}).get("snmp") == "disabled":
if "snmp" in (raw_have.get("vrrp") or {}):
commands.append(("delete", _BASE + ["vrrp", "snmp"]))
return commands
def main():
module = AnsibleModule(ARGUMENT_SPEC, supports_check_mode=True)
vyos = VyOSModule(module)
state = module.params["state"]
config = module.params.get("config") or {}
raw_have = get_running_config(vyos)
have = _device_to_argspec(raw_have)
if state == "gathered":
module.exit_json(changed=False, gathered=have)
commands = build_commands(config, raw_have, state)
if module.check_mode:
module.exit_json(changed=bool(commands), commands=commands, before=have)
if commands:
response = vyos.apply_commands(commands)
saved = vyos.save_config()
after = _device_to_argspec(get_running_config(vyos))
module.exit_json(
changed=True,
before=have,
after=after,
commands=commands,
saved=saved,
response=response,
)
module.exit_json(changed=False, before=have, after=have, commands=[])
if __name__ == "__main__":
main()
|