Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add foreman

  • Loading branch information...
commit fc8720aa65cf2262e2ebbe0aac2155b8e81d5c1e 1 parent b5e487f
@franckcuny authored
Showing with 80 additions and 0 deletions.
  1. +80 −0 plugins/inventory/theforeman.py
View
80 plugins/inventory/theforeman.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+
+'''
+Foreman external inventory script
+=================================
+'''
+
+import sys
+# import os
+import argparse
+# import re
+# import ConfigParser
+
+# try:
+# import json
+# except ImportError:
+# import simplejson as json
+
+# import python-foreman
+
+try:
+ from foreman.client import Foreman
+except ImportError, e:
+ print e
+ print ('python-foreman required for this module')
+ sys.exit(1)
+
+
+class ForemanInventory(object):
+ """Foreman Inventory"""
+
+ def _empty_inventory(self):
+ """Empty inventory"""
+ return {'_meta': {'hostvars': {}}}
+
+ def __init__(self):
+ """Main execution path"""
+
+ self.inventory = self._empty_inventory()
+
+ # Index of host name (address) to instance ID
+ self.index = {}
+
+ # Read settings and parse CLI arguments
+ self.read_settings()
+ self.parse_cli_args()
+
+ if self.args.host:
+ data_to_print = self.get_host_info()
+ elif self.args.list:
+ data_to_print = self.get_inventory()
+ else:
+ data_to_print = "nop"
+
+ print data_to_print
+
+ def get_host_info(self):
+ """Get information about an host"""
+ pass
+
+ def get_inventory(self):
+ """Get all the host from the inventory"""
+ f = Foreman('http://foreman', ('', ''))
+ h = f.index_hosts()
+ return h
+
+ def read_settings(self):
+ """Read the settings from the foreman.ini file"""
+ pass
+
+ def parse_cli_args(self):
+ """Command line argument processing"""
+ parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on EC2')
+ parser.add_argument('--list', action='store_true', default=True, help='List instances (default: True)')
+ parser.add_argument('--host', action='store', help='Get all the variables about a specific instance')
+ parser.add_argument('--refresh-cache', action='store_true', default=False, help='Force refresh of cache by making API requests to EC2 (default: False - use cache files)')
+ self.args = parser.parse_args()
+
+
+ForemanInventory()
Please sign in to comment.
Something went wrong with that request. Please try again.