Skip to content

Commit 089418f

Browse files
committed
added vyos_config confirm options
1 parent a4fc3de commit 089418f

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

plugins/cliconf/vyos.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def get_config(self, flags=None, format=None):
118118
return out
119119

120120
def edit_config(
121-
self, candidate=None, commit=True, replace=None, comment=None
121+
self, candidate=None, commit=True, replace=None, comment=None, confirm=None
122122
):
123123
resp = {}
124124
operations = self.get_device_operations()
@@ -142,7 +142,7 @@ def edit_config(
142142
if diff_config:
143143
if commit:
144144
try:
145-
self.commit(comment)
145+
self.commit(comment, confirm)
146146
except AnsibleConnectionFailure as e:
147147
msg = "commit failed: %s" % e.message
148148
self.discard_changes()
@@ -194,12 +194,18 @@ def get(
194194
check_all=check_all,
195195
)
196196

197-
def commit(self, comment=None):
198-
if comment:
199-
command = 'commit comment "{0}"'.format(comment)
197+
def commit(self, comment=None, confirm=None):
198+
if confirm:
199+
if comment:
200+
command = 'commit-confirm {0} comment {1}'.format(confirm, comment)
201+
else:
202+
command = 'commit-confirm {0}'.format(confirm)
200203
else:
201-
command = "commit"
202-
self.send_command(command)
204+
if comment:
205+
command = 'commit {0}'.format(comment)
206+
else:
207+
command = "commit"
208+
self.send_command(command, "Proceed?", "\n")
203209

204210
def discard_changes(self):
205211
self.send_command("exit discard")

plugins/module_utils/network/vyos/vyos.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ def run_commands(module, commands, check_rc=True):
117117
return response
118118

119119

120-
def load_config(module, commands, commit=False, comment=None):
120+
def load_config(module, commands, commit=False, comment=None, confirm=None):
121121
connection = get_connection(module)
122122

123123
try:
124124
response = connection.edit_config(
125-
candidate=commands, commit=commit, comment=comment
125+
candidate=commands, commit=commit, comment=comment, confirm=confirm
126126
)
127127
except ConnectionError as exc:
128128
module.fail_json(msg=to_text(exc, errors="surrogate_then_replace"))

plugins/modules/vyos_config.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@
8383
is ignored.
8484
default: configured by vyos_config
8585
type: str
86+
confirm:
87+
description:
88+
- The C(confirm) argument will tell vyos to revert to the previous configuration
89+
if not explicitly confirmed after applying the new config. When set to C(automatic)
90+
this module will automatically confirm the configuration, if the current session
91+
remains working with the new config. When set to C(manual), this module does
92+
not issue the confirmation itself.
93+
type: str
94+
default: none
95+
choices:
96+
- automatic
97+
- manual
98+
- none
99+
confirm_timeout:
100+
description:
101+
- Minutes to wait for confirmation before reverting the configuration. Does
102+
not apply when C(confirm) is set to C(none) .
103+
type: int
104+
default: 10
86105
config:
87106
description:
88107
- The C(config) argument specifies the base configuration to use to compare against
@@ -141,6 +160,11 @@
141160
vyos.vyos.vyos_config:
142161
src: vyos_template.j2
143162
163+
- name: revert after one minute, if connection is lost
164+
vyos.vyos.vyos_config:
165+
src: vyos_template.j2
166+
confirm: yes
167+
144168
- name: for idempotency, use full-form commands
145169
vyos.vyos.vyos_config:
146170
lines:
@@ -310,10 +334,15 @@ def run(module, result):
310334

311335
commit = not module.check_mode
312336
comment = module.params["comment"]
337+
confirm = None
338+
if module.params["confirm"] == "automatic" or module.params["confirm"] == "manual":
339+
confirm = module.params["confirm_timeout"]
313340

314341
diff = None
315342
if commands:
316-
diff = load_config(module, commands, commit=commit, comment=comment)
343+
diff = load_config(module, commands, commit=commit, comment=comment, confirm=confirm)
344+
if module.params["confirm"] == "automatic":
345+
run_commands(module, ["configure", "confirm", "exit"])
317346

318347
if result.get("filtered"):
319348
result["warnings"].append(
@@ -334,6 +363,8 @@ def main():
334363
lines=dict(type="list", elements="str"),
335364
match=dict(default="line", choices=["line", "none"]),
336365
comment=dict(default=DEFAULT_COMMENT),
366+
confirm=dict(choices=["automatic", "manual", "none"], default='none'),
367+
confirm_timeout=dict(type="int", default=10),
337368
config=dict(),
338369
backup=dict(type="bool", default=False),
339370
backup_options=dict(type="dict", options=backup_spec),

0 commit comments

Comments
 (0)