2016-02-03 02:42:10 +00: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/>.
|
|
|
|
#
|
2017-01-07 01:22:17 +00:00
|
|
|
ANSIBLE_METADATA = {
|
|
|
|
'status': ['deprecated'],
|
|
|
|
'supported_by': 'community',
|
|
|
|
'version': '1.0'
|
|
|
|
}
|
2016-12-06 10:35:05 +00:00
|
|
|
|
2016-02-03 02:42:10 +00:00
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
2016-03-10 20:54:41 +00:00
|
|
|
module: ios_template
|
2016-02-03 02:42:10 +00:00
|
|
|
version_added: "2.1"
|
2016-07-11 14:25:04 +00:00
|
|
|
author: "Peter Sprygada (@privateip)"
|
2016-02-03 02:42:10 +00:00
|
|
|
short_description: Manage Cisco IOS device configurations over SSH
|
|
|
|
description:
|
2016-04-19 21:46:34 +00:00
|
|
|
- Manages Cisco IOS network device configurations over SSH. This module
|
2016-07-06 11:27:25 +00:00
|
|
|
allows implementers to work with the device running-config. It
|
2016-02-03 02:42:10 +00:00
|
|
|
provides a way to push a set of commands onto a network device
|
2016-07-06 11:27:25 +00:00
|
|
|
by evaluating the current running-config and only pushing configuration
|
2016-02-03 02:42:10 +00:00
|
|
|
commands that are not already configured. The config source can
|
|
|
|
be a set of commands or a template.
|
2017-01-06 16:21:39 +00:00
|
|
|
deprecated: Deprecated in 2.2. Use M(ios_config) instead.
|
2017-01-07 01:22:17 +00:00
|
|
|
notes:
|
|
|
|
- Provider arguments are no longer supported. Network tasks should now
|
|
|
|
specify connection plugin network_cli instead.
|
2016-02-03 02:42:10 +00:00
|
|
|
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 first search for the source
|
|
|
|
file in role or playbook root folder in templates unless a full
|
|
|
|
path to the file is given.
|
2016-04-19 21:46:34 +00:00
|
|
|
required: true
|
2016-02-03 02:42:10 +00:00
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- The force argument instructs the module not to consider the
|
|
|
|
current device 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-04-02 11:13:46 +00:00
|
|
|
choices: [ "true", "false" ]
|
2016-02-03 02:42:10 +00:00
|
|
|
include_defaults:
|
|
|
|
description:
|
|
|
|
- The module, by default, will collect the current device
|
2016-07-06 11:27:25 +00:00
|
|
|
running-config to use as a base for comparison to the commands
|
2016-02-03 02:42:10 +00:00
|
|
|
in I(src). Setting this value to true will cause the command
|
|
|
|
issued to add any necessary flags to collect all defaults as
|
|
|
|
well as the device configuration. If the destination device
|
|
|
|
does not support such a flag, this argument is silently ignored.
|
2016-08-05 23:26:09 +00:00
|
|
|
required: true
|
2016-04-02 11:13:46 +00:00
|
|
|
choices: [ "true", "false" ]
|
2016-02-03 02:42:10 +00: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-04-02 11:13:46 +00:00
|
|
|
choices: [ "true", "false" ]
|
2016-02-03 02:42:10 +00: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. The I(config) argument allows the implementer to
|
2016-07-06 11:27:25 +00:00
|
|
|
pass in the configuration to use as the base config for
|
|
|
|
comparison.
|
2016-02-03 02:42:10 +00:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: push a configuration onto the device
|
2016-03-10 20:54:41 +00:00
|
|
|
ios_template:
|
2016-02-03 02:42:10 +00:00
|
|
|
src: config.j2
|
|
|
|
|
|
|
|
- name: forceable push a configuration onto the device
|
2016-03-10 20:54:41 +00:00
|
|
|
ios_template:
|
2016-02-03 02:42:10 +00:00
|
|
|
src: config.j2
|
|
|
|
force: yes
|
|
|
|
|
2016-10-13 14:47:50 +00:00
|
|
|
- name: provide the base configuration for comparison
|
2016-03-10 20:54:41 +00:00
|
|
|
ios_template:
|
2016-02-03 02:42:10 +00:00
|
|
|
src: candidate_config.txt
|
|
|
|
config: current_config.txt
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
2016-02-09 19:32:01 +00:00
|
|
|
updates:
|
2016-02-03 02:42:10 +00:00
|
|
|
description: The set of commands that will be pushed to the remote device
|
|
|
|
returned: always
|
|
|
|
type: list
|
2016-02-09 19:32:01 +00:00
|
|
|
sample: ['...', '...']
|
2017-01-07 01:22:17 +00:00
|
|
|
start:
|
|
|
|
description: The time the job started
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "2016-11-16 10:38:15.126146"
|
|
|
|
end:
|
|
|
|
description: The time the job ended
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "2016-11-16 10:38:25.595612"
|
|
|
|
delta:
|
|
|
|
description: The time elapsed to perform all operations
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "0:00:10.469466"
|
2016-02-03 02:42:10 +00:00
|
|
|
"""
|
2017-01-07 01:22:17 +00:00
|
|
|
from ansible.module_utils.local import LocalAnsibleModule
|
2016-07-11 14:25:04 +00:00
|
|
|
from ansible.module_utils.netcfg import NetworkConfig, dumps
|
2017-01-07 01:22:17 +00:00
|
|
|
from ansible.module_utils.ios import get_config, load_config
|
|
|
|
from ansible.module_utils.network import NET_TRANSPORT_ARGS, _transitional_argument_spec
|
|
|
|
|
|
|
|
|
|
|
|
def check_args(module):
|
|
|
|
warnings = list()
|
|
|
|
for key in NET_TRANSPORT_ARGS:
|
|
|
|
if module.params[key]:
|
|
|
|
warnings.append(
|
|
|
|
'network provider arguments are no longer supported. Please '
|
|
|
|
'use connection: network_cli for the task'
|
|
|
|
)
|
|
|
|
break
|
|
|
|
return warnings
|
|
|
|
|
|
|
|
def get_current_config(module):
|
|
|
|
if module.params['config']:
|
|
|
|
return module.params['config']
|
|
|
|
if module.params['include_defaults']:
|
|
|
|
flags = ['all']
|
|
|
|
else:
|
|
|
|
flags = []
|
2017-01-20 20:17:35 +00:00
|
|
|
return get_config(module=module, flags=flags)
|
2016-02-03 02:42:10 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
""" main entry point for module execution
|
|
|
|
"""
|
|
|
|
argument_spec = dict(
|
|
|
|
src=dict(),
|
|
|
|
force=dict(default=False, type='bool'),
|
|
|
|
include_defaults=dict(default=True, type='bool'),
|
|
|
|
backup=dict(default=False, type='bool'),
|
|
|
|
config=dict(),
|
|
|
|
)
|
|
|
|
|
2017-01-07 01:22:17 +00:00
|
|
|
# Removed the use of provider arguments in 2.3 due to network_cli
|
|
|
|
# connection plugin. To be removed in 2.5
|
|
|
|
argument_spec.update(_transitional_argument_spec())
|
|
|
|
|
2016-02-03 02:42:10 +00:00
|
|
|
mutually_exclusive = [('config', 'backup'), ('config', 'force')]
|
|
|
|
|
2017-01-07 01:22:17 +00:00
|
|
|
module = LocalAnsibleModule(argument_spec=argument_spec,
|
|
|
|
mutually_exclusive=mutually_exclusive,
|
|
|
|
supports_check_mode=True)
|
|
|
|
|
|
|
|
warnings = check_args(module)
|
2016-02-03 02:42:10 +00:00
|
|
|
|
2017-01-07 01:22:17 +00:00
|
|
|
result = dict(changed=False, warnings=warnings)
|
2016-02-03 02:42:10 +00:00
|
|
|
|
2016-04-04 11:00:55 +00:00
|
|
|
candidate = NetworkConfig(contents=module.params['src'], indent=1)
|
2016-02-03 02:42:10 +00:00
|
|
|
|
2017-01-07 01:22:17 +00:00
|
|
|
result = {'changed': False}
|
|
|
|
|
|
|
|
if module.params['backup']:
|
2017-01-20 20:17:35 +00:00
|
|
|
result['__backup__'] = get_config(module=module)
|
2016-02-03 02:42:10 +00:00
|
|
|
|
2016-04-04 11:00:55 +00:00
|
|
|
if not module.params['force']:
|
2017-01-07 01:22:17 +00:00
|
|
|
contents = get_current_config(module)
|
|
|
|
configobj = NetworkConfig(contents=contents, indent=1)
|
|
|
|
commands = candidate.difference(configobj)
|
2016-08-26 20:05:46 +00:00
|
|
|
commands = dumps(commands, 'commands').split('\n')
|
2017-01-07 01:22:17 +00:00
|
|
|
commands = [str(c).strip() for c in commands if c]
|
2016-04-04 11:00:55 +00:00
|
|
|
else:
|
2017-01-07 01:22:17 +00:00
|
|
|
commands = [c.strip() for c in str(candidate).split('\n')]
|
2016-02-03 02:42:10 +00:00
|
|
|
|
|
|
|
if commands:
|
|
|
|
if not module.check_mode:
|
2017-01-20 20:17:35 +00:00
|
|
|
load_config(module, commands)
|
2016-02-03 02:42:10 +00:00
|
|
|
result['changed'] = True
|
|
|
|
|
2016-02-09 19:32:01 +00:00
|
|
|
result['updates'] = commands
|
2016-02-03 02:42:10 +00:00
|
|
|
|
2017-01-07 01:22:17 +00:00
|
|
|
module.exit_json(**result)
|
2016-02-03 02:42:10 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|