mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
5.8 KiB
Python
175 lines
5.8 KiB
Python
9 years ago
|
#!/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 = """
|
||
|
---
|
||
9 years ago
|
module: ios_template
|
||
9 years ago
|
version_added: "2.1"
|
||
8 years ago
|
author: "Peter Sprygada (@privateip)"
|
||
9 years ago
|
short_description: Manage Cisco IOS device configurations over SSH
|
||
|
description:
|
||
9 years ago
|
- Manages Cisco IOS network device configurations over SSH. This module
|
||
8 years ago
|
allows implementers to work with the device running-config. It
|
||
9 years ago
|
provides a way to push a set of commands onto a network device
|
||
8 years ago
|
by evaluating the current running-config and only pushing configuration
|
||
9 years ago
|
commands that are not already configured. The config source can
|
||
|
be a set of commands or a template.
|
||
8 years ago
|
deprecated: Deprecated in 2.2. Use eos_config instead
|
||
9 years ago
|
extends_documentation_fragment: ios
|
||
|
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.
|
||
9 years ago
|
required: true
|
||
9 years ago
|
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
|
||
9 years ago
|
choices: [ "true", "false" ]
|
||
9 years ago
|
include_defaults:
|
||
|
description:
|
||
|
- The module, by default, will collect the current device
|
||
8 years ago
|
running-config to use as a base for comparison to the commands
|
||
9 years ago
|
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.
|
||
8 years ago
|
required: true
|
||
9 years ago
|
choices: [ "true", "false" ]
|
||
9 years ago
|
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
|
||
9 years ago
|
choices: [ "true", "false" ]
|
||
9 years ago
|
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
|
||
8 years ago
|
pass in the configuration to use as the base config for
|
||
|
comparison.
|
||
9 years ago
|
required: false
|
||
|
default: null
|
||
|
"""
|
||
|
|
||
|
EXAMPLES = """
|
||
|
- name: push a configuration onto the device
|
||
9 years ago
|
ios_template:
|
||
9 years ago
|
host: hostname
|
||
|
username: foo
|
||
9 years ago
|
src: config.j2
|
||
|
|
||
|
- name: forceable push a configuration onto the device
|
||
9 years ago
|
ios_template:
|
||
9 years ago
|
host: hostname
|
||
|
username: foo
|
||
9 years ago
|
src: config.j2
|
||
|
force: yes
|
||
|
|
||
|
- name: provide the base configuration for comparision
|
||
9 years ago
|
ios_template:
|
||
9 years ago
|
host: hostname
|
||
|
username: foo
|
||
9 years ago
|
src: candidate_config.txt
|
||
|
config: current_config.txt
|
||
|
"""
|
||
|
|
||
|
RETURN = """
|
||
9 years ago
|
updates:
|
||
9 years ago
|
description: The set of commands that will be pushed to the remote device
|
||
|
returned: always
|
||
|
type: list
|
||
9 years ago
|
sample: ['...', '...']
|
||
9 years ago
|
|
||
9 years ago
|
responses:
|
||
|
description: The set of responses from issuing the commands on the device
|
||
8 years ago
|
returned: when not check_mode
|
||
9 years ago
|
type: list
|
||
|
sample: ['...', '...']
|
||
9 years ago
|
"""
|
||
8 years ago
|
import ansible.module_utils.ios
|
||
8 years ago
|
from ansible.module_utils.netcfg import NetworkConfig, dumps
|
||
8 years ago
|
from ansible.module_utils.ios import NetworkModule
|
||
9 years ago
|
|
||
|
def get_config(module):
|
||
|
config = module.params['config'] or dict()
|
||
8 years ago
|
defaults = module.params['include_defaults']
|
||
9 years ago
|
if not config and not module.params['force']:
|
||
8 years ago
|
config = module.config.get_config(include_defaults=defaults)
|
||
9 years ago
|
return config
|
||
|
|
||
|
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(),
|
||
|
)
|
||
|
|
||
|
mutually_exclusive = [('config', 'backup'), ('config', 'force')]
|
||
|
|
||
8 years ago
|
module = NetworkModule(argument_spec=argument_spec,
|
||
|
mutually_exclusive=mutually_exclusive,
|
||
|
supports_check_mode=True)
|
||
9 years ago
|
|
||
|
result = dict(changed=False)
|
||
|
|
||
9 years ago
|
candidate = NetworkConfig(contents=module.params['src'], indent=1)
|
||
9 years ago
|
|
||
|
contents = get_config(module)
|
||
9 years ago
|
if contents:
|
||
|
config = NetworkConfig(contents=contents, indent=1)
|
||
8 years ago
|
result['_backup'] = str(contents)
|
||
9 years ago
|
|
||
9 years ago
|
if not module.params['force']:
|
||
8 years ago
|
commands = candidate.difference(config)
|
||
|
commands = dumps(commands, 'commands').split('\n')
|
||
|
commands = [str(c) for c in commands if c]
|
||
9 years ago
|
else:
|
||
8 years ago
|
commands = str(candidate).split('\n')
|
||
9 years ago
|
|
||
|
if commands:
|
||
|
if not module.check_mode:
|
||
8 years ago
|
response = module.config(commands)
|
||
9 years ago
|
result['responses'] = response
|
||
9 years ago
|
result['changed'] = True
|
||
|
|
||
9 years ago
|
result['updates'] = commands
|
||
9 years ago
|
module.exit_json(**result)
|
||
9 years ago
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|