Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python3
- """
- Custom inventory script for Ansible populated by the JSS
- """
- from os.path import dirname, realpath, join
- from urllib.parse import quote
- import argparse
- import json
- import configparser
- import requests
- # import pprint
- class JSS:
- def __init__(self, url, username, password):
- self.url = url
- self.username = username
- self.password = password
- def get_all_computers(self):
- req = requests.get(self.url + '/JSSResource/computers',
- auth=(self.username, self.password),
- headers={'Accept': 'application/json'})
- if req.status_code == 200:
- return json.loads(req.content.decode('utf-8'))
- return None
- def get_computer(self, id=None, name=None):
- if id:
- url = (self.url +
- '/JSSResource/computers/id/' +
- str(id))
- else:
- url = (self.url +
- '/JSSResource/computers/name/' +
- quote(name))
- req = requests.get(url,
- auth=(self.username, self.password),
- headers={'Accept': 'application/json'})
- if req.status_code == 200:
- return json.loads(req.content.decode('utf-8'))
- return None
- def get_all_computergroups(self):
- req = requests.get(self.url + '/JSSResource/computergroups',
- auth=(self.username, self.password),
- headers={'Accept': 'application/json'})
- if req.status_code == 200:
- return json.loads(req.content.decode('utf-8'))
- return None
- def get_computergroup(self, id=None, name=None):
- if id:
- url = (self.url +
- '/JSSResource/computergroups/id/' +
- str(id))
- else:
- url = (self.url +
- '/JSSResource/computergroups/name/' +
- quote(name))
- req = requests.get(url,
- auth=(self.username, self.password),
- headers={'Accept': 'application/json'})
- if req.status_code == 200:
- return json.loads(req.content.decode('utf-8'))
- return None
- # Empty inventory for testing.
- def empty_inventory():
- return {'_meta': {'hostvars': {}}}
- def main(args=None):
- # pp = pprint.PrettyPrinter(indent=2)
- mypath = dirname(realpath(__file__))
- config = read_jss_config(join(dirname(mypath),
- 'private/jss.conf'),
- 'JSS')
- jss = JSS(config['url'],
- config['api'][0],
- config['api'][1])
- all = jss.get_all_computers()
- if args.host:
- print(json.dumps(empty_inventory()))
- exit(0)
- computers = [x['name'] for x in all['computers']]
- ret = {'all': computers,
- '_meta': {'hostvars': {}}}
- for group in jss.get_all_computergroups()['computer_groups']:
- # print(jss.get_computergroup(id=group['id']))
- group = jss.get_computergroup(id=group['id'])
- name = group['computer_group']['name'].replace(' ', '_')
- ret[name] = []
- for computer in group['computer_group']['computers']:
- ret[name].append(computer['name'])
- # for computer in computers:
- # data = jss.get_computer(name=computer)
- # ret['_meta']['hostvars'][computer] = data
- print(json.dumps(ret))
- def read_jss_config(path, section):
- '''Read the jss config and return a dictionary containing the proper settings
- :path - Full path to the config file
- :section - Section name that contains the JSS info
- '''
- config = configparser.ConfigParser()
- config.read(path)
- return {'url': config.get(section, 'URL'),
- 'api': (config.get(section, 'username'),
- config.get(section, 'password')),
- 'repo': (config.get(section, 'repo_rw_username'),
- config.get(section, 'repo_rw_password'),
- config.get(section, 'repo_name'),
- config.get(section, 'repo_mount_point'))}
- if __name__ == '__main__':
- PARSER = argparse.ArgumentParser()
- PARSER.add_argument('--host', action='store')
- PARSER.add_argument('--list', action='store_true')
- main(PARSER.parse_args())
Add Comment
Please, Sign In to add comment