Skip to content

Commit

Permalink
load_config and save_config vyos
Browse files Browse the repository at this point in the history
Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>
  • Loading branch information
trishnaguha committed Sep 4, 2018
1 parent 4fda695 commit d16fee7
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 2 deletions.
14 changes: 13 additions & 1 deletion defaults/main.yml
@@ -1,2 +1,14 @@
---
# defaults file for ansible-network.vyos
# defaults file for ansible-network.vyos
#
vyos_config_file: "{{ config_file | default(None) }}"
vyos_config_text: "{{ config_text | default(None) }}"
vyos_config_commit_comment: "{{ commit_comment | default(None) }}"
vyos_config_replace: "{{ replace | default(False) }}"
vyos_config_working_dir: ~/.ansible/vyos
vyos_config_checkpoint_filename: chk_ansible
vyos_config_rollback_enabled: "{{ rollback | default(True) }}"
vyos_config_ignore_keywords:
- exit
- commit
- discard
21 changes: 21 additions & 0 deletions includes/checkpoint/create.yaml
@@ -0,0 +1,21 @@
# only create the checkpoint configuration file if rollback is currently
# enabled (default)
- name: save current configuration
include_tasks: "{{ role_path }}/includes/checkpoint/save.yaml"

- name: get current files on running://config
cli:
command: "show file running://config/"
register: vyos_dir_listing

- name: delete checkpoint file
vyos_command:
commands:
- command: "delete file running://config/{{ vyos_config_checkpoint_filename }}"
prompt: "Do you want to erase the running://config/{{ vyos_config_checkpoint_filename }} file"
answer: 'y'
when: vyos_config_checkpoint_filename in vyos_dir_listing.stdout

- name: create configuration checkpoint
cli:
command: "copy file running://config/config.boot to running://config/{{ vyos_config_checkpoint_filename }}"
6 changes: 6 additions & 0 deletions includes/checkpoint/remove.yaml
@@ -0,0 +1,6 @@
- name: delete checkpoint file
vyos_command:
commands:
- command: "delete file running://config/{{ vyos_config_checkpoint_filename }}"
prompt: "Do you want to erase the running://config/{{ vyos_config_checkpoint_filename }} file"
answer: 'y'
35 changes: 35 additions & 0 deletions includes/checkpoint/restore.yaml
@@ -0,0 +1,35 @@
# Restore configuration if loading configuration fails
#
- name: get current files on running://config
cli:
command: "show file running://config/"
register: vyos_dir_listing

- name: verify checkpoint file exists
fail:
msg: missing checkpoint file, cannot rollback
when: vyos_config_checkpoint_filename not in vyos_dir_listing.stdout

- block:
- name: enter configuration mode
cli:
command: configure

- name: restore previous configuration
cli:
command: "load /config/{{ vyos_config_checkpoint_filename }}"
register: vyos_config_restore

- name: commit
cli:
command: commit
when: "'Load complete' in vyos_config_restore.stdout"
changed_when: true

- name: display message
debug:
msg: "successfully restored checkpoint configuration"

always:
- name: invoke remove
include_tasks: "{{ role_path }}/includes/checkpoint/remove.yaml"
31 changes: 31 additions & 0 deletions includes/checkpoint/save.yaml
@@ -0,0 +1,31 @@
- block:
- name: enter configuration mode
cli:
command: configure

- name: compare current config with saved config
cli:
command: compare saved
register: vyos_config_compare

- name: save config
cli:
command: save
when: vyos_config_compare.stdout != '[edit]'

- name: display message
debug:
msg: nothing to save
when: vyos_config_compare.stdout == '[edit]'

- name: leave configuration mode
cli:
command: exit
rescue:
- name: leave configuration mode
cli:
command: exit

- name: mark the host as failed
fail:
msg: "error saving configuration onto target device"
71 changes: 71 additions & 0 deletions includes/configure/configure.yaml
@@ -0,0 +1,71 @@
# configure
#
- name: create checkpoint configuration on target device
include_tasks: "{{ role_path }}/includes/checkpoint/create.yaml"
when: vyos_rollback_enabled

- name: load configuration
block:
- name: enter configuration mode
cli:
command: configure

- name: load configuration lines
cli:
command: "{{ line.strip() }}"
loop: "{{ vyos_config_text | to_lines }}"
loop_control:
loop_var: line
when: line not in vyos_config_ignore_keywords

- name: compare
cli:
command: compare
register: vyos_config_diff
changed_when: vyos_config_diff.stdout

- name: display config diff
debug:
msg: "{{ vyos_config_diff.stdout.splitlines() }}"

- name: commit the configuration
cli:
command: commit
when: not ansible_check_mode and vyos_config_commit_comment == "" and vyos_config_diff.stdout is not search("No changes")

