From 8d26e191947dbf151c6d95a28c0999fc262f5178 Mon Sep 17 00:00:00 2001 From: Omar Sanchez Date: Tue, 18 Aug 2020 11:24:29 -0400 Subject: [PATCH 1/4] updated legacy script to run with new structure --- contrib/inventory/lib.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/contrib/inventory/lib.py b/contrib/inventory/lib.py index e213741..6ad87a6 100644 --- a/contrib/inventory/lib.py +++ b/contrib/inventory/lib.py @@ -1,11 +1,14 @@ from __future__ import (absolute_import, division, print_function) -from io import StringIO import requests -import base64 import configparser import csv import sys import os +from requests.auth import HTTPBasicAuth +try: + from StringIO import StringIO ## for Python 2 +except ImportError: + from io import StringIO ## for Python 3 try: import json @@ -18,8 +21,11 @@ def get_conf(): try: + current_dir = os.getcwd() + config_file_path = os.path.join(current_dir, 'contrib/inventory/conf.ini') + conf_file = configparser.ConfigParser() - conf_file.read('conf.ini') + conf_file.read(config_file_path) except Exception: print('Failed to read config file, have you changed the config file name to conf.ini?') sys.exit(1) @@ -90,14 +96,13 @@ def __init__(self, conf): def fetcher(self, url, query): headers = { - 'Authorization': 'Basic ' + base64.b64encode(self.username + ':' + self.password), 'Content-Type': 'application/x-www-form-urlencoded' } r = requests.post(url, data={ 'query': query, 'header': 'yes' - }, headers=headers, verify=False) + }, headers=headers, verify=False, auth=HTTPBasicAuth(self.username, self.password)) return r.text def doql(self): @@ -106,7 +111,7 @@ def doql(self): @staticmethod def get_list_from_csv(text): - f = StringIO(text.encode("utf-8", "replace")) + f = StringIO(text) list_ = [] dict_reader = csv.DictReader(f, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True, dialect='excel') for item in dict_reader: From 271ee17a970452436cfed0d46928ea138a62380d Mon Sep 17 00:00:00 2001 From: Omar Sanchez Date: Tue, 18 Aug 2020 11:38:14 -0400 Subject: [PATCH 2/4] updated readme --- README.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f559420..24eb1a2 100644 --- a/README.md +++ b/README.md @@ -57,8 +57,8 @@ touch requirements.yml ```bash collections: - name: device42.d42 -version: 1.0.0 -source: https://galaxy.ansible.com/device42/d42 +version: 1.1.1 +source: https://galaxy.ansible.com/ ``` * Install the Ansible collection by running command @@ -157,6 +157,13 @@ ansible-playbook *.yaml -f 10 ## Legacy Inventory Usage ----------------------------- +### Requirements +- ansible 2.9+ +- python 3.6.x+ or python 2.7.x +- Device42 16.12.00+ +- requests (you can install it with pip install requests or apt-get install python-requests) +- Ansible must have an available connection to your Device42 instance in order to collect devices for inventory + * rename conf.sample.ini to conf.ini * in conf add D42 URL/credentials ( also instead of conf file, possible to use environment variables ) ``` @@ -176,7 +183,9 @@ GROUP_BY_REFERENCE_FIELD = name SPLIT_GROUP_BY_COMMA = False ``` -Run the `python d42_ansible_inventory_hostfile.py` and enjoy! +Navigate to the root folder of the script `.../ansible_device42` + +Run the `python -m contrib.inventory.d42_ansible_inventory_hostfile` and enjoy! Also you may use automatic version with Ansible commands ex. From 5273062cbfb156d2214fa7938286bf4ec7171d30 Mon Sep 17 00:00:00 2001 From: Omar Sanchez Date: Tue, 18 Aug 2020 11:42:49 -0400 Subject: [PATCH 3/4] upped version --- README.md | 2 +- galaxy.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 24eb1a2..9447d92 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ touch requirements.yml ```bash collections: - name: device42.d42 -version: 1.1.1 +version: 1.1.2 source: https://galaxy.ansible.com/ ``` diff --git a/galaxy.yml b/galaxy.yml index 2cd0820..e26d537 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,6 +1,6 @@ namespace: "device42" name: "d42" -version: "1.1.1" +version: "1.1.2" readme: "README.md" authors: - "Will Tome (@willtome)" From 060d46249cd3649d66f09d089ba1e26ffd34c596 Mon Sep 17 00:00:00 2001 From: Omar Sanchez Date: Tue, 1 Sep 2020 10:43:19 -0400 Subject: [PATCH 4/4] needed init files for packages --- contrib/__init__.py | 0 contrib/inventory/__init__.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 contrib/__init__.py create mode 100644 contrib/inventory/__init__.py diff --git a/contrib/__init__.py b/contrib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/contrib/inventory/__init__.py b/contrib/inventory/__init__.py new file mode 100644 index 0000000..e69de29