Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] added commit-confirm options to vyos_config #229

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions plugins/cliconf/vyos.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get_config(self, flags=None, format=None):
return out

def edit_config(
self, candidate=None, commit=True, replace=None, comment=None
self, candidate=None, commit=True, replace=None, comment=None, confirm=None
):
resp = {}
operations = self.get_device_operations()
Expand All @@ -142,7 +142,7 @@ def edit_config(
if diff_config:
if commit:
try:
self.commit(comment)
self.commit(comment, confirm)
except AnsibleConnectionFailure as e:
msg = "commit failed: %s" % e.message
self.discard_changes()
Expand Down Expand Up @@ -194,12 +194,18 @@ def get(
check_all=check_all,
)

def commit(self, comment=None):
if comment:
command = 'commit comment "{0}"'.format(comment)
def commit(self, comment=None, confirm=None):
if confirm:
if comment:
command = 'commit-confirm {0} comment {1}'.format(confirm, comment)
else:
command = 'commit-confirm {0}'.format(confirm)
else:
command = "commit"
self.send_command(command)
if comment:
command = 'commit {0}'.format(comment)
else:
command = "commit"
self.send_command(command, "Proceed?", "\n")

def discard_changes(self):
self.send_command("exit discard")
Expand Down
4 changes: 2 additions & 2 deletions plugins/module_utils/network/vyos/vyos.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ def run_commands(module, commands, check_rc=True):
return response


def load_config(module, commands, commit=False, comment=None):
def load_config(module, commands, commit=False, comment=None, confirm=None):
connection = get_connection(module)

try:
response = connection.edit_config(
candidate=commands, commit=commit, comment=comment
candidate=commands, commit=commit, comment=comment, confirm=confirm
)
except ConnectionError as exc:
module.fail_json(msg=to_text(exc, errors="surrogate_then_replace"))
Expand Down
33 changes: 32 additions & 1 deletion plugins/modules/vyos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@
is ignored.
default: configured by vyos_config
type: str
confirm:
description:
- The C(confirm) argument will tell vyos to revert to the previous configuration
if not explicitly confirmed after applying the new config. When set to C(automatic)
this module will automatically confirm the configuration, if the current session
remains working with the new config. When set to C(manual), this module does
not issue the confirmation itself.
type: str
default: none
choices:
- automatic
- manual
- none
confirm_timeout:
description:
- Minutes to wait for confirmation before reverting the configuration. Does
not apply when C(confirm) is set to C(none) .
type: int
default: 10
config:
description:
- The C(config) argument specifies the base configuration to use to compare against
Expand Down Expand Up @@ -141,6 +160,11 @@
vyos.vyos.vyos_config:
src: vyos_template.j2

- name: revert after ten minutes, if connection is lost
vyos.vyos.vyos_config:
src: vyos_template.j2
confirm: yes

- name: for idempotency, use full-form commands
vyos.vyos.vyos_config:
lines:
Expand Down Expand Up @@ -310,10 +334,15 @@ def run(module, result):

commit = not module.check_mode
comment = module.params["comment"]
confirm = None
if module.params["confirm"] == "automatic" or module.params["confirm"] == "manual":
confirm = module.params["confirm_timeout"]

diff = None
if commands:
diff = load_config(module, commands, commit=commit, comment=comment)
diff = load_config(module, commands, commit=commit, comment=comment, confirm=confirm)
if module.params["confirm"] == "automatic":
run_commands(module, ["configure", "confirm", "exit"])

if result.get("filtered"):
result["warnings"].append(
Expand All @@ -334,6 +363,8 @@ def main():
lines=dict(type="list", elements="str"),
match=dict(default="line", choices=["line", "none"]),
comment=dict(default=DEFAULT_COMMENT),
confirm=dict(choices=["automatic", "manual", "none"], default='none'),
confirm_timeout=dict(type="int", default=10),
config=dict(),
backup=dict(type="bool", default=False),
backup_options=dict(type="dict", options=backup_spec),
Expand Down