- name: commit the configuration with comment
cli:
command: "commit comment {{ vyos_config_commit_comment }}"
when: not ansible_check_mode and vyos_config_commit_comment != "" and vyos_config_diff.stdout is not search("No changes")

- name: discard when ansible_check_mode
cli:
command: discard
when: ansible_check_mode

- name: leave configuration mode
cli:
command: exit

rescue:
- name: discard configuration
cli:
command: discard

- name: leave configuration mode
cli:
command: exit

- name: rollback initiated
block:
- name: display message
debug:
msg: "error loading configuration, restoring checkpoint"

- name: restore previous checkpoint configuration
include_tasks: "{{ role_path }}/includes/checkpoint/restore.yaml"
when: nxos_rollback_enabled

- name: mark the host as failed
fail:
msg: "error loading configuration onto target device"
18 changes: 18 additions & 0 deletions includes/init.yaml
@@ -0,0 +1,18 @@
- name: set the role version
set_fact:
ansible_network_vyos_path: "{{ role_path }}"
ansible_network_vyos_version: "devel"

- name: display the role version to stdout
debug:
msg: "ansible_network.vyos version {{ ansible_network_vyos_version }}"

- name: validate ansible_network_os == 'vyos'
fail:
msg: "expected ansible_network_os to be `vyos`, got `{{ ansible_network_os }}`"
when: ansible_network_os != 'vyos'

- name: validate ansible_connection == 'network_cli'
fail:
msg: "expected ansible_network to be `network_cli`, got `{{ ansible_connection }}`"
when: ansible_connection != 'network_cli'
57 changes: 57 additions & 0 deletions library/vyos_capabilities.py
@@ -0,0 +1,57 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2018, Ansible by Red Hat, inc
# 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


ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'network'}


DOCUMENTATION = """
---
module: vyos_capabilities
version_added: "2.7"
short_description: Collect device capabilities from Cisco NX-OS
description:
- Collect basic fact capabilities from Cisco NX-OS devices and return
the capabilities as Ansible facts.
author:
- Ansible Netowrk Community (ansible-network)
options: {}
"""

EXAMPLES = """
- facts:
"""

RETURN = """
"""
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.connection import Connection


def main():
""" main entry point for Ansible module
"""
argument_spec = {}

module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

connection = Connection(module._socket_path)
facts = connection.get_capabilities()
facts = module.from_json(facts)
result = {
'changed': False,
'ansible_facts': {'vyos': {'capabilities': facts['device_info']}}
}
module.exit_json(**result)


if __name__ == '__main__':
main()
26 changes: 26 additions & 0 deletions meta/load_config_spec.yaml
@@ -0,0 +1,26 @@
argument_spec:

ansible_network_os:
description:
- Set the name of Ansible network os platform. This value should be
set to `vyos` for this provider role.
required: true

vyos_config_text:
description:
- Provide the network device configuration as a single string. The
configuration text will be loaded onto the target network device.
type: str
aliases:
- config_text

vyos_config_file:
description:
- Provide relative or absolute path to the configuration file to be
loaded onto the target network device.
type: str
aliases:
- config_file

required_one_of:
- ['vyos_config_text', 'vyos_config_file']
15 changes: 15 additions & 0 deletions tasks/load_config.yaml
@@ -0,0 +1,15 @@
# load_config task
#
- name: initialize function
include_tasks: includes/init.yaml

- name: validate task arguments
validate_role_spec:
spec: load_config_spec.yaml

- name: lookup config file
set_fact:
vyos_config_text: "{{ lookup('config_template', vyos_config_file) | join('\n') }}"

- name: Include configure task
include_tasks: includes/configure/configure.yaml
20 changes: 19 additions & 1 deletion tasks/main.yml
@@ -1,2 +1,20 @@
---
# tasks file for ansible-network.vyos
# tasks file for ansible-network.vyos
#
- name: initialize function
include_tasks: includes/init.yaml

- name: set role supported functions
set_fact:
vyos_functions:
- load_config
- save_config
- noop

- name: validate the requested function is supported
fail:
msg: "invalid function specified, expected one of {{ vyos_functions }}, got {{ function }}"
when: function | default('noop') not in vyos_functions

- name: include function specific tasks and run
include_tasks: "{{ function | default('noop') }}.yaml"
7 changes: 7 additions & 0 deletions tasks/save_config.yaml
@@ -0,0 +1,7 @@
# save_config task
#
- name: initialize function
include_tasks: includes/init.yaml

- name: Include save_config task
include_tasks: includes/checkpoint/save.yaml

0 comments on commit d16fee7

Please sign in to comment.