Fixing merge conflicts due to optional_args

This commit is contained in:
Kirk Byers committed 2016-01-26 12:25:45 -08:00
commit 8bc869d927
4 files changed
+63 -302

No files matched your search

+1 -1
View File
@@ -1,2 +1,2 @@
David Barroso <dbarroso@spotify.com>
David Barroso <dbarrosop@dravetech.com>
Elisa Jasinska <elisa@bigwaveit.org>
-70
View File
@@ -1,70 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2014 Spotify AB. All rights reserved.
#
# The contents of this file are licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""
"""
import ast
import logging
logger = logging.getLogger('eos_install_config')
DOCUMENTATION = '''
'''
EXAMPLES = '''
'''
def main():
module = AnsibleModule(
argument_spec=dict(
hostname=dict(required=True),
username=dict(required=True),
password=dict(required=True),
dev_os=dict(required=True),
optional_args=dict(required=False, type='dict', default=None)
),
supports_check_mode=True
)
if module.params['optional_args'] is None:
optional_args = {}
else:
optional_args = module.params['optional_args']
hostname = module.params['hostname']
username = module.params['username']
dev_os = module.params['dev_os']
password = module.params['password']
network_driver = get_network_driver(dev_os)
device = network_driver(hostname, username, password, optional_args=optional_args)
device.open()
facts = device.get_facts()
facts['interface_details'] = device.get_interfaces()
device.close()
module.exit_json(ansible_facts=facts)
from ansible.module_utils.basic import *
from napalm import get_network_driver
main()
-191
View File
@@ -1,191 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2015 Spotify AB. All rights reserved.
#
# The contents of this file are licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""
"""
import ast
import logging
logger = logging.getLogger('eos_install_config')
DOCUMENTATION = '''
---
module: napalm_install_config
author: David Barroso <dbarroso@spotify.net>
version_added: "1.0.0"
short_description: Replaces the configuration taken from a file on a device supported by NAPALM.
description:
This library will take the configuration from a file and load it into a device running any OS supported by napalm.
The old configuration will be replaced by the new one.
requirements:
- napalm
options:
hostname:
description: IP or FQDN of the device you want to connect to
required: true
username:
description: Username
required: true
dev_os:
description: OS running the device
required: true
password:
description: Password
required: true
timeout:
description: Timeout for connections and requests to device
required: false
default: 60
config_file:
description: Where to load the configuration from.
required: true
commit_changes:
description: If set to True the configuration will be actually replaced. If the set to False, we will not
apply the changes, just check the differences.
required: true
replace_config:
description: If set to True the entire configuration on the device will be replaced during the commit. If
set to False, we will merge the new config with the existing one. Default: False.
required: False
diff_file:
description: A file where to store the "diff" between the running configuration and the new configuration. If
it's not set the diff between configurations is not saved.
required: False
get_diffs:
description:
- Set to false to not have any diffs generated and always apply config. Useful if platform does
not support commands being used to generated diffs. Note: By default diffs are generated
even if the diff_file param is not set.
choices: ['true', 'false']
required: False
'''
EXAMPLES = '''
In our playbook would have something like:
tasks:
- name: Assemble configuration
assemble: src=../compiled/{{ inventory_hostname }}/ dest=../compiled/{{ inventory_hostname }}/running.conf
- name: Push Configuration
napalm_install_config:
hostname={{ inventory_hostname }}
username=dbarroso
dev_os={{os}}
password=p4ssw0rd
config_file=../compiled/{{ inventory_hostname }}/running.conf
commit_changes={{ commit_changes }}
replace_config={{ replace_config }}
diff_file=../compiled/{{ inventory_hostname }}/diff
timeout={{timeout}}
From the CLI we would trigger the playbook like:
# We don't commit changes, we only want to check the diff
$ ansible-playbook -v -e commit_changes=0 napalm_test.yml
or
# We actually commit the changes
$ ansible-playbook -v -e commit_changes=1 napalm_test.yml
'''
def save_to_file(content, filename):
with open(filename, 'w') as f:
f.write(content)
def main():
module = AnsibleModule(
argument_spec=dict(
hostname=dict(required=True),
username=dict(required=True),
password=dict(required=True),
timeout=dict(required=False, default=60, type='int'),
config_file=dict(required=True),
dev_os=dict(required=True),
commit_changes=dict(required=True),
replace_config=dict(required=True),
diff_file=dict(required=False, default=None),
get_diffs=dict(required=False, choices=BOOLEANS, type='bool', default=True),
optional_args=dict(required=False, type='dict', default=None)
),
supports_check_mode=True
)
if module.params['optional_args'] is None:
optional_args = {}
else:
optional_args = module.params['optional_args']
hostname = module.params['hostname']
username = module.params['username']
dev_os = module.params['dev_os']
password = module.params['password']
timeout = module.params['timeout']
config_file = module.params['config_file']
commit_changes = module.params['commit_changes']
replace_config = module.params['replace_config']
diff_file = module.params['diff_file']
get_diffs = module.params['get_diffs']
if commit_changes.__class__ is str:
commit_changes = ast.literal_eval(commit_changes)
if replace_config.__class__ is str:
replace_config = ast.literal_eval(replace_config)
network_driver = get_network_driver(dev_os)
device = network_driver(hostname, username, password, timeout, optional_args=optional_args)
device.open()
if replace_config:
device.load_replace_candidate(filename=config_file)
else:
device.load_merge_candidate(filename=config_file)
if get_diffs:
diff = device.compare_config().encode('utf-8')
changed = len(diff) > 0
else:
changed = True
diff = None
if diff_file is not None and get_diffs:
save_to_file(diff, diff_file)
if module.check_mode or not commit_changes:
device.discard_config()
module.exit_json(changed=changed, msg=diff)
else:
if changed:
device.commit_config()
module.exit_json(changed=changed, msg=diff)
logger.info('DEVICE=%s CHANGED=%s STATUS=%s' % (hostname, changed, 'OK'))
device.close()
from ansible.module_utils.basic import *
from napalm import get_network_driver
main()
+62 -40
View File
@@ -19,28 +19,18 @@ from exceptions import ReplaceConfigException, MergeConfigException
from napalm.utils.string_parsers import colon_separated_string_to_dict, convert_uptime_string_seconds
def execute_get(device, cmd, separator=':', auto=False):
output = device.execute_command(cmd)
if auto:
if ':' in output[0]:
separator = ':'
elif '\t' in output[0]:
separator = '\t'
else:
raise Exception('Unknown separator for block:\n{}'.format(output))
return colon_separated_string_to_dict('\n'.join(output), separator)
class FortiOSDriver(NetworkDriver):
def __init__(self, hostname, username, password, timeout=60, optional_args=None):
if optional_args is None:
optional_args = {}
self.hostname = hostname
self.username = username
self.password = password
self.device = FortiOS(hostname, username=username, password=password, timeout=timeout)
if optional_args is not None:
self.vdom = optional_args.pop('fortios_vdom', None)
else:
self.vdom = None
self.device = FortiOS(hostname, username=username, password=password, timeout=timeout, vdom=self.vdom)
self.config_replace = False
def open(self):
@@ -49,6 +39,40 @@ class FortiOSDriver(NetworkDriver):
def close(self):
self.device.close()
def execute_command_with_vdom(self, command, vdom=None):
# If the user doesn't specify a particular vdom we use the default vdom for the object.
vdom = vdom or self.vdom
if vdom == 'global' and self.vdom is not None:
# If vdom is global we go to the global vdom, execute the commands and then back to the root.
# There is a catch, if the device doesn't have vdoms enabled we have to execute the command in the root
command = 'conf global\n{command}\nend'.format(command=command)
# We skip the lines telling us that we changed vdom
return self.device.execute_command(command)[1:-2]
elif vdom not in ['global', None]:
# If we have a vdom we change to the vdom, execute the commands and then exit back to the root
command = 'conf vdom\nedit {vdom}\n{command}\nend'.format(vdom=vdom, command=command)
# We skip the lines telling us that we changed vdom
return self.device.execute_command(command)[3:-2]
else:
# If there is no vdom we just execute the command
return self.device.execute_command(command)
def get_command_with_vdom(self, cmd, separator=':', auto=False, vdom=None):
output = self.execute_command_with_vdom(cmd, vdom)
if auto:
if ':' in output[0]:
separator = ':'
elif '\t' in output[0]:
separator = '\t'
else:
raise Exception('Unknown separator for block:\n{}'.format(output))
return colon_separated_string_to_dict('\n'.join(output), separator)
def _load_config(self, filename, config):
if filename is None:
configuration = config
@@ -87,7 +111,7 @@ class FortiOSDriver(NetworkDriver):
def commit_config(self):
try:
self.device.execute_command('execute backup config flash commit_with_napalm')
self.execute_command_with_vdom('execute backup config flash commit_with_napalm')
self.device.commit()
self.discard_config()
except FailedCommit as e:
@@ -101,9 +125,10 @@ class FortiOSDriver(NetworkDriver):
self.device.load_config(in_candidate=True)
def rollback(self):
output = self.device.execute_command('fnsysctl ls -l data2/config')
output = self.execute_command_with_vdom('fnsysctl ls -l data2/config', vdom=None)
rollback_file = output[-2].split()[-1]
rollback_config = self.device.execute_command('fnsysctl cat data2/config/%s' % rollback_file)
rollback_config = self.execute_command_with_vdom(
'fnsysctl cat data2/config/{rollback_file}'.format(rollback_file))
self.device.load_config(empty_candidate=True)
self.load_replace_candidate(config=rollback_config)
@@ -114,13 +139,13 @@ class FortiOSDriver(NetworkDriver):
self.device.commit()
def get_facts(self):
system_status = execute_get(self.device, 'get system status')
performance_status = execute_get(self.device, 'get system performance status')
system_status = self.get_command_with_vdom('get system status', vdom='global')
performance_status = self.get_command_with_vdom('get system performance status', vdom='global')
interfaces = execute_get(self.device, 'get system interface | grep ==')
interface_list = [x.split()[2] for x in interfaces.keys()]
interfaces = self.execute_command_with_vdom('get system interface | grep ==', vdom='global')
interface_list = [x.split()[2] for x in interfaces if x.strip() is not '']
domain = execute_get(self.device, 'get system dns | grep domain')['domain']
domain = self.get_command_with_vdom('get system dns | grep domain', vdom='global')['domain']
return {
'vendor': unicode('Fortigate'),
@@ -155,17 +180,13 @@ class FortiOSDriver(NetworkDriver):
}
def get_interfaces(self):
cmd_prefix = ''
try:
cmd_data = self.device.execute_command('diagnose hardware deviceinfo nic')
except CommandExecutionException:
cmd_data = self.device.execute_command('conf global\n diagnose hardware deviceinfo nic ')
cmd_prefix = 'conf global\n'
cmd_data = self.execute_command_with_vdom('diagnose hardware deviceinfo nic', vdom='global')
interface_list = [x.replace('\t', '') for x in cmd_data if x.startswith('\t')]
interface_statistics = {}
for interface in interface_list:
if_data = self.device.execute_command(cmd_prefix + 'diagnose hardware deviceinfo nic {}'.format(interface))
if_data = self.execute_command_with_vdom(
'diagnose hardware deviceinfo nic {}'.format(interface), vdom='global')
parsed_data = {}
if interface.startswith('mgmt'):
for line in if_data:
@@ -211,7 +232,8 @@ class FortiOSDriver(NetworkDriver):
command_received = 'get router info bgp neighbors {} received-routes | grep prefixes '
peers = dict()
bgp_sum = self.device.execute_command(command_sum)
bgp_sum = self.execute_command_with_vdom(command_sum)
re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
neighbors = {n.split()[0]: n.split()[1:] for n in bgp_sum if re.match(re_neigh, n)}
@@ -234,7 +256,7 @@ class FortiOSDriver(NetworkDriver):
neighbor_dict['address_family']['ipv4'] = dict()
neighbor_dict['address_family']['ipv6'] = dict()
detail_output = [x.lower() for x in self.device.execute_command(command_detail.format(neighbor))]
detail_output = [x.lower() for x in self.execute_command_with_vdom(command_detail.format(neighbor))]
m = re.search('remote router id (.+?)\n', '\n'.join(detail_output))
if m:
neighbor_dict['remote_id'] = unicode(m.group(1))
@@ -251,7 +273,7 @@ class FortiOSDriver(NetworkDriver):
t = [int(s) for s in text.split() if s.isdigit()][0]
neighbor_dict['address_family'][family][term] = t
received = self.device.execute_command(
received = self.execute_command_with_vdom(
command_received.format(neighbor))[0].split()
if len(received) > 0:
neighbor_dict['address_family'][family]['received_prefixes'] = received[-1]
@@ -268,7 +290,7 @@ class FortiOSDriver(NetworkDriver):
}
def get_interfaces_counters(self):
cmd = self.device.execute_command('fnsysctl ifconfig')
cmd = self.execute_command_with_vdom('fnsysctl ifconfig', vdom=None)
if_name = None
interface_counters = dict()
for line in cmd:
@@ -364,7 +386,7 @@ class FortiOSDriver(NetworkDriver):
out = dict()
sensors_block = [parse_string(x) for x in self.device.execute_command('execute sensor detail') if x]
sensors_block = [parse_string(x) for x in self.execute_command_with_vdom('execute sensor detail', vdom='global') if x]
# temp
temp_lines = [x for x in sensors_block if any([True for y in ['dts', 'temp', 'adt7490'] if y in x])]
@@ -375,11 +397,11 @@ class FortiOSDriver(NetworkDriver):
# cpu
out['cpu'] = get_cpu(
[x for x in self.device.execute_command('get system performance status | grep CPU')[1:] if x])
[x for x in self.execute_command_with_vdom('get system performance status | grep CPU', vdom='global')[1:] if x])
# memory
memory_command = 'diag hard sys mem | grep Mem:'
t = [x for x in re.split('\s+', self.device.execute_command(memory_command)[0]) if x]
t = [x for x in re.split('\s+', self.execute_command_with_vdom(memory_command, vdom='global')[0]) if x]
out['memory'] = get_memory(t)
# power, not implemented