Guest User

Untitled

a guest
Feb 13th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. #!/usr/local/bin/python3
  2. """
  3. Custom inventory script for Ansible populated by the JSS
  4. """
  5.  
  6. from os.path import dirname, realpath, join
  7. from urllib.parse import quote
  8. import argparse
  9. import json
  10. import configparser
  11. import requests
  12. # import pprint
  13.  
  14.  
  15. class JSS:
  16.  
  17. def __init__(self, url, username, password):
  18. self.url = url
  19. self.username = username
  20. self.password = password
  21.  
  22. def get_all_computers(self):
  23. req = requests.get(self.url + '/JSSResource/computers',
  24. auth=(self.username, self.password),
  25. headers={'Accept': 'application/json'})
  26. if req.status_code == 200:
  27. return json.loads(req.content.decode('utf-8'))
  28. return None
  29.  
  30. def get_computer(self, id=None, name=None):
  31. if id:
  32. url = (self.url +
  33. '/JSSResource/computers/id/' +
  34. str(id))
  35. else:
  36. url = (self.url +
  37. '/JSSResource/computers/name/' +
  38. quote(name))
  39.  
  40. req = requests.get(url,
  41. auth=(self.username, self.password),
  42. headers={'Accept': 'application/json'})
  43. if req.status_code == 200:
  44. return json.loads(req.content.decode('utf-8'))
  45. return None
  46.  
  47. def get_all_computergroups(self):
  48. req = requests.get(self.url + '/JSSResource/computergroups',
  49. auth=(self.username, self.password),
  50. headers={'Accept': 'application/json'})
  51. if req.status_code == 200:
  52. return json.loads(req.content.decode('utf-8'))
  53. return None
  54.  
  55. def get_computergroup(self, id=None, name=None):
  56. if id:
  57. url = (self.url +
  58. '/JSSResource/computergroups/id/' +
  59. str(id))
  60. else:
  61. url = (self.url +
  62. '/JSSResource/computergroups/name/' +
  63. quote(name))
  64. req = requests.get(url,
  65. auth=(self.username, self.password),
  66. headers={'Accept': 'application/json'})
  67. if req.status_code == 200:
  68. return json.loads(req.content.decode('utf-8'))
  69. return None
  70.  
  71.  
  72. # Empty inventory for testing.
  73. def empty_inventory():
  74. return {'_meta': {'hostvars': {}}}
  75.  
  76.  
  77. def main(args=None):
  78. # pp = pprint.PrettyPrinter(indent=2)
  79. mypath = dirname(realpath(__file__))
  80. config = read_jss_config(join(dirname(mypath),
  81. 'private/jss.conf'),
  82. 'JSS')
  83.  
  84. jss = JSS(config['url'],
  85. config['api'][0],
  86. config['api'][1])
  87.  
  88. all = jss.get_all_computers()
  89.  
  90. if args.host:
  91. print(json.dumps(empty_inventory()))
  92. exit(0)
  93. computers = [x['name'] for x in all['computers']]
  94. ret = {'all': computers,
  95. '_meta': {'hostvars': {}}}
  96. for group in jss.get_all_computergroups()['computer_groups']:
  97. # print(jss.get_computergroup(id=group['id']))
  98. group = jss.get_computergroup(id=group['id'])
  99. name = group['computer_group']['name'].replace(' ', '_')
  100. ret[name] = []
  101. for computer in group['computer_group']['computers']:
  102. ret[name].append(computer['name'])
  103. # for computer in computers:
  104. # data = jss.get_computer(name=computer)
  105. # ret['_meta']['hostvars'][computer] = data
  106.  
  107. print(json.dumps(ret))
  108.  
  109.  
  110. def read_jss_config(path, section):
  111. '''Read the jss config and return a dictionary containing the proper settings
  112. :path - Full path to the config file
  113. :section - Section name that contains the JSS info
  114. '''
  115. config = configparser.ConfigParser()
  116. config.read(path)
  117. return {'url': config.get(section, 'URL'),
  118. 'api': (config.get(section, 'username'),
  119. config.get(section, 'password')),
  120. 'repo': (config.get(section, 'repo_rw_username'),
  121. config.get(section, 'repo_rw_password'),
  122. config.get(section, 'repo_name'),
  123. config.get(section, 'repo_mount_point'))}
  124.  
  125.  
  126. if __name__ == '__main__':
  127. PARSER = argparse.ArgumentParser()
  128. PARSER.add_argument('--host', action='store')
  129. PARSER.add_argument('--list', action='store_true')
  130. main(PARSER.parse_args())
Add Comment
Please, Sign In to add comment