Obtenez des données sur le maquereau et crachez du json pour un inventaire dynamique ansible

Aperçu

Un programme qui récupère les données du maquereau et crache json pour un inventaire dynamique ansible. J'étais particulier sur le fait de bien classer par les enfants. Vous pouvez utiliser ce programme pour obtenir dynamiquement la liste d'hôtes et exécuter ansible.

code

https://gist.github.com/sioncojp/d24eb96d7c253c5a1ab5

inventory.py


#!/usr/bin/python
import urllib2
import sys
try:
    import json
except ImportError:
    import simplejson as json

###Entrez votre clé API
MACKEREL_API_KEY=''

class MackerelInventory:
  def getData(self, path, params=''):
      url = 'https://mackerel.io/api/v0/' + str(path) + str(params)
      headers = { 'X-Api-Key': MACKEREL_API_KEY }
      request = urllib2.Request(url, None, headers)
      response = urllib2.urlopen(request)
      return json.loads(response.read())

  def getGroupList(self):
      hosts = self.getHosts()
      print json.dumps(hosts, indent=4)

  def getHostInfo(self, hostname):
      hosts = self.getHosts(hostname)
      print json.dumps(hosts['_meta']['hostvars'][hostname], indent=4)

  def getHosts(self, hostname=''):
      if hostname != '':
          params = '?name=' + hostname

      inventories = {
          'production': { 'children': [] },
          '_meta': { 'hostvars': {} },
      }

      services = self.getData('services')['services']
      for t in services:
          if t not in inventories['production']['children']:
            inventories['production']['children'].append(t['name'])
          if t['name'] not in inventories:
            inventories[t['name']] = { 'children': [] }

      hosts = self.getData('hosts.json?')
      for host in hosts['hosts']:
          ipAddress = None
          for interface in host['interfaces']:
              if interface['name'] == 'eth0':
                  ipAddress = interface['ipAddress']
                  break

          if ipAddress == None:
              continue

          for serviceNames, roles in host['roles'].iteritems():
              for t in roles:
                  role_name = t + '_' +serviceNames
                  if role_name not in inventories[serviceNames]['children']:
                    inventories[serviceNames]['children'].append(role_name)
                  if role_name not in inventories:
                    inventories[role_name] = { 'hosts': [] }

                  inventories[role_name]['hosts'].append(host['name'])
                  inventories['_meta']['hostvars'][host['name']] = {
                  'ansible_ssh_host': ipAddress,
                  'ansible_ssh_port': '22',
                  }

      return inventories

mackerel = MackerelInventory()
if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
    mackerel.getGroupList()
elif len(sys.argv) == 3 and (sys.argv[1] == '--host'):
    mackerel.getHostInfo(sys.argv[2])
else:
    print "Usage: %s --list or --host <hostname>" % sys.argv[0]
    sys.exit(1)

Exemple de sortie

###Liste liste
$ python inventory.py --list
{
    "hoge_jp": {
        "children": [
            "web_hoge_jp",
            "db_hoge_jp"
        ]
    },
    "db": {
        "children": [
            "hoge_jp_db"
        ]
    },
    "web": {
        "children": [
            "hoge_jp_web"
        ]
    },
    "db_hoge_jp": {
        "hosts": [
            "host10.example.com"
        ]
    },
    "hoge_jp_db": {
        "hosts": [
            "host10.example.com"
        ]
    },
    "hoge_jp_web": {
        "hosts": [
            "host1.example.com",
            "host2.example.com"
        ]
    },
    "web_hoge_jp": {
        "hosts": [
            "host1.example.com",
            "host2.example.com"
        ]
    },
    "_meta": {
        "hostvars": {
            "host1.example.com": {
                "ansible_ssh_host": "192.168.1.1",
                "ansible_ssh_port": "22"
            },
            "host2.example.com": {
                "ansible_ssh_host": "192.168.1.2",
                "ansible_ssh_port": "22"
            },
            "host10.example.com": {
                "ansible_ssh_host": "192.168.1.10",
                "ansible_ssh_port": "22"
            }
        }
    }
}


###Obtenir des informations sur l'hôte
$ python inventory.py --host host1.example.com
{
    "ansible_ssh_host": "192.168.1.1",
    "ansible_ssh_port": "22"
}

Recommended Posts

Obtenez des données sur le maquereau et crachez du json pour un inventaire dynamique ansible
Fusionner les données au format JSON avec Ansible
Obtenez la clé pour la migration de la deuxième couche de données JSON avec python