2016-02-02 03:56:35 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: eos_template
|
|
|
|
version_added: "2.1"
|
|
|
|
author: "Peter sprygada (@privateip)"
|
|
|
|
short_description: Manage Arista EOS device configurations
|
|
|
|
description:
|
|
|
|
- Manages network device configurations over SSH or eAPI. This module
|
2016-08-02 22:46:08 +02:00
|
|
|
allows implementers to work with the device running-config. It
|
2016-02-02 03:56:35 +01:00
|
|
|
provides a way to push a set of commands onto a network device
|
2016-08-02 22:46:08 +02:00
|
|
|
by evaluating the current running-config and only pushing configuration
|
2016-02-02 03:56:35 +01:00
|
|
|
commands that are not already configured. The config source can
|
|
|
|
be a set of commands or a template.
|
|
|
|
extends_documentation_fragment: eos
|
|
|
|
options:
|
|
|
|
src:
|
|
|
|
description:
|
|
|
|
- The path to the config source. The source can be either a
|
|
|
|
file with config or a template that will be merged during
|
|
|
|
runtime. By default the task will search for the source
|
|
|
|
file in role or playbook root folder in templates directory.
|
2016-03-12 04:08:12 +01:00
|
|
|
required: true
|
2016-02-02 03:56:35 +01:00
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- The force argument instructs the module to not consider the
|
|
|
|
current devices running-config. When set to true, this will
|
|
|
|
cause the module to push the contents of I(src) into the device
|
|
|
|
without first checking if already configured.
|
|
|
|
required: false
|
|
|
|
default: false
|
2016-03-12 04:08:12 +01:00
|
|
|
choices: ['yes', 'no']
|
|
|
|
include_defaults:
|
|
|
|
description:
|
|
|
|
- By default when the M(eos_template) connects to the remote
|
2016-08-02 22:46:08 +02:00
|
|
|
device to retrieve the configuration it will issue the C(show
|
|
|
|
running-config) command. If this option is set to True then
|
|
|
|
the issued command will be C(show running-config all).
|
2016-03-12 04:08:12 +01:00
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
choices: ['yes', 'no']
|
2016-02-02 03:56:35 +01:00
|
|
|
backup:
|
|
|
|
description:
|
|
|
|
- When this argument is configured true, the module will backup
|
|
|
|
the running-config from the node prior to making any changes.
|
|
|
|
The backup file will be written to backup_{{ hostname }} in
|
|
|
|
the root of the playbook directory.
|
|
|
|
required: false
|
|
|
|
default: false
|
2016-03-12 04:08:12 +01:00
|
|
|
choices: ['yes', 'no']
|
2016-02-02 03:56:35 +01:00
|
|
|
replace:
|
|
|
|
description:
|
|
|
|
- This argument will cause the provided configuration to be replaced
|
|
|
|
on the destination node. The use of the replace argument will
|
|
|
|
always cause the task to set changed to true and will implies
|
2016-08-02 22:46:08 +02:00
|
|
|
C(force=true). This argument is only valid with C(transport=eapi).
|
2016-02-02 03:56:35 +01:00
|
|
|
required: false
|
|
|
|
default: false
|
2016-03-12 04:08:12 +01:00
|
|
|
choices: ['yes', 'no']
|
2016-02-02 03:56:35 +01:00
|
|
|
config:
|
|
|
|
description:
|
|
|
|
- The module, by default, will connect to the remote device and
|
|
|
|
retrieve the current running-config to use as a base for comparing
|
|
|
|
against the contents of source. There are times when it is not
|
|
|
|
desirable to have the task get the current running-config for
|
|
|
|
every task in a playbook. The I(config) argument allows the
|
2016-08-02 22:46:08 +02:00
|
|
|
implementer to pass in the configuration to use as the base
|
|
|
|
config for comparison.
|
2016-02-02 03:56:35 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2016-08-02 22:46:08 +02:00
|
|
|
- name: Push a configuration onto the device
|
2016-02-02 03:56:35 +01:00
|
|
|
eos_template:
|
|
|
|
src: config.j2
|
|
|
|
|
2016-08-02 22:46:08 +02:00
|
|
|
- name: Forceable push a configuration onto the device
|
2016-02-02 03:56:35 +01:00
|
|
|
eos_template:
|
|
|
|
src: config.j2
|
|
|
|
force: yes
|
|
|
|
|
2016-08-02 22:46:08 +02:00
|
|
|
- name: Provide the base configuration for comparison
|
2016-02-02 03:56:35 +01:00
|
|
|
eos_template:
|
|
|
|
src: candidate_config.txt
|
|
|
|
config: current_config.txt
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
2016-02-09 20:33:47 +01:00
|
|
|
updates:
|
2016-02-02 03:56:35 +01:00
|
|
|
description: The set of commands that will be pushed to the remote device
|
|
|
|
returned: always
|
|
|
|
type: list
|
2016-02-09 20:33:47 +01:00
|
|
|
sample: ['...', '...']
|
2016-02-02 03:56:35 +01:00
|
|
|
|
2016-02-09 20:33:47 +01:00
|
|
|
responses:
|
|
|
|
description: The set of responses from issuing the commands on the device
|
|
|
|
retured: when not check_mode
|
|
|
|
type: list
|
|
|
|
sample: ['...', '...']
|
2016-02-02 03:56:35 +01:00
|
|
|
"""
|
2016-02-11 01:21:24 +01:00
|
|
|
import re
|
|
|
|
|
2016-08-26 22:36:30 +02:00
|
|
|
from ansible.module_utils.netcfg import NetworkConfig, dumps
|
|
|
|
from ansible.module_utils.eos import NetworkModule
|
|
|
|
|
2016-02-02 03:56:35 +01:00
|
|
|
def get_config(module):
|
2016-03-12 04:08:12 +01:00
|
|
|
config = module.params.get('config')
|
2016-09-07 04:51:40 +02:00
|
|
|
defaults = module.params['include_defaults']
|
2016-02-02 03:56:35 +01:00
|
|
|
if not config and not module.params['force']:
|
2016-09-07 04:51:40 +02:00
|
|
|
config = module.config.get_config(include_defaults=defaults)
|
2016-02-02 03:56:35 +01:00
|
|
|
return config
|
|
|
|
|
2016-04-06 04:56:30 +02:00
|
|
|
def filter_exit(commands):
|
|
|
|
# Filter out configuration mode commands followed immediately by an
|
|
|
|
# exit command indented by one level only, e.g.
|
|
|
|
# - route-map map01 permit 10
|
|
|
|
# - exit
|
|
|
|
#
|
|
|
|
# Build a temporary list as we filter, then copy the temp list
|
|
|
|
# back onto the commands list.
|
|
|
|
temp = []
|
|
|
|
ind_prev = 999
|
|
|
|
count = 0
|
|
|
|
for c in commands:
|
|
|
|
ind_this = c.count(' ')
|
|
|
|
if re.search(r"^\s*exit$", c) and ind_this == ind_prev + 1:
|
|
|
|
temp.pop()
|
|
|
|
count -= 1
|
|
|
|
if count != 0:
|
|
|
|
ind_prev = temp[-1].count(' ')
|
|
|
|
continue
|
|
|
|
temp.append(c)
|
|
|
|
ind_prev = ind_this
|
|
|
|
count += 1
|
|
|
|
return temp
|
|
|
|
|
2016-02-02 03:56:35 +01:00
|
|
|
def main():
|
|
|
|
""" main entry point for module execution
|
|
|
|
"""
|
|
|
|
|
|
|
|
argument_spec = dict(
|
2016-03-12 04:08:12 +01:00
|
|
|
src=dict(required=True),
|
2016-02-02 03:56:35 +01:00
|
|
|
force=dict(default=False, type='bool'),
|
|
|
|
include_defaults=dict(default=False, type='bool'),
|
|
|
|
backup=dict(default=False, type='bool'),
|
|
|
|
replace=dict(default=False, type='bool'),
|
|
|
|
config=dict()
|
|
|
|
)
|
|
|
|
|
|
|
|
mutually_exclusive = [('config', 'backup'), ('config', 'force')]
|
|
|
|
|
2016-08-26 22:36:30 +02:00
|
|
|
module = NetworkModule(argument_spec=argument_spec,
|
|
|
|
mutually_exclusive=mutually_exclusive,
|
|
|
|
supports_check_mode=True)
|
2016-02-02 03:56:35 +01:00
|
|
|
|
|
|
|
replace = module.params['replace']
|
|
|
|
|
2016-04-06 04:56:30 +02:00
|
|
|
commands = list()
|
|
|
|
running = None
|
|
|
|
|
2016-02-02 03:56:35 +01:00
|
|
|
result = dict(changed=False)
|
|
|
|
|
2016-04-04 13:19:08 +02:00
|
|
|
candidate = NetworkConfig(contents=module.params['src'], indent=3)
|
2016-02-02 03:56:35 +01:00
|
|
|
|
2016-04-06 04:56:30 +02:00
|
|
|
if replace:
|
|
|
|
if module.params['transport'] == 'cli':
|
|
|
|
module.fail_json(msg='config replace is only supported over eapi')
|
2016-04-04 13:19:08 +02:00
|
|
|
commands = str(candidate).split('\n')
|
2016-04-06 04:56:30 +02:00
|
|
|
else:
|
|
|
|
contents = get_config(module)
|
|
|
|
if contents:
|
|
|
|
running = NetworkConfig(contents=contents, indent=3)
|
|
|
|
result['_backup'] = contents
|
2016-02-02 03:56:35 +01:00
|
|
|
|
2016-04-06 04:56:30 +02:00
|
|
|
if not module.params['force']:
|
|
|
|
commands = candidate.difference((running or list()))
|
2016-08-26 22:36:30 +02:00
|
|
|
commands = dumps(commands, 'commands').split('\n')
|
|
|
|
commands = [str(c) for c in commands if c]
|
2016-04-06 04:56:30 +02:00
|
|
|
else:
|
|
|
|
commands = str(candidate).split('\n')
|
2016-02-11 01:21:24 +01:00
|
|
|
|
2016-06-14 18:09:23 +02:00
|
|
|
commands = filter_exit(commands)
|
2016-02-02 03:56:35 +01:00
|
|
|
if commands:
|
|
|
|
if not module.check_mode:
|
2016-09-07 04:51:40 +02:00
|
|
|
response = module.config.load_config(commands,
|
|
|
|
replace=replace,
|
|
|
|
session='eos_template',
|
|
|
|
commit=True)
|
|
|
|
|
|
|
|
module.cli('no configure session eos_template')
|
2016-02-09 20:33:47 +01:00
|
|
|
result['responses'] = response
|
2016-02-02 03:56:35 +01:00
|
|
|
result['changed'] = True
|
|
|
|
|
2016-02-09 20:33:47 +01:00
|
|
|
result['updates'] = commands
|
2016-04-04 13:19:08 +02:00
|
|
|
module.exit_json(**result)
|
2016-02-02 03:56:35 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|