Working ansible plugin for EOS and JUNOS

This commit is contained in:
David Barroso committed 2015-03-19 15:42:32 +01:00
1 parent 2d34412d77
commit d64e448017
1 file changed
+24 -31
+24 -31
View File
@@ -26,17 +26,16 @@ logger = logging.getLogger('eos_install_config')
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: eos_install_config module: napalm_install_config
author: David Barroso <dbarroso@spotify.net> author: David Barroso <dbarroso@spotify.net>
version_added: "1.0.0" version_added: "1.0.0"
short_description: Replaces the configuration taken from a file on a device running EOS. short_description: Replaces the configuration taken from a file on a device running EOS.
description: description:
This library will take the configuration from a file and load it into a device running EOS. The old configuration This library will take the configuration from a file and load it into a device running any OS supported by napalm.
will be replaced using the feature 'config replace terminal: force'. The use this module you need to enable access The old configuration will be replaced by the new one.
to the device via the eAPI.
requirements: requirements:
- pyEOS - napalm
options: options:
hostname: hostname:
@@ -51,9 +50,6 @@ options:
password: password:
description: Password description: Password
required: true required: true
use_ssl:
description: If set to True we will connect via https, if False we will use http instead. Default is True.
required: false
config_file: config_file:
description: Where to load the configuration from. description: Where to load the configuration from.
required: true required: true
@@ -71,19 +67,17 @@ EXAMPLES = '''
In our playbook would have something like: In our playbook would have something like:
tasks: tasks:
- name: Assembling configuration - name: Assemble configuration
assemble: src=compiled/{{ inventory_hostname }} dest=compiled/{{ inventory_hostname }}/running.conf assemble: src=../compiled/{{ inventory_hostname }}/ dest=../compiled/{{ inventory_hostname }}/running.conf
- name: Push Configuration
- name: Pushing configuration to EOS napalm_install_config:
eos_install_config:
hostname={{ inventory_hostname }} hostname={{ inventory_hostname }}
username=admin username=dbarroso
dev_os={{dev_os}} dev_os={{os}}
password=p4ssw0rd password=p4ssw0rd
use_ssl=False config_file=../compiled/{{ inventory_hostname }}/running.conf
config_file=~/spotify/network-ansible/compiled/{{ inventory_hostname }}/running.conf
commit_changes={{commit_changes}} commit_changes={{commit_changes}}
diff_file=logs/{{ inventory_hostname }}.log diff_file=../compiled/{{ inventory_hostname }}/diff
From the CLI we would trigger the playbook like: From the CLI we would trigger the playbook like:
@@ -108,8 +102,8 @@ def main():
hostname=dict(required=True), hostname=dict(required=True),
username=dict(required=True), username=dict(required=True),
password=dict(required=True), password=dict(required=True),
use_ssl=dict(required=False, default=True),
config_file=dict(required=True), config_file=dict(required=True),
dev_os=dict(required=True),
commit_changes=dict(required=True), commit_changes=dict(required=True),
diff_file=dict(required=False, default=None), diff_file=dict(required=False, default=None),
), ),
@@ -120,7 +114,6 @@ def main():
username = module.params['username'] username = module.params['username']
dev_os = module.params['dev_os'] dev_os = module.params['dev_os']
password = module.params['password'] password = module.params['password']
use_ssl = module.params['use_ssl']
config_file = module.params['config_file'] config_file = module.params['config_file']
commit_changes = module.params['commit_changes'] commit_changes = module.params['commit_changes']
@@ -128,19 +121,18 @@ def main():
if commit_changes.__class__ is str: if commit_changes.__class__ is str:
commit_changes = ast.literal_eval(commit_changes) commit_changes = ast.literal_eval(commit_changes)
if use_ssl.__class__ is str:
use_ssl = ast.literal_eval(use_ssl)
if dev_os == 'EOS': if dev_os.upper() == 'EOS':
from drivers.eos import EOSDriver from napalm.eos import EOSDriver as NetworkDriver
device = EOSDriver(hostname, username, password, use_ssl) elif dev_os.upper() == 'JUNOS':
from napalm.junos import JunOSDriver as NetworkDriver
else: else:
raise Exception('Network OS not supported: %s' % dev_os) raise Exception('Network OS not supported: %s' % dev_os)
device = NetworkDriver(hostname, username, password)
device.open() device.open()
device.load_candidate_config(filename=config_file) device.load_replace_candidate(filename=config_file)
#content of the
diff = device.compare_config() diff = device.compare_config()
changed = len(diff) > 0 changed = len(diff) > 0
@@ -148,13 +140,14 @@ def main():
save_to_file(diff, diff_file) save_to_file(diff, diff_file)
if module.check_mode or not commit_changes: if module.check_mode or not commit_changes:
module.exit_json(changed=False, msg=diff) device.discard_config()
module.exit_json(changed=changed, msg=diff)
else: else:
if len(diff) > 0: if changed:
device.replace_config() device.commit_config()
module.exit_json(changed=changed, msg=diff) module.exit_json(changed=changed, msg=diff)
logger.info('DEVICE=%s CHANGED=%s STATUS=%s' % (hostname, len(changed.splitlines())), 'OK') logger.info('DEVICE=%s CHANGED=%s STATUS=%s' % (hostname, changed, 'OK'))
device.close() device.close()