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
|
#!/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_configure
short_description: Send raw set/delete commands to a VyOS device via REST API.
description:
- Sends one or more set/delete configuration commands to a VyOS device
via the HTTPS REST API as a single atomic batch commit.
- Useful for configuration not covered by dedicated resource modules,
or for test setup and teardown tasks.
- Commands are parsed from CLI-style strings (C(set ...) / C(delete ...)).
version_added: "1.0.0"
author:
- VyOS Community (@vyos)
options:
commands:
description:
- List of CLI-style configuration commands.
- Each command must start with C(set) or C(delete).
- Quote a value that contains spaces, as in the CLI, for example
C(set interfaces ethernet eth0 description "WAN link").
type: list
elements: str
required: true
save:
description:
- Whether to save the configuration after applying commands.
- The task fails if the configuration cannot be saved, even though the commands were applied.
type: bool
default: false
seealso:
- module: vyos.vyos.vyos_config
"""
EXAMPLES = r"""
- name: Add loopback address for testing
vyos.rest.vyos_configure:
commands:
- set interfaces loopback lo address 20.1.1.1/32
save: false
- name: Remove loopback address after testing
vyos.rest.vyos_configure:
commands:
- delete interfaces loopback lo address 20.1.1.1/32
save: false
- name: Multiple commands in one atomic commit
vyos.rest.vyos_configure:
commands:
- set system host-name vyos-test
- set system domain-name example.com
save: true
"""
RETURN = r"""
commands:
description: Parsed command payloads sent to the device.
returned: always
type: list
response:
description: Raw API response.
returned: when commands are applied
type: dict
"""
import shlex
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos import VyOSModule
def _parse_command(line):
"""Parse a CLI-style command string into (op, path) tuple.
Examples:
"set interfaces loopback lo address 20.1.1.1/32"
-> ("set", ["interfaces", "loopback", "lo", "address", "20.1.1.1/32"])
'set interfaces ethernet eth0 description "WAN link"'
-> ("set", ["interfaces", "ethernet", "eth0", "description", "WAN link"])
"delete service snmp"
-> ("delete", ["service", "snmp"])
Returns None when the line is not a set or delete command with a path.
Raises ValueError when a quote is not closed.
"""
tokens = shlex.split(line)
if len(tokens) < 2 or tokens[0] not in ("set", "delete"):
return None
return (tokens[0], tokens[1:])
def main():
module = AnsibleModule(
argument_spec=dict(
commands=dict(type="list", elements="str", required=True),
save=dict(type="bool", default=False),
),
supports_check_mode=True,
)
vyos = VyOSModule(module)
commands_raw = module.params["commands"]
do_save = module.params["save"]
commands = []
for line in commands_raw:
parsed = None
try:
parsed = _parse_command(line)
except ValueError as exc:
module.fail_json(msg="Invalid command '{c}': {e}".format(c=line, e=exc))
if parsed is None:
module.fail_json(
msg="Invalid command '{c}' — must start with 'set' or 'delete'".format(c=line),
)
commands.append(parsed)
if not commands:
module.exit_json(changed=False, commands=[])
if module.check_mode:
module.exit_json(changed=True, commands=commands)
response = vyos.apply_commands(commands)
if do_save and not vyos.save_config():
module.fail_json(
msg="The commands were applied but saving the configuration failed",
changed=True,
commands=commands,
response=response,
)
module.exit_json(
changed=True,
commands=commands,
response=response,
)
if __name__ == "__main__":
main()
|