Skip to content

Commit

Permalink
Napalm ansible 1.1 Release (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers authored May 15, 2020
1 parent fbaa6be commit 845723a
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 349 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ env:
- ANSIBLE_VERSION=2.9

install:
- pip install pylama
- pip install -r requirements.txt
- pip install "ansible>=$ANSIBLE_VERSION.0,<$ANSIBLE_VERSION.99"
- pip install -r requirements-dev.txt
- pip install "ansible>=$ANSIBLE_VERSION.0,<$ANSIBLE_VERSION.99"
- pip install -e .

script:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ develop
- Improving documentation.
- Update module docstring.
- Update napalm_get_facts to allow _get_checkpoint_file for NX-OS
- Update dev tooling to pin dependencies and to install black
- Blacken the code base; exclude from PY27 install

1.0.0
=====
Expand Down
2 changes: 1 addition & 1 deletion napalm_ansible/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

def main():
path = os.path.dirname(__file__)
if LooseVersion(ansible.__version__) < LooseVersion('2.3.0.0'):
if LooseVersion(ansible.__version__) < LooseVersion("2.3.0.0"):
action_plugins = ""
else:
action_plugins = "action_plugins = {path}/plugins/action".format(path=path)
Expand Down
83 changes: 46 additions & 37 deletions napalm_ansible/modules/napalm_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def return_values(obj):
yield str(obj)


DOCUMENTATION = '''
DOCUMENTATION = """
---
module: napalm_cli
author: "Charlie Allom"
Expand Down Expand Up @@ -51,9 +51,9 @@ def return_values(obj):
Note - local param takes precedence, e.g. hostname is preferred to provider['hostname']
required: False
'''
"""

EXAMPLES = '''
EXAMPLES = """
- napalm_cli:
hostname: "{{ inventory_hostname }}"
username: "napalm"
Expand All @@ -70,9 +70,9 @@ def return_values(obj):
commands:
- show version
- show snmp chassis
'''
"""

RETURN = '''
RETURN = """
changed:
description: ALWAYS RETURNS FALSE
returned: always
Expand All @@ -86,12 +86,13 @@ def return_values(obj):
"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" # noqa
}'
'''
"""

napalm_found = False
try:
from napalm import get_network_driver
from napalm.base import ModuleImportError

napalm_found = True
except ImportError:
pass
Expand All @@ -100,67 +101,75 @@ def return_values(obj):
def main():
module = AnsibleModule(
argument_spec=dict(
hostname=dict(type='str', required=False, aliases=['host']),
username=dict(type='str', required=False),
password=dict(type='str', required=False, no_log=True),
provider=dict(type='dict', required=False),
timeout=dict(type='int', required=False, default=60),
dev_os=dict(type='str', required=False),
optional_args=dict(required=False, type='dict', default=None),
args=dict(required=True, type='dict', default=None),
hostname=dict(type="str", required=False, aliases=["host"]),
username=dict(type="str", required=False),
password=dict(type="str", required=False, no_log=True),
provider=dict(type="dict", required=False),
timeout=dict(type="int", required=False, default=60),
dev_os=dict(type="str", required=False),
optional_args=dict(required=False, type="dict", default=None),
args=dict(required=True, type="dict", default=None),
),
supports_check_mode=False
supports_check_mode=False,
)

if not napalm_found:
module.fail_json(msg="the python module napalm is required")

provider = module.params['provider'] or {}
provider = module.params["provider"] or {}

no_log = ['password', 'secret']
no_log = ["password", "secret"]
for param in no_log:
if provider.get(param):
module.no_log_values.update(return_values(provider[param]))
if provider.get('optional_args') and provider['optional_args'].get(param):
module.no_log_values.update(return_values(provider['optional_args'].get(param)))
if module.params.get('optional_args') and module.params['optional_args'].get(param):
module.no_log_values.update(return_values(module.params['optional_args'].get(param)))
if provider.get("optional_args") and provider["optional_args"].get(param):
module.no_log_values.update(
return_values(provider["optional_args"].get(param))
)
if module.params.get("optional_args") and module.params["optional_args"].get(
param
):
module.no_log_values.update(
return_values(module.params["optional_args"].get(param))
)

# allow host or hostname
provider['hostname'] = provider.get('hostname', None) or provider.get('host', None)
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) 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']
args = module.params['args']
hostname = module.params["hostname"]
username = module.params["username"]
dev_os = module.params["dev_os"]
password = module.params["password"]
timeout = module.params["timeout"]
args = module.params["args"]

argument_check = {'hostname': hostname, 'username': username, 'dev_os': dev_os}
argument_check = {"hostname": hostname, "username": username, "dev_os": dev_os}
for key, val in argument_check.items():
if val is None:
module.fail_json(msg=str(key) + " is required")

if module.params['optional_args'] is None:
if module.params["optional_args"] is None:
optional_args = {}
else:
optional_args = module.params['optional_args']
optional_args = module.params["optional_args"]

try:
network_driver = get_network_driver(dev_os)
except ModuleImportError as e:
module.fail_json(msg="Failed to import napalm driver: " + str(e))

try:
device = network_driver(hostname=hostname,
username=username,
password=password,
timeout=timeout,
optional_args=optional_args)
device = network_driver(
hostname=hostname,
username=username,
password=password,
timeout=timeout,
optional_args=optional_args,
)
device.open()
except Exception as e:
module.fail_json(msg="cannot connect to device: " + str(e))
Expand All @@ -178,5 +187,5 @@ def main():
module.exit_json(changed=False, cli_results=cli_response)


if __name__ == '__main__':
if __name__ == "__main__":
main()
20 changes: 10 additions & 10 deletions napalm_ansible/modules/napalm_diff_yang.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
napalm_yang = None


DOCUMENTATION = '''
DOCUMENTATION = """
---
module: napalm_diff_yang
author: "David Barroso (@dbarrosop)"
Expand All @@ -51,18 +51,18 @@
description:
- Dictionary with the data to load into the second YANG object
required: True
'''
"""

EXAMPLES = '''
EXAMPLES = """
- napalm_diff_yang:
first: "{{ candidate.yang_model }}"
second: "{{ running_config.yang_model }}"
models:
- models.openconfig_interfaces
register: diff
'''
"""

RETURN = '''
RETURN = """
diff:
description: "Same output as the method napalm_yang.utils.diff"
returned: always
Expand All @@ -82,7 +82,7 @@
}
}
}'
'''
"""


def get_root_object(models):
Expand All @@ -104,10 +104,10 @@ def main():
module = AnsibleModule(
argument_spec=dict(
models=dict(type="list", required=True),
first=dict(type='dict', required=True),
second=dict(type='dict', required=True),
first=dict(type="dict", required=True),
second=dict(type="dict", required=True),
),
supports_check_mode=True
supports_check_mode=True,
)

if not napalm_yang:
Expand All @@ -124,5 +124,5 @@ def main():
module.exit_json(yang_diff=diff)


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading

0 comments on commit 845723a

Please sign in to comment.