Allow applications postfix to be configured
The postfix _hosts appended to applications to create host groups is now
configurable.
Signed-off-by: martin f. krafft <madduck@madduck.net>
diff --git a/README b/README
index 73c1fd2..a18b280 100644
--- a/README
+++ b/README
@@ -337,7 +337,9 @@
- Every entry in the list of a host's applications might well correspond to
an Ansible playbook. Therefore, reclass creates a (Ansible-)group for
- every application, and adds '_hosts' to the name.
+ every application, and adds '_hosts' to the name. This postfix can be
+ configured with a CLI option (--applications-postfix) or in the
+ configuration file (applications_postfix).
For instance, the ssh.server class adds the ssh.server application to
a node's application list. Now the admin might create an Ansible playbook
diff --git a/adapters/ansible b/adapters/ansible
index 2e485ec..53b8f52 100755
--- a/adapters/ansible
+++ b/adapters/ansible
@@ -48,6 +48,9 @@
classes_uri = options['nodes_uri']
options['classes_uri'] = classes_uri
+if 'applications_postfix' not in options:
+ options['applications_postfix'] = '_hosts'
+
# Invoke reclass according to what Ansible wants.
# If the 'node' option is set, we want node information. If the option is
# False instead, we print the inventory. Yeah for option abuse!
@@ -70,7 +73,8 @@
try:
data = reclass.get_data(options['storage_type'], options['nodes_uri'],
- options['classes_uri'], options['node'])
+ options['classes_uri'],
+ options['applications_postfix'], options['node'])
if options['node']:
# Massage and shift the data like Ansible wants it
diff --git a/config.py b/config.py
index 58c29c5..1a3c603 100644
--- a/config.py
+++ b/config.py
@@ -33,6 +33,9 @@
default=defaults.get('pretty_print', False),
action="store_true",
help='try to make the output prettier [%default]')
+ options_group.add_option('--applications-postfix', dest='applications_postfix',
+ default=defaults.get('applications_postfix', '_hosts'),
+ help="the postfix to apply to groups made from applications ['%default']")
parser.add_option_group(options_group)
run_modes = optparse.OptionGroup(parser, 'Modes',
diff --git a/reclass.py b/reclass.py
index cd44652..9471646 100755
--- a/reclass.py
+++ b/reclass.py
@@ -22,10 +22,12 @@
def get_options(config_file=None):
return config.get_options(__name__, __version__, __description__, config_file)
-def get_data(storage_type, nodes_uri, classes_uri, node):
+def get_data(storage_type, nodes_uri, classes_uri, applications_postfix, node):
storage_class = StorageBackendLoader(storage_type).load()
storage = storage_class(os.path.abspath(os.path.expanduser(nodes_uri)),
- os.path.abspath(os.path.expanduser(classes_uri)))
+ os.path.abspath(os.path.expanduser(classes_uri)),
+ applications_postfix
+ )
if node is False:
ret = storage.inventory()
else:
@@ -54,7 +56,8 @@
try:
options = get_options(config_file)
data = get_data(options.storage_type, options.nodes_uri,
- options.classes_uri, options.node)
+ options.classes_uri, options.applications_postfix,
+ options.node)
print output(data, options.output, options.pretty_print)
sys.exit(posix.EX_OK)
diff --git a/storage/__init__.py b/storage/__init__.py
index 0613bfe..27cbf7d 100644
--- a/storage/__init__.py
+++ b/storage/__init__.py
@@ -8,9 +8,10 @@
#
class NodeStorageBase(object):
- def __init__(self, nodes_uri, classes_uri):
+ def __init__(self, nodes_uri, classes_uri, applications_postfix):
self._nodes_uri = nodes_uri
self._classes_uri = classes_uri
+ self._applications_postfix = applications_postfix
nodes_uri = property(lambda self: self._nodes_uri)
classes_uri = property(lambda self: self._classes_uri)
@@ -32,7 +33,7 @@
def inventory(self):
entity, applications, classes = self._list_inventory()
ret = classes
- ret.update([(k + '_hosts',v) for k,v in applications.iteritems()])
+ ret.update([(k + self._applications_postfix,v) for k,v in applications.iteritems()])
return ret
class StorageBackendLoader(object):
diff --git a/storage/yaml_fs/__init__.py b/storage/yaml_fs/__init__.py
index f53048f..9843333 100644
--- a/storage/yaml_fs/__init__.py
+++ b/storage/yaml_fs/__init__.py
@@ -16,8 +16,9 @@
class ExternalNodeStorage(NodeStorageBase):
- def __init__(self, nodes_uri, classes_uri):
- super(ExternalNodeStorage, self).__init__(nodes_uri, classes_uri)
+ def __init__(self, nodes_uri, classes_uri, applications_postfix):
+ super(ExternalNodeStorage, self).__init__(nodes_uri, classes_uri,
+ applications_postfix)
def _read_nodeinfo(self, name, base_uri, seen, nodename=None):
path = os.path.join(base_uri, name + FILE_EXTENSION)
diff --git a/storage/yaml_fs/tests/test_yaml_fs.py b/storage/yaml_fs/tests/test_yaml_fs.py
index 89ae294..b3dee9b 100644
--- a/storage/yaml_fs/tests/test_yaml_fs.py
+++ b/storage/yaml_fs/tests/test_yaml_fs.py
@@ -10,14 +10,15 @@
import os
+POSTFIX = '_hosts'
PWD = os.path.dirname(__file__)
HOSTS = ['red', 'blue', 'green']
-MEMBERSHIPS = {'apt_hosts': HOSTS,
- 'motd_hosts': HOSTS,
- 'firewall_hosts': HOSTS[:2],
- 'lighttpd_hosts': HOSTS[:2],
- 'postfix_hosts': HOSTS[1:],
- 'blues_hosts': HOSTS[1:2],
+MEMBERSHIPS = {'apt%s' % POSTFIX: HOSTS,
+ 'motd%s' % POSTFIX: HOSTS,
+ 'firewall%s' % POSTFIX: HOSTS[:2],
+ 'lighttpd%s' % POSTFIX: HOSTS[:2],
+ 'postfix%s' % POSTFIX: HOSTS[1:],
+ 'blues%s' % POSTFIX: HOSTS[1:2],
'basenode': HOSTS,
'debiannode': HOSTS,
'debiannode@sid': HOSTS[0:1],
@@ -33,7 +34,8 @@
def setUp(self):
self._storage = ExternalNodeStorage(os.path.join(PWD, 'nodes'),
- os.path.join(PWD, 'classes'))
+ os.path.join(PWD, 'classes'),
+ POSTFIX)
self._inventory = self._storage.inventory()
def test_inventory_setup(self):