2017-01-28 15:32:04 +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-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
2017-08-16 04:10:36 +00:00
|
|
|
'supported_by': 'network'}
|
2017-03-14 16:07:22 +00:00
|
|
|
|
2017-02-02 19:45:22 +00:00
|
|
|
|
2017-01-28 15:32:04 +00:00
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
2017-03-05 08:37:01 +00:00
|
|
|
module: eos_banner
|
2017-01-28 15:32:04 +00:00
|
|
|
version_added: "2.3"
|
|
|
|
author: "Peter Sprygada (@privateip)"
|
|
|
|
short_description: Manage multiline banners on Arista EOS devices
|
|
|
|
description:
|
|
|
|
- This will configure both login and motd banners on remote devices
|
|
|
|
running Arista EOS. It allows playbooks to add or remote
|
|
|
|
banner text from the active running configuration.
|
2017-03-05 08:37:01 +00:00
|
|
|
extends_documentation_fragment: eos
|
2017-08-23 15:09:31 +00:00
|
|
|
notes:
|
|
|
|
- Tested against EOS 4.15
|
2017-01-28 15:32:04 +00:00
|
|
|
options:
|
|
|
|
banner:
|
|
|
|
description:
|
2017-03-15 16:00:43 +00:00
|
|
|
- Specifies which banner that should be
|
|
|
|
configured on the remote device.
|
2017-01-28 15:32:04 +00:00
|
|
|
required: true
|
2017-06-17 20:02:48 +00:00
|
|
|
choices: ['login', 'motd']
|
2017-01-28 15:32:04 +00:00
|
|
|
text:
|
|
|
|
description:
|
2017-03-15 16:00:43 +00:00
|
|
|
- The banner text that should be
|
2017-01-28 15:32:04 +00:00
|
|
|
present in the remote device running configuration. This argument
|
2017-03-15 16:00:43 +00:00
|
|
|
accepts a multiline string. Requires I(state=present).
|
2017-01-28 15:32:04 +00:00
|
|
|
state:
|
|
|
|
description:
|
2017-03-15 16:00:43 +00:00
|
|
|
- Specifies whether or not the configuration is
|
|
|
|
present in the current devices active running configuration.
|
2017-01-28 15:32:04 +00:00
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: configure the login banner
|
|
|
|
eos_banner:
|
|
|
|
banner: login
|
|
|
|
text: |
|
|
|
|
this is my login banner
|
|
|
|
that contains a multiline
|
|
|
|
string
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: remove the motd banner
|
2017-04-20 10:12:40 +00:00
|
|
|
eos_banner:
|
|
|
|
banner: motd
|
|
|
|
state: absent
|
2017-01-28 15:32:04 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
commands:
|
|
|
|
description: The list of configuration mode commands to send to the device
|
|
|
|
returned: always
|
|
|
|
type: list
|
|
|
|
sample:
|
|
|
|
- banner login
|
|
|
|
- this is my login banner
|
|
|
|
- that contains a multiline
|
|
|
|
- string
|
|
|
|
- EOF
|
|
|
|
session_name:
|
|
|
|
description: The EOS config session name used to load the configuration
|
2017-03-15 16:00:43 +00:00
|
|
|
returned: if changes
|
2017-01-28 15:32:04 +00:00
|
|
|
type: str
|
|
|
|
sample: ansible_1479315771
|
|
|
|
"""
|
2017-02-14 01:22:10 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.eos.eos import load_config, run_commands
|
|
|
|
from ansible.module_utils.network.eos.eos import eos_argument_spec, check_args
|
2017-09-14 08:26:32 +00:00
|
|
|
from ansible.module_utils.six import string_types
|
2017-07-11 17:28:33 +00:00
|
|
|
from ansible.module_utils._text import to_text
|
2017-01-28 15:32:04 +00:00
|
|
|
|
2017-09-14 08:26:32 +00:00
|
|
|
|
2017-01-28 15:32:04 +00:00
|
|
|
def map_obj_to_commands(updates, module):
|
|
|
|
commands = list()
|
|
|
|
want, have = updates
|
|
|
|
state = module.params['state']
|
|
|
|
|
2017-07-11 17:28:33 +00:00
|
|
|
if state == 'absent' and have.get('text'):
|
2018-05-24 18:25:19 +00:00
|
|
|
if isinstance(have['text'], string_types):
|
2017-07-11 17:28:33 +00:00
|
|
|
commands.append('no banner %s' % module.params['banner'])
|
|
|
|
elif have['text'].get('loginBanner') or have['text'].get('motd'):
|
|
|
|
commands.append({'cmd': 'no banner %s' % module.params['banner']})
|
2017-01-28 15:32:04 +00:00
|
|
|
|
|
|
|
elif state == 'present':
|
2017-09-14 08:26:32 +00:00
|
|
|
if isinstance(have['text'], string_types):
|
2017-07-11 17:28:33 +00:00
|
|
|
if want['text'] != have['text']:
|
2017-03-14 20:00:17 +00:00
|
|
|
commands.append('banner %s' % module.params['banner'])
|
|
|
|
commands.extend(want['text'].strip().split('\n'))
|
|
|
|
commands.append('EOF')
|
2017-07-11 17:28:33 +00:00
|
|
|
else:
|
|
|
|
have_text = have['text'].get('loginBanner') or have['text'].get('motd')
|
|
|
|
if have_text:
|
|
|
|
have_text = have_text.strip()
|
|
|
|
|
|
|
|
if to_text(want['text']) != have_text or not have_text:
|
2017-03-14 20:00:17 +00:00
|
|
|
# For EAPI we need to construct a dict with cmd/input
|
|
|
|
# key/values for the banner
|
|
|
|
commands.append({'cmd': 'banner %s' % module.params['banner'],
|
|
|
|
'input': want['text'].strip('\n')})
|
2017-01-28 15:32:04 +00:00
|
|
|
|
|
|
|
return commands
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2017-01-28 15:32:04 +00:00
|
|
|
def map_config_to_obj(module):
|
|
|
|
output = run_commands(module, ['show banner %s' % module.params['banner']])
|
|
|
|
obj = {'banner': module.params['banner'], 'state': 'absent'}
|
|
|
|
if output:
|
2017-12-05 14:57:40 +00:00
|
|
|
if module.params['transport'] == 'eapi':
|
2017-03-14 20:00:17 +00:00
|
|
|
# On EAPI we need to extract the banner text from dict key
|
|
|
|
# 'loginBanner'
|
2017-04-07 13:32:10 +00:00
|
|
|
if module.params['banner'] == 'login':
|
|
|
|
banner_response_key = 'loginBanner'
|
|
|
|
else:
|
|
|
|
banner_response_key = 'motd'
|
|
|
|
if isinstance(output[0], dict) and banner_response_key in output[0].keys():
|
2017-09-14 08:26:32 +00:00
|
|
|
obj['text'] = output[0]
|
2017-12-05 14:57:40 +00:00
|
|
|
else:
|
|
|
|
obj['text'] = output[0]
|
2017-01-28 15:32:04 +00:00
|
|
|
obj['state'] = 'present'
|
|
|
|
return obj
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2017-01-28 15:32:04 +00:00
|
|
|
def map_params_to_obj(module):
|
|
|
|
text = module.params['text']
|
|
|
|
if text:
|
2018-05-24 18:25:19 +00:00
|
|
|
text = to_text(text).strip()
|
2017-01-28 15:32:04 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'banner': module.params['banner'],
|
|
|
|
'text': text,
|
|
|
|
'state': module.params['state']
|
|
|
|
}
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2017-01-28 15:32:04 +00:00
|
|
|
def main():
|
|
|
|
""" main entry point for module execution
|
|
|
|
"""
|
|
|
|
argument_spec = dict(
|
|
|
|
banner=dict(required=True, choices=['login', 'motd']),
|
|
|
|
text=dict(),
|
|
|
|
state=dict(default='present', choices=['present', 'absent'])
|
|
|
|
)
|
|
|
|
|
2017-02-28 01:22:42 +00:00
|
|
|
argument_spec.update(eos_argument_spec)
|
|
|
|
|
2017-01-28 15:32:04 +00:00
|
|
|
required_if = [('state', 'present', ('text',))]
|
|
|
|
|
2017-02-14 01:22:10 +00:00
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
required_if=required_if,
|
|
|
|
supports_check_mode=True)
|
2017-01-28 15:32:04 +00:00
|
|
|
|
2017-02-28 01:22:42 +00:00
|
|
|
warnings = list()
|
|
|
|
check_args(module, warnings)
|
|
|
|
|
2017-01-28 15:32:04 +00:00
|
|
|
result = {'changed': False}
|
2017-02-28 01:22:42 +00:00
|
|
|
if warnings:
|
|
|
|
result['warnings'] = warnings
|
2017-01-28 15:32:04 +00:00
|
|
|
want = map_params_to_obj(module)
|
|
|
|
have = map_config_to_obj(module)
|
|
|
|
|
|
|
|
commands = map_obj_to_commands((want, have), module)
|
|
|
|
result['commands'] = commands
|
|
|
|
|
|
|
|
if commands:
|
|
|
|
commit = not module.check_mode
|
|
|
|
response = load_config(module, commands, commit=commit)
|
|
|
|
if response.get('diff') and module._diff:
|
|
|
|
result['diff'] = {'prepared': response.get('diff')}
|
|
|
|
result['session_name'] = response.get('session')
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2018-07-29 11:46:06 +00:00
|
|
|
|
2017-01-28 15:32:04 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|