diff --git a/LICENSE b/LICENSE index bef6deb..bd64b90 100644 --- a/LICENSE +++ b/LICENSE @@ -4,10 +4,10 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 +http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and -limitations under the License. \ No newline at end of file +limitations under the License. diff --git a/contrib/inventory/conf.sample.ini b/contrib/inventory/conf.sample.ini index 5f11c8b..9671518 100644 --- a/contrib/inventory/conf.sample.ini +++ b/contrib/inventory/conf.sample.ini @@ -10,4 +10,4 @@ D42_SKIP_SSL_CHECK = True GROUP_BY_QUERY = select name, service_level from view_device_v1 GROUP_BY_FIELD = service_level GROUP_BY_REFERENCE_FIELD = name -SPLIT_GROUP_BY_COMMA = False +SPLIT_GROUP_BY_COMMA = False \ No newline at end of file diff --git a/contrib/inventory/d42_ansible_dynamic_inventory.py b/contrib/inventory/d42_ansible_dynamic_inventory.py index 7b80928..ae90ff1 100755 --- a/contrib/inventory/d42_ansible_dynamic_inventory.py +++ b/contrib/inventory/d42_ansible_dynamic_inventory.py @@ -1,16 +1,15 @@ -#!/usr/bin/env python - +from __future__ import (absolute_import, division, print_function) import argparse -import sys -from lib import * - -from contrib.inventory.lib import get_conf +from contrib.inventory.lib import get_conf, Ansible, Device42 try: import json except ImportError: import simplejson as json +__metaclass__ = type + + class Inventory(object): def __init__(self): @@ -39,4 +38,5 @@ def inventory(self): def empty_inventory(self): return {'_meta': {'hostvars': {}}} + Inventory() diff --git a/contrib/inventory/d42_ansible_inventory_hostfile.py b/contrib/inventory/d42_ansible_inventory_hostfile.py index da68892..c61e015 100644 --- a/contrib/inventory/d42_ansible_inventory_hostfile.py +++ b/contrib/inventory/d42_ansible_inventory_hostfile.py @@ -1,9 +1,8 @@ +from __future__ import (absolute_import, division, print_function) import sys -import json -import requests -from lib import * +from contrib.inventory.lib import get_conf, Ansible, Device42 -from contrib.inventory.lib import get_conf +__metaclass__ = type if __name__ == '__main__': conf = get_conf() diff --git a/contrib/inventory/lib.py b/contrib/inventory/lib.py index f274000..e213741 100644 --- a/contrib/inventory/lib.py +++ b/contrib/inventory/lib.py @@ -1,18 +1,19 @@ +from __future__ import (absolute_import, division, print_function) from io import StringIO import requests -import codecs import base64 import configparser import csv import sys import os - try: import json except ImportError: import simplejson as json +__metaclass__ = type + def get_conf(): @@ -77,6 +78,7 @@ def get_conf(): } return conf + class Device42: def __init__(self, conf): @@ -151,5 +153,6 @@ def write_inventory_file(groups): return True + if __name__ == "__main__": - print(get_conf()) \ No newline at end of file + print(get_conf()) diff --git a/galaxy.yml b/galaxy.yml index 58643ac..03d93b5 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,6 +1,6 @@ namespace: "device42" name: "d42" -version: "1.0.1" +version: "1.0.2" readme: "README.md" authors: - "Will Tome (@willtome)" @@ -12,4 +12,4 @@ tags: - device42 - integration - collection -repository: "https://github.com/device42/ansible_device42" +repository: "https://github.com/device42/ansible_device42" \ No newline at end of file diff --git a/plugins/inventory/d42.py b/plugins/inventory/d42.py index 9c47d95..1f95e1c 100644 --- a/plugins/inventory/d42.py +++ b/plugins/inventory/d42.py @@ -1,7 +1,6 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -from __future__ import absolute_import, division, print_function +from __future__ import (absolute_import, division, print_function) +from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable, to_safe_group_name +import requests __metaclass__ = type @@ -61,24 +60,22 @@ separator: '' ''' -from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable, to_safe_group_name -import requests - class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): NAME = 'device42.d42.d42' def verify_file(self, path): - valid = False - if super(InventoryModule, self).verify_file(path): - if path.endswith(('d42.yaml', 'd42.yml')): - valid = True - else: - self.display.vvv('Skipping due to inventory source not ending in "d42.yaml" nor "d42.yml"') - return valid + valid = False + + if super(InventoryModule, self).verify_file(path): + if path.endswith(('d42.yaml', 'd42.yml')): + valid = True + else: + self.display.vvv('Skipping due to inventory source not ending in "d42.yaml" nor "d42.yml"') + return valid - def parse(self, inventory, loader, path, cache=False): + def parse(self, inventory, loader, path, cache=False): super(InventoryModule, self).parse(inventory, loader, path) self._read_config_data(path) @@ -92,7 +89,8 @@ def parse(self, inventory, loader, path, cache=False): try: objects = [] - response = requests.get(base_url + "/api/1.0/devices/all", auth=(username, password), verify=ssl_check, timeout=10) + response = requests.get(base_url + "/api/1.0/devices/all", auth=(username, password), verify=ssl_check, + timeout=10) print('response code: ' + str(response.status_code)) json_response = response.json() @@ -105,7 +103,7 @@ def parse(self, inventory, loader, path, cache=False): for k in object_.keys(): self.inventory.set_variable(host_name, 'd42_' + k, object_[k]) - if object_['ip_addresses'] != []: + if object_['ip_addresses']: self.inventory.set_variable(host_name, 'ansible_host', object_['ip_addresses'][0]['ip']) self._set_composite_vars( @@ -117,6 +115,3 @@ def parse(self, inventory, loader, path, cache=False): self._add_host_to_keyed_groups(self.get_option('keyed_groups'), dict(), host_name, strict) except Exception as e: print(e) - - - diff --git a/plugins/lookup/d42.py b/plugins/lookup/d42.py index a7a267e..f3e1e4e 100644 --- a/plugins/lookup/d42.py +++ b/plugins/lookup/d42.py @@ -1,6 +1,14 @@ +from __future__ import (absolute_import, division, print_function) from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase -import json, requests, sys, csv, io, os +import json +import requests +import sys +import csv +import io +import os +from ansible.utils.display import Display +display = Display() if 'D42_SKIP_SSL_CHECK' in os.environ and os.environ['D42_SKIP_SSL_CHECK'] == 'True': requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) @@ -17,11 +25,8 @@ print('Please set D42_PWD environ.') sys.exit() -try: - from __main__ import display -except ImportError: - from ansible.utils.display import Display - display = Display() + +__metaclass__ = type class LookupModule(LookupBase): @@ -30,12 +35,13 @@ class LookupModule(LookupBase): def get_list_from_csv(text): f = io.StringIO(text) output_list = [] - dict_reader = csv.DictReader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True, dialect='excel') + dict_reader = csv.DictReader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True, + dialect='excel') for item in dict_reader: output_list.append(item) if len(output_list) == 1: - output_list = [output_list,] + output_list = [output_list, ] return output_list @@ -47,11 +53,11 @@ def run(self, terms, variables=None, **kwargs): } if terms[1] == "password": - return self.getUserPass(conf, terms[0], terms[2]) + return self.get_user_pass(conf, terms[0], terms[2]) elif terms[1] == "doql": - return self.runDoql(conf, terms[0], terms[2]) + return self.run_doql(conf, terms[0], terms[2]) - def getUserPass(self, conf, device, username): + def get_user_pass(self, conf, device, username): url = conf['D42_URL'] + "/api/1.0/passwords/?plain_text=yes&device=" + device + "&username=" + username resp = requests.request("GET", url, @@ -72,7 +78,7 @@ def getUserPass(self, conf, device, username): else: raise AnsibleError("No password found for user: %s and device: %s" % (username, device)) - def runDoql(self, conf, query, output_type): + def run_doql(self, conf, query, output_type): url = conf['D42_URL'] + "/services/data/v1.0/query/" post_data = { @@ -80,17 +86,13 @@ def runDoql(self, conf, query, output_type): "header": 'yes' if output_type == 'list_dicts' else 'no' } - resp = requests.request("POST", - url, - auth=(conf['D42_USER'], conf['D42_PWD']), - data=post_data, - verify=False) + resp = requests.request("POST", url, auth=(conf['D42_USER'], conf['D42_PWD']), data=post_data, verify=False) if resp.status_code != 200: raise AnsibleError("API Call failed with status code: " + str(resp.status_code)) if output_type == 'string': - return [resp.text.replace('\n', ''),] + return [resp.text.replace('\n', ''), ] elif output_type == 'list': return resp.text.split('\n') diff --git a/plugins/lookup/d42_prompt.py b/plugins/lookup/d42_prompt.py index 9ff381f..0847dd1 100644 --- a/plugins/lookup/d42_prompt.py +++ b/plugins/lookup/d42_prompt.py @@ -1,15 +1,19 @@ +from __future__ import (absolute_import, division, print_function) from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase -import json, requests, imp, sys, csv, io, os +import json +import requests +import csv +import io +import os if 'D42_SKIP_SSL_CHECK' in os.environ and os.environ['D42_SKIP_SSL_CHECK'] == 'True': requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) -try: - from __main__ import display -except ImportError: - from ansible.utils.display import Display - display = Display() +from ansible.utils.display import Display +display = Display() + +__metaclass__ = type class LookupModule(LookupBase): @@ -18,27 +22,28 @@ class LookupModule(LookupBase): def get_list_from_csv(text): f = io.StringIO(text) output_list = [] - dict_reader = csv.DictReader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True, dialect='excel') + dict_reader = csv.DictReader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True, + dialect='excel') for item in dict_reader: output_list.append(item) if len(output_list) == 1: - output_list = [output_list,] + output_list = [output_list, ] return output_list def run(self, terms, variables=None, **kwargs): conf = { - 'D42_URL': terms[0], - 'D42_USER': terms[1], + 'D42_URL': terms[0], + 'D42_USER': terms[1], 'D42_PWD': terms[2], } if terms[4] == "password": - return self.getUserPass(conf, terms[3], terms[5]) + return self.get_user_pass(conf, terms[3], terms[5]) elif terms[4] == "doql": - return self.runDoql(conf, terms[3], terms[5]) + return self.run_doql(conf, terms[3], terms[5]) - def getUserPass(self, conf, device, username): + def get_user_pass(self, conf, device, username): url = conf['D42_URL'] + "/api/1.0/passwords/?plain_text=yes&device=" + device + "&username=" + username resp = requests.request("GET", url, @@ -59,7 +64,7 @@ def getUserPass(self, conf, device, username): else: raise AnsibleError("No password found for user: %s and device: %s" % (username, device)) - def runDoql(self, conf, query, output_type): + def run_doql(self, conf, query, output_type): url = conf['D42_URL'] + "/services/data/v1.0/query/" post_data = { @@ -77,7 +82,7 @@ def runDoql(self, conf, query, output_type): raise AnsibleError("API Call failed with status code: " + str(resp.status_code)) if output_type == 'string': - return [resp.text.replace('\n', ''),] + return [resp.text.replace('\n', ''), ] elif output_type == 'list': return resp.text.split('\n')