Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:napalm-automation/napalm-ansible…
Browse files Browse the repository at this point in the history
… into develop
  • Loading branch information
dbarrosop committed Aug 3, 2017
2 parents 4830cb0 + df95e28 commit e18d641
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 86 deletions.
10 changes: 3 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ env:
- ANSIBLE_VERSION=2.3

install:
- pip install pylama
- pip install napalm-base
- pip install "ansible>=$ANSIBLE_VERSION.0,<$ANSIBLE_VERSION.99"

script:
- pylama
- cd tests
- ansible-playbook -i napalm_install_config/hosts -l "*.dry_run.*" napalm_install_config/config.yaml -C
- ansible-playbook -i napalm_install_config/hosts -l "*.commit.*" napalm_install_config/config.yaml
- ansible-playbook -i napalm_install_config/hosts -l "*.error*" napalm_install_config/config_error.yaml
- ansible-playbook -i napalm_get_facts/hosts napalm_get_facts/get_facts_ok.yaml -l multiple_facts.ok
- ansible-playbook -i napalm_get_facts/hosts napalm_get_facts/get_facts_not_implemented.yaml -l multiple_facts.not_implemented -e "ignore_notimplemented=true"
- ansible-playbook -i napalm_get_facts/hosts napalm_get_facts/get_facts_not_implemented.yaml -l multiple_facts.not_implemented -e "ignore_notimplemented=false"
- ansible-playbook -i napalm_get_facts/hosts napalm_get_facts/get_facts_error.yaml -l multiple_facts.error
- ./run_tests.sh
- ./test_changelog.sh

deploy:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.7.0
=====

- Minor internal improvements
- [#77] Added ``napalm_cli`` module
- Prepend 'napalm_' to NAPALM ansible facts generated by napalm_get_facts module
- Make NAPALM 'get_facts' be directly accessible Ansible facts (e.g. napalm_model)
Expand Down
39 changes: 23 additions & 16 deletions napalm_ansible/napalm_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from ansible.module_utils.basic import AnsibleModule, return_values


DOCUMENTATION = '''
---
module: napalm_cli
Expand All @@ -9,9 +12,9 @@
requirements:
- napalm
options:
cli_args:
args:
description:
- Command to run on the CLI
- Keyword arguments to pass to the `cli` method
required: True
'''

Expand All @@ -24,9 +27,10 @@
dev_os: "eos"
- napalm_cli:
provider: "{{ napalm_provider }}"
cli_args:
- show version
- show snmp chassis
args:
commands:
- show version
- show snmp chassis
'''

RETURN = '''
Expand All @@ -42,7 +46,7 @@
sample:
{
"show snmp chassis": "Chassis: 1234\n",
"show version": "Arista vEOS\nHardware version: \nSerial number: \nSystem MAC address: 0800.27c3.5f28\n\nSoftware image version: 4.17.5M\nArchitecture: i386\nInternal build version: 4.17.5M-4414219.4175M\nInternal build ID: d02143c6-e42b-4fc3-99b6-97063bddb6b8\n\nUptime: 1 hour and 21 minutes\nTotal memory: 1893416 kB\nFree memory: 956488 kB\n\n"
"show version": "Arista vEOS\nHardware version: \nSerial number: \nSystem MAC address: 0800.27c3.5f28\n\nSoftware image version: 4.17.5M\nArchitecture: i386\nInternal build version: 4.17.5M-4414219.4175M\nInternal build ID: d02143c6-e42b-4fc3-99b6-97063bddb6b8\n\nUptime: 1 hour and 21 minutes\nTotal memory: 1893416 kB\nFree memory: 956488 kB\n\n" # noqa
}
'''

Expand All @@ -53,8 +57,10 @@
else:
napalm_found = True


def main():
os_choices = ['eos', 'junos', 'ios', 'vyos', 'ros']
os_choices = ['eos', 'junos', 'iosxr', 'fortios',
'ios', 'mock', 'nxos', 'panos', 'vyos', 'ros']
module = AnsibleModule(
argument_spec=dict(
hostname=dict(type='str', required=False, aliases=['host']),
Expand All @@ -64,9 +70,9 @@ def main():
timeout=dict(type='int', required=False, default=60),
dev_os=dict(type='str', required=False, choices=os_choices),
optional_args=dict(required=False, type='dict', default=None),
cli_args=dict(required=True, type='list', default=None),
args=dict(required=True, type='dict', default=None),
),
supports_check_mode=True
supports_check_mode=False
)

if not napalm_found:
Expand All @@ -87,17 +93,18 @@ def main():
provider['hostname'] = provider.get('hostname', None) or provider.get('host', None)
# allow local params to override provider
for param, pvalue in provider.items():
if module.params.get(param) != False:
if module.params.get(param) is not False:
module.params[param] = module.params.get(param) or pvalue

hostname = module.params['hostname']
username = module.params['username']
dev_os = module.params['dev_os']
password = module.params['password']
timeout = module.params['timeout']
cli_args = module.params['cli_args']
args = module.params['args']

argument_check = { 'hostname': hostname, 'username': username, 'dev_os': dev_os, 'password': password }
argument_check = {'hostname': hostname, 'username': username,
'dev_os': dev_os, 'password': password}
for key, val in argument_check.items():
if val is None:
module.fail_json(msg=str(key) + " is required")
Expand All @@ -122,7 +129,10 @@ def main():
except Exception, e:
module.fail_json(msg="cannot connect to device: " + str(e))

cli_response = device.cli(cli_args)
try:
cli_response = device.cli(**args)
except Exception as e:
module.fail_json(msg="{}".format(e))

try:
device.close()
Expand All @@ -131,9 +141,6 @@ def main():

module.exit_json(changed=False, results=cli_response)

# standard ansible module imports
from ansible.module_utils.basic import *

if __name__ == '__main__':
main()

25 changes: 12 additions & 13 deletions napalm_ansible/napalm_get_facts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
(c) 2016 Elisa Jasinska <[email protected]>
Expand All @@ -19,6 +16,8 @@
You should have received a copy of the GNU General Public License
along with Ansible. If not, see <http://www.gnu.org/licenses/>.
"""
from ansible.module_utils.basic import AnsibleModule, return_values


DOCUMENTATION = '''
---
Expand Down Expand Up @@ -47,7 +46,7 @@
description:
- OS of the device
required: False
choices: ['eos', 'junos', 'iosxr', 'fortios', 'ibm', 'ios', 'mock', 'nxos', 'panos', 'vyos']
choices: ['eos', 'junos', 'iosxr', 'fortios', 'ios', 'mock', 'nxos', 'panos', 'vyos']
provider:
description:
- Dictionary which acts as a collection of arguments used to define the characteristics
Expand Down Expand Up @@ -76,13 +75,15 @@
filter:
description:
- A list of facts to retreive from a device and provided though C(ansible_facts)
The list of facts available are maintained at: http://napalm.readthedocs.io/en/latest/support/
The list of facts available are maintained at:
http://napalm.readthedocs.io/en/latest/support/
Note- not all getters are implemented on all supported device types
required: False
default: ['facts']
args:
description:
- dictionary of kwargs arguments to pass to the filter. The outer key is the name of the getter (same as the filter)
- dictionary of kwargs arguments to pass to the filter. The outer key is the name of
the getter (same as the filter)
requited: False
default: None
'''
Expand Down Expand Up @@ -152,7 +153,7 @@


def main():
os_choices = ['eos', 'junos', 'iosxr', 'fortios', 'ibm', 'ios', 'mock', 'nxos', 'panos', 'vyos', 'ros']
os_choices = ['eos', 'junos', 'iosxr', 'fortios', 'ios', 'mock', 'nxos', 'panos', 'vyos', 'ros']
module = AnsibleModule(
argument_spec=dict(
hostname=dict(type='str', required=False, aliases=['host']),
Expand Down Expand Up @@ -188,7 +189,7 @@ def main():
provider['hostname'] = provider.get('hostname', None) or provider.get('host', None)
# allow local params to override provider
for param, pvalue in provider.items():
if module.params.get(param) != False:
if module.params.get(param) is not False:
module.params[param] = module.params.get(param) or pvalue

hostname = module.params['hostname']
Expand All @@ -201,8 +202,8 @@ def main():
ignore_notimplemented = module.params['ignore_notimplemented']
implementation_errors = []


argument_check = { 'hostname': hostname, 'username': username, 'dev_os': dev_os, 'password': password }
argument_check = {'hostname': hostname, 'username': username,
'dev_os': dev_os, 'password': password}
for key, val in argument_check.items():
if val is None:
module.fail_json(msg=str(key) + " is required")
Expand Down Expand Up @@ -231,7 +232,7 @@ def main():
# retreive data from device
facts = {}

NAPALM_GETTERS=[getter for getter in dir(network_driver) if getter.startswith("get_")]
NAPALM_GETTERS = [getter for getter in dir(network_driver) if getter.startswith("get_")]

for getter in filter_list:
getter_function = "get_{}".format(getter)
Expand Down Expand Up @@ -275,8 +276,6 @@ def main():

module.exit_json(**results)

# standard ansible module imports
from ansible.module_utils.basic import *

if __name__ == '__main__':
main()
45 changes: 23 additions & 22 deletions napalm_ansible/napalm_install_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
(c) 2016 Elisa Jasinska <[email protected]>
Original prototype by David Barroso <[email protected]>
Expand All @@ -20,6 +17,8 @@
You should have received a copy of the GNU General Public License
along with Ansible. If not, see <http://www.gnu.org/licenses/>.
"""
from ansible.module_utils.basic import AnsibleModule, return_values


DOCUMENTATION = '''
---
Expand All @@ -28,8 +27,8 @@
version_added: "2.1"
short_description: "Installs 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 or merged with the new one."
- "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 or merged with the new one."
requirements:
- napalm
options:
Expand Down Expand Up @@ -57,7 +56,7 @@
description:
- OS of the device
required: False
choices: ['eos', 'junos', 'iosxr', 'fortios', 'ibm', 'ios', 'mock', 'nxos', 'panos', 'vyos']
choices: ['eos', 'junos', 'iosxr', 'fortios', 'ios', 'mock', 'nxos', 'panos', 'vyos']
timeout:
description:
- Time in seconds to wait for the device to respond
Expand All @@ -78,35 +77,36 @@
required: False
commit_changes:
description:
- If set to True the configuration will be actually merged or replaced. If the set to False,
we will not apply the changes, just check and report the diff
- If set to True the configuration will be actually merged or replaced. If the set to
False, we will not apply the changes, just check and report the diff
choices: [true,false]
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
- 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
choices: [true,false]
default: False
required: False
diff_file:
description:
- A path to the file where we store the "diff" between the running configuration and the new
configuration. If not set the diff between configurations will not be saved.
- A path to the file where we store the "diff" between the running configuration and the
new configuration. If not set the diff between configurations will not be saved.
default: None
required: False
get_diffs:
description:
- Set to False to not have any diffs generated. Useful if platform does not support commands
being used to generate diffs. Note- By default diffs are generated even if the diff_file
param is not set.
- Set to False to not have any diffs generated. Useful if platform does not support
commands being used to generate diffs. Note- By default diffs are generated even
if the diff_file param is not set.
choices: [true,false]
default: True
required: False
archive_file:
description:
- File to store backup of running-configuration from device. Configuration will not be retrieved
if not set.
- File to store backup of running-configuration from device. Configuration will not be
retrieved if not set.
default: None
required: False
'''
Expand Down Expand Up @@ -166,15 +166,17 @@
else:
napalm_found = True


def save_to_file(content, filename):
f = open(filename, 'w')
try:
f.write(content)
finally:
f.close()


def main():
os_choices = ['eos', 'junos', 'iosxr', 'fortios', 'ibm', 'ios', 'mock', 'nxos', 'panos', 'vyos', 'ros']
os_choices = ['eos', 'junos', 'iosxr', 'fortios', 'ios', 'mock', 'nxos', 'panos', 'vyos', 'ros']
module = AnsibleModule(
argument_spec=dict(
hostname=dict(type='str', required=False, aliases=['host']),
Expand Down Expand Up @@ -213,7 +215,7 @@ def main():
provider['hostname'] = provider.get('hostname', None) or provider.get('host', None)
# allow local params to override provider
for param, pvalue in provider.items():
if module.params.get(param) != False:
if module.params.get(param) is not False:
module.params[param] = module.params.get(param) or pvalue

hostname = module.params['hostname']
Expand All @@ -229,7 +231,8 @@ def main():
get_diffs = module.params['get_diffs']
archive_file = module.params['archive_file']

argument_check = { 'hostname': hostname, 'username': username, 'dev_os': dev_os, 'password': password }
argument_check = {'hostname': hostname, 'username': username,
'dev_os': dev_os, 'password': password}
for key, val in argument_check.items():
if val is None:
module.fail_json(msg=str(key) + " is required")
Expand Down Expand Up @@ -304,8 +307,6 @@ def main():

module.exit_json(changed=changed, msg=diff)

# standard ansible module imports
from ansible.module_utils.basic import *

if __name__ == '__main__':
main()
9 changes: 3 additions & 6 deletions napalm_ansible/napalm_parse_yang.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
(c) 2017 David Barroso <[email protected]>
Expand All @@ -20,7 +17,7 @@
along with Ansible. If not, see <http://www.gnu.org/licenses/>.
"""
# standard ansible module imports
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import AnsibleModule, return_values

import json

Expand Down Expand Up @@ -66,7 +63,7 @@
description:
- OS of the device
required: False
choices: ['eos', 'junos', 'iosxr', 'fortios', 'ibm', 'ios', 'mock',
choices: ['eos', 'junos', 'iosxr', 'fortios', 'ios', 'mock',
'nxos', 'panos', 'vyos']
provider:
description:
Expand Down Expand Up @@ -254,7 +251,7 @@ def parse_from_device(module, os_choices):


def main():
os_choices = ['eos', 'junos', 'iosxr', 'fortios', 'ibm', 'ios',
os_choices = ['eos', 'junos', 'iosxr', 'fortios', 'ios',
'mock', 'nxos', 'panos', 'vyos']
module = AnsibleModule(
argument_spec=dict(
Expand Down
Loading

0 comments on commit e18d641

Please sign in to comment.