summaryrefslogtreecommitdiff
path: root/plugins/modules/vyos_ospf_interfaces.py
blob: 5388ef06fe03ef91dede24f4f93faf07246ba706 (plain)
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
#!/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_ospf_interfaces
short_description: Manage OSPF interface configuration on VyOS devices using REST API
description:
  - Manages OSPF and OSPFv3 interface configuration on VyOS devices via the REST API.
  - IPv4 OSPF maps to C(protocols ospf interface).
  - IPv6 OSPFv3 maps to C(protocols ospfv3 interface).
  - Uses REST API (C(connection=httpapi)) instead of CLI.
version_added: "1.0.0"
author:
  - VyOS Community (@vyos)
options:
  config:
    description: List of OSPF interface configurations.
    type: list
    elements: dict
    suboptions:
      name:
        description: Interface name.
        type: str
        required: true
      address_family:
        description: OSPF settings per address family.
        type: list
        elements: dict
        suboptions:
          afi:
            description: Address family identifier.
            type: str
            choices: [ipv4, ipv6]
            required: true
          authentication:
            description: Authentication settings (IPv4 only).
            type: dict
            suboptions:
              plaintext_password:
                description: Plaintext password.
                type: str
              md5_key:
                description: MD5 authentication key.
                type: dict
                suboptions:
                  key_id:
                    description: MD5 key ID.
                    type: int
                  key:
                    description: MD5 key string.
                    type: str
          bandwidth:
            description: Interface bandwidth in kbps (IPv4 only).
            type: int
          cost:
            description: Interface cost metric.
            type: int
          dead_interval:
            description: Dead router detection interval in seconds.
            type: int
          hello_interval:
            description: Hello packet interval in seconds.
            type: int
          ifmtu:
            description: Interface MTU (IPv6 only).
            type: int
          instance:
            description: OSPFv3 instance ID (IPv6 only).
            type: str
          mtu_ignore:
            description: Disable MTU check (IPv4 only).
            type: bool
          network:
            description: Network type (IPv4 only).
            type: str
            choices: [broadcast, non-broadcast, point-to-multipoint, point-to-point]
          passive:
            description: Disable adjacency formation (IPv6 only).
            type: bool
          priority:
            description: Interface priority.
            type: int
          retransmit_interval:
            description: LSA retransmit interval in seconds.
            type: int
          transmit_delay:
            description: LSA transmit delay in seconds.
            type: int
  state:
    description:
      - Desired state of the OSPF interface configuration.
      - C(merged) adds or updates without removing existing config.
      - C(replaced) replaces per-interface OSPF config for named interfaces.
      - C(overridden) replaces all OSPF interface config.
      - C(deleted) removes OSPF interface config.
      - C(gathered) returns current configuration as structured data.
    type: str
    choices: [merged, replaced, overridden, deleted, gathered]
    default: merged
notes:
  - Requires C(ansible_connection=httpapi) with the VyOS httpapi plugin.
  - C(ansible_network_os) must be set to C(vyos.rest.vyos).
"""

EXAMPLES = r"""
- name: Merge OSPF interface configuration
  vyos.rest.vyos_ospf_interfaces:
    config:
      - name: eth1
        address_family:
          - afi: ipv4
            cost: 100
            transmit_delay: 50
            priority: 26
          - afi: ipv6
            dead_interval: 39
            passive: true
    state: merged

- name: Delete OSPF interface configuration
  vyos.rest.vyos_ospf_interfaces:
    config:
      - name: eth1
    state: deleted

- name: Delete all OSPF interface configuration
  vyos.rest.vyos_ospf_interfaces:
    state: deleted

- name: Gather current OSPF interface configuration
  vyos.rest.vyos_ospf_interfaces:
    state: gathered
"""

RETURN = r"""
before:
  description: OSPF interface configuration before this module ran.
  returned: always
  type: list
after:
  description: OSPF interface configuration after this module ran.
  returned: when changed
  type: list
commands:
  description: List of API command tuples sent to the device.
  returned: always
  type: list
gathered:
  description: Current OSPF interface configuration as structured data.
  returned: when state is gathered
  type: list
saved:
  description: Whether the config was saved after changes.
  returned: when changes are applied
  type: bool
response:
  description: Raw API response.
  returned: when changes are applied
  type: dict
"""

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos import VyOSModule


_BASE4 = ["protocols", "ospf", "interface"]
_BASE6 = ["protocols", "ospfv3", "interface"]

# IPv4 scalar fields: argspec_key -> api_key
_IPV4_SCALARS = {
    "bandwidth": "bandwidth",
    "cost": "cost",
    "dead_interval": "dead-interval",
    "hello_interval": "hello-interval",
    "mtu_ignore": "mtu-ignore",
    "network": "network",
    "priority": "priority",
    "retransmit_interval": "retransmit-interval",
    "transmit_delay": "transmit-delay",
}

# IPv6 scalar fields
_IPV6_SCALARS = {
    "cost": "cost",
    "dead_interval": "dead-interval",
    "hello_interval": "hello-interval",
    "ifmtu": "ifmtu",
    "instance": "instance-id",
    "passive": "passive",
    "priority": "priority",
    "retransmit_interval": "retransmit-interval",
    "transmit_delay": "transmit-delay",
}

# bool fields that use presence (no value)
_IPV4_BOOL_PRESENCE = {"mtu_ignore"}
_IPV6_BOOL_PRESENCE = {"passive"}


def _parse_ipv4_iface(data):
    af = {"afi": "ipv4"}
    data = data or {}
    for arg_key, api_key in _IPV4_SCALARS.items():
        if api_key in data:
            if arg_key in _IPV4_BOOL_PRESENCE:
                af[arg_key] = True
            else:
                val = data[api_key]
                if arg_key in (
                    "bandwidth",
                    "cost",
                    "dead_interval",
                    "hello_interval",
                    "priority",
                    "retransmit_interval",
                    "transmit_delay",
                ):
                    try:
                        af[arg_key] = int(val)
                    except (TypeError, ValueError):
                        af[arg_key] = val
                else:
                    af[arg_key] = val
    # authentication
    auth = data.get("authentication", {})
    if auth:
        auth_entry = {}
        if "plaintext-password" in auth:
            auth_entry["plaintext_password"] = auth["plaintext-password"]
        md5 = auth.get("md5", {})
        if md5:
            key_id_data = md5.get("key-id", {})
            if key_id_data:
                key_id = list(key_id_data.keys())[0]
                md5_key = key_id_data[key_id].get("md5-key")
                auth_entry["md5_key"] = {"key_id": int(key_id), "key": md5_key}
        if auth_entry:
            af["authentication"] = auth_entry
    return af


def _parse_ipv6_iface(data):
    af = {"afi": "ipv6"}
    data = data or {}
    for arg_key, api_key in _IPV6_SCALARS.items():
        if api_key in data:
            if arg_key in _IPV6_BOOL_PRESENCE:
                af[arg_key] = True
            else:
                val = data[api_key]
                if arg_key in (
                    "cost",
                    "dead_interval",
                    "hello_interval",
                    "ifmtu",
                    "priority",
                    "retransmit_interval",
                    "transmit_delay",
                ):
                    try:
                        af[arg_key] = int(val)
                    except (TypeError, ValueError):
                        af[arg_key] = val
                else:
                    af[arg_key] = val
    return af


def get_running_config(vyos):
    raw4 = vyos.get_config(_BASE4) or {}
    raw4 = raw4.get("interface", raw4)
    raw6 = vyos.get_config(_BASE6) or {}
    raw6 = raw6.get("interface", raw6)

    ifaces = {}

    for iface_name, data in raw4.items():
        if iface_name not in ifaces:
            ifaces[iface_name] = {"name": iface_name, "address_family": []}
        af = _parse_ipv4_iface(data)
        if len(af) > 1:  # more than just afi key
            ifaces[iface_name]["address_family"].append(af)

    for iface_name, data in raw6.items():
        if iface_name not in ifaces:
            ifaces[iface_name] = {"name": iface_name, "address_family": []}
        af = _parse_ipv6_iface(data)
        if len(af) > 1:
            ifaces[iface_name]["address_family"].append(af)

    result = sorted(ifaces.values(), key=lambda x: x["name"])
    # Remove empty address_family lists
    for iface in result:
        if not iface["address_family"]:
            del iface["address_family"]
    return result


def _ipv4_af_cmds(iface_name, af, have_af, op="set"):
    cmds = []
    base = _BASE4 + [iface_name]
    have_af = have_af or {}

    for arg_key, api_key in _IPV4_SCALARS.items():
        want_val = af.get(arg_key)
        have_val = have_af.get(arg_key)
        if want_val is not None and want_val != have_val:
            if arg_key in _IPV4_BOOL_PRESENCE:
                cmds.append(("set", base + [api_key]))
            else:
                cmds.append(("set", base + [api_key, str(want_val)]))
        elif op == "replace" and have_val is not None and want_val != have_val:
            cmds.append(("delete", base + [api_key]))

    # authentication
    want_auth = af.get("authentication") or {}
    have_auth = have_af.get("authentication") or {}
    if want_auth.get("plaintext_password") and want_auth["plaintext_password"] != have_auth.get(
        "plaintext_password",
    ):
        cmds.append(
            (
                "set",
                base
                + [
                    "authentication",
                    "plaintext-password",
                    want_auth["plaintext_password"],
                ],
            ),
        )
    md5 = want_auth.get("md5_key") or {}
    if md5 and md5 != have_auth.get("md5_key"):
        cmds.append(
            (
                "set",
                base
                + [
                    "authentication",
                    "md5",
                    "key-id",
                    str(md5["key_id"]),
                    "md5-key",
                    md5["key"],
                ],
            ),
        )

    return cmds


def _ipv6_af_cmds(iface_name, af, have_af, op="set"):
    cmds = []
    base = _BASE6 + [iface_name]
    have_af = have_af or {}

    for arg_key, api_key in _IPV6_SCALARS.items():
        want_val = af.get(arg_key)
        have_val = have_af.get(arg_key)
        if want_val is not None and want_val != have_val:
            if arg_key in _IPV6_BOOL_PRESENCE:
                cmds.append(("set", base + [api_key]))
            else:
                cmds.append(("set", base + [api_key, str(want_val)]))
        elif op == "replace" and have_val is not None and want_val != have_val:
            cmds.append(("delete", base + [api_key]))

    return cmds


def build_commands(config, have_raw, state):
    cmds = []
    have_map = {e["name"]: e for e in have_raw}

    if state == "deleted":
        if not config:
            # delete all
            if have_raw:
                for iface in have_raw:
                    name = iface["name"]
                    afs = {af["afi"] for af in iface.get("address_family", [])}
                    if "ipv4" in afs:
                        cmds.append(("delete", _BASE4 + [name]))
                    if "ipv6" in afs:
                        cmds.append(("delete", _BASE6 + [name]))
        else:
            for entry in config:
                name = entry["name"]
                if name in have_map:
                    have_afs = {af["afi"] for af in have_map[name].get("address_family", [])}
                    want_afis = {af["afi"] for af in (entry.get("address_family") or [])}
                    if not want_afis:
                        # delete all AFIs for this interface
                        if "ipv4" in have_afs:
                            cmds.append(("delete", _BASE4 + [name]))
                        if "ipv6" in have_afs:
                            cmds.append(("delete", _BASE6 + [name]))
                    else:
                        if "ipv4" in want_afis and "ipv4" in have_afs:
                            cmds.append(("delete", _BASE4 + [name]))
                        if "ipv6" in want_afis and "ipv6" in have_afs:
                            cmds.append(("delete", _BASE6 + [name]))
        return cmds

    if state == "overridden":
        want_names = {e["name"] for e in config}
        for name, have_iface in have_map.items():
            if name not in want_names:
                have_afs = {af["afi"] for af in have_iface.get("address_family", [])}
                if "ipv4" in have_afs:
                    cmds.append(("delete", _BASE4 + [name]))
                if "ipv6" in have_afs:
                    cmds.append(("delete", _BASE6 + [name]))

    for entry in config:
        name = entry["name"]
        have_iface = have_map.get(name, {})
        have_af_map = {af["afi"]: af for af in have_iface.get("address_family", [])}

        for af in entry.get("address_family", []):
            afi = af["afi"]
            have_af = have_af_map.get(afi, {})

            if state == "replaced":
                af_clean = {k: v for k, v in af.items() if v is not None}
                if have_af and have_af != af_clean:
                    if afi == "ipv4":
                        cmds.append(("delete", _BASE4 + [name]))
                        have_af = {}
                    else:
                        cmds.append(("delete", _BASE6 + [name]))
                        have_af = {}
                elif have_af == af_clean:
                    continue  # already matches — idempotent

            if afi == "ipv4":
                cmds += _ipv4_af_cmds(name, af, have_af)
            else:
                cmds += _ipv6_af_cmds(name, af, have_af)

    return cmds


ARGUMENT_SPEC = dict(
    config=dict(
        type="list",
        elements="dict",
        options=dict(
            name=dict(type="str", required=True),
            address_family=dict(
                type="list",
                elements="dict",
                options=dict(
                    afi=dict(type="str", choices=["ipv4", "ipv6"], required=True),
                    authentication=dict(
                        type="dict",
                        options=dict(
                            plaintext_password=dict(type="str", no_log=True),
                            md5_key=dict(
                                type="dict",
                                no_log=True,
                                options=dict(
                                    key_id=dict(type="int"),
                                    key=dict(type="str", no_log=True),
                                ),
                            ),
                        ),
                    ),
                    bandwidth=dict(type="int"),
                    cost=dict(type="int"),
                    dead_interval=dict(type="int"),
                    hello_interval=dict(type="int"),
                    ifmtu=dict(type="int"),
                    instance=dict(type="str"),
                    mtu_ignore=dict(type="bool"),
                    network=dict(
                        type="str",
                        choices=[
                            "broadcast",
                            "non-broadcast",
                            "point-to-multipoint",
                            "point-to-point",
                        ],
                    ),
                    passive=dict(type="bool"),
                    priority=dict(type="int"),
                    retransmit_interval=dict(type="int"),
                    transmit_delay=dict(type="int"),
                ),
            ),
        ),
    ),
    state=dict(
        default="merged",
        choices=["merged", "replaced", "overridden", "deleted", "gathered"],
    ),
)


def main():
    module = AnsibleModule(ARGUMENT_SPEC, supports_check_mode=True)
    vyos = VyOSModule(module)

    state = module.params["state"]
    config = module.params.get("config") or []

    have = get_running_config(vyos)

    if state == "gathered":
        module.exit_json(changed=False, gathered=have)

    commands = build_commands(config, 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()
        module.exit_json(
            changed=True,
            before=have,
            after=get_running_config(vyos),
            commands=commands,
            saved=saved,
            response=response,
        )

    module.exit_json(changed=False, before=have, after=have, commands=[])


if __name__ == "__main__":
    main()