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
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r"""
---
module: vyos_interfaces
short_description: Manage interface attributes on a VyOS device via the REST API.
description:
- Manages interface base attributes (description, MTU, enabled state, VIFs,
duplex, speed) on VyOS devices using the HTTPS REST API.
- Mirrors C(vyos.vyos.vyos_interfaces) but uses HTTP API.
- Supports Ethernet, Bonding, VXLAN, Loopback, and Virtual Tunnel interfaces.
version_added: "1.0.0"
author:
- VyOS Community (@vyos)
options:
config:
description: List of interface configurations.
type: list
elements: dict
suboptions:
name:
description: >
Full interface name (e.g. C(eth0), C(bond0), C(vti1), C(vxlan2)).
type: str
required: true
description:
description: Interface description.
type: str
enabled:
description: >
Administrative state. C(true) = up, C(false) = admin-down.
type: bool
default: true
mtu:
description: MTU in bytes. Applicable for Ethernet, Bonding, VXLAN, VTI.
type: int
duplex:
description: Duplex mode. Only for Ethernet interfaces.
type: str
choices: [auto, full, half]
speed:
description: Link speed. Only for Ethernet interfaces.
type: str
vifs:
description: List of VLAN sub-interface configurations.
type: list
elements: dict
suboptions:
vlan_id:
description: 802.1Q VLAN ID.
type: int
required: true
description:
description: VIF description.
type: str
enabled:
description: Administrative state of the VIF.
type: bool
default: true
state:
description:
- C(merged): Merge the provided config with existing interface config.
- C(replaced): Replace per-interface config with provided values.
- C(overridden): Override all interface config with provided values.
- C(deleted): Delete interface config for listed interfaces (or all).
- C(gathered): Read current interface config from device.
type: str
choices: [merged, replaced, overridden, deleted, gathered]
default: merged
hostname:
description: IP address or FQDN of the VyOS device.
type: str
required: true
port:
description: HTTPS port for the REST API.
type: int
default: 443
api_key:
description: API key configured on the device.
type: str
required: true
no_log: true
timeout:
description: Request timeout in seconds.
type: int
default: 30
verify_ssl:
description: Validate the device's TLS certificate.
type: bool
default: false
requirements:
- VyOS 1.3+
seealso:
- module: vyos.vyos.vyos_interfaces
- module: vyos.rest.vyos_l3_interfaces
examples: |
- name: Set description and MTU on eth1
vyos.rest.vyos_interfaces:
hostname: 192.168.1.1
api_key: MY-KEY
config:
- name: eth1
description: "WAN uplink"
mtu: 1500
enabled: true
state: merged
- name: Disable eth2
vyos.rest.vyos_interfaces:
hostname: 192.168.1.1
api_key: MY-KEY
config:
- name: eth2
enabled: false
state: merged
- name: Create a VLAN sub-interface
vyos.rest.vyos_interfaces:
hostname: 192.168.1.1
api_key: MY-KEY
config:
- name: eth1
vifs:
- vlan_id: 100
description: "VLAN 100"
state: merged
"""
RETURN = r"""
before:
description: Interface configuration before the module ran.
returned: always
type: list
after:
description: Interface configuration after the module ran.
returned: when changed
type: list
gathered:
description: Interface config read from device (state=gathered).
returned: when state is gathered
type: list
commands:
description: REST API set/delete commands issued.
returned: always
type: list
"""
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos_rest import (
VYOS_REST_CONNECTION_ARGSPEC,
VyOSRestClient,
VyOSRestError,
)
_IFACE_TYPES = {
"eth": "ethernet",
"bond": "bonding",
"vti": "vti",
"vxlan": "vxlan",
"lo": "loopback",
"dummy": "dummy",
"peth": "pseudo-ethernet",
"vtun": "openvpn",
"wg": "wireguard",
"br": "bridge",
"macsec": "macsec",
"tun": "tunnel",
}
def _iface_type(name):
"""Infer the VyOS interface type from its name prefix."""
for prefix, itype in _IFACE_TYPES.items():
if name.startswith(prefix):
return itype
return "ethernet"
def _iface_base_path(name):
return ["interfaces", _iface_type(name), name]
def _get_interfaces(client):
try:
result = client.retrieve_show_config(["interfaces"])
ifaces_raw = result.get("data") or {}
interfaces = []
for itype, itype_data in ifaces_raw.items():
if not isinstance(itype_data, dict):
continue
for iname, idata in itype_data.items():
if not isinstance(idata, dict):
continue
entry = {"name": iname}
if "description" in idata:
entry["description"] = idata["description"]
if "mtu" in idata:
entry["mtu"] = int(idata["mtu"])
entry["enabled"] = "disable" not in idata
if "duplex" in idata:
entry["duplex"] = idata["duplex"]
if "speed" in idata:
entry["speed"] = idata["speed"]
if "vif" in idata:
vifs = []
for vid, vdata in idata["vif"].items():
vif_entry = {"vlan_id": int(vid)}
if isinstance(vdata, dict):
if "description" in vdata:
vif_entry["description"] = vdata["description"]
vif_entry["enabled"] = "disable" not in vdata
vifs.append(vif_entry)
entry["vifs"] = vifs
interfaces.append(entry)
return interfaces
except VyOSRestError:
return []
def _apply_interface(client, iface_cfg, commands):
name = iface_cfg["name"]
base = _iface_base_path(name)
if iface_cfg.get("description") is not None:
client.configure_set(base + ["description"], iface_cfg["description"])
commands.append(
"set interfaces {t} {n} description '{d}'".format(
t=_iface_type(name),
n=name,
d=iface_cfg["description"],
),
)
if iface_cfg.get("mtu") is not None:
client.configure_set(base + ["mtu"], str(iface_cfg["mtu"]))
commands.append(
"set interfaces {t} {n} mtu {m}".format(
t=_iface_type(name),
n=name,
m=iface_cfg["mtu"],
),
)
if "enabled" in iface_cfg:
if not iface_cfg["enabled"]:
client.configure_set(base + ["disable"])
commands.append(
"set interfaces {t} {n} disable".format(t=_iface_type(name), n=name),
)
else:
try:
client.configure_delete(base + ["disable"])
commands.append(
"delete interfaces {t} {n} disable".format(
t=_iface_type(name),
n=name,
),
)
except VyOSRestError:
pass
if iface_cfg.get("duplex"):
client.configure_set(base + ["duplex"], iface_cfg["duplex"])
commands.append(
"set interfaces {t} {n} duplex {d}".format(
t=_iface_type(name),
n=name,
d=iface_cfg["duplex"],
),
)
if iface_cfg.get("speed"):
client.configure_set(base + ["speed"], iface_cfg["speed"])
commands.append(
"set interfaces {t} {n} speed {s}".format(
t=_iface_type(name),
n=name,
s=iface_cfg["speed"],
),
)
for vif in iface_cfg.get("vifs") or []:
vid = str(vif["vlan_id"])
vif_base = base + ["vif", vid]
client.configure_set(vif_base)
commands.append(
"set interfaces {t} {n} vif {v}".format(
t=_iface_type(name),
n=name,
v=vid,
),
)
if vif.get("description"):
client.configure_set(vif_base + ["description"], vif["description"])
commands.append(
"set interfaces {t} {n} vif {v} description '{d}'".format(
t=_iface_type(name),
n=name,
v=vid,
d=vif["description"],
),
)
if "enabled" in vif and not vif["enabled"]:
client.configure_set(vif_base + ["disable"])
commands.append(
"set interfaces {t} {n} vif {v} disable".format(
t=_iface_type(name),
n=name,
v=vid,
),
)
def main():
argument_spec = dict(
config=dict(
type="list",
elements="dict",
options=dict(
name=dict(type="str", required=True),
description=dict(type="str"),
enabled=dict(type="bool", default=True),
mtu=dict(type="int"),
duplex=dict(type="str", choices=["auto", "full", "half"]),
speed=dict(type="str"),
vifs=dict(
type="list",
elements="dict",
options=dict(
vlan_id=dict(type="int", required=True),
description=dict(type="str"),
enabled=dict(type="bool", default=True),
),
),
),
),
state=dict(
type="str",
default="merged",
choices=["merged", "replaced", "overridden", "deleted", "gathered"],
),
)
argument_spec.update(VYOS_REST_CONNECTION_ARGSPEC)
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
)
client = VyOSRestClient(module)
state = module.params["state"]
config = module.params.get("config") or []
commands = []
changed = False
before = _get_interfaces(client)
if state == "gathered":
module.exit_json(changed=False, gathered=before, before=before, commands=[])
if module.check_mode:
module.exit_json(changed=True, before=before, commands=["(check mode)"])
try:
if state == "deleted":
targets = {i["name"] for i in config} if config else {i["name"] for i in before}
for iface in before:
if iface["name"] in targets:
base = _iface_base_path(iface["name"])
# Only delete mutable attributes, not the interface itself
for attr in ["description", "mtu", "duplex", "speed"]:
if attr in iface:
try:
client.configure_delete(base + [attr])
commands.append(
"delete interfaces {t} {n} {a}".format(
t=_iface_type(iface["name"]),
n=iface["name"],
a=attr,
),
)
except VyOSRestError:
pass
changed = True
elif state in ("merged", "replaced", "overridden"):
for iface_cfg in config:
_apply_interface(client, iface_cfg, commands)
changed = True
except VyOSRestError as exc:
module.fail_json(msg=str(exc))
after = _get_interfaces(client) if changed else before
module.exit_json(changed=changed, before=before, after=after, commands=commands)
if __name__ == "__main__":
main()
|