Skip to content

Commit

Permalink
Merge pull request #21 from device42/D42-16833
Browse files Browse the repository at this point in the history
D42-16833 - Legacy ansible static inventory file throw's an error when running
Fixed error when running the legacy inventory script.
  • Loading branch information
cscaglioned42 committed Sep 22, 2020
2 parents 9aadb3b + c63a479 commit efdce05
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
16 changes: 12 additions & 4 deletions README.md
Expand Up @@ -57,9 +57,8 @@ touch requirements.yml
```bash
collections:
- name: device42.d42
version: 1.1.1
source: https://galaxy.ansible.com
```
version: 1.1.2
source: https://galaxy.ansible.com/

* Install the Ansible collection by running command
```bash
Expand Down Expand Up @@ -157,6 +156,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 )
```
Expand All @@ -176,7 +182,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.

Expand Down
Empty file added contrib/__init__.py
Empty file.
Empty file added contrib/inventory/__init__.py
Empty file.
17 changes: 11 additions & 6 deletions 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
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion 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)"
Expand Down

0 comments on commit efdce05

Please sign in to comment.