diff options
author | Guido Günther <agx@sigxcpu.org> | 2016-02-29 18:53:59 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2016-02-29 18:53:59 +0100 |
commit | 0d52a129e223be61dbf54b221cfd2ba4bfc27c52 (patch) | |
tree | a009b14e2d41686f6f9f1a178ed77407d3a85b3b | |
parent | 12cff5111229280c8c264d93cce86509de83d8a9 (diff) | |
parent | d2928fa1b741aa1e11bfad64cb702fe45ab61d8e (diff) |
Merge branch 'gh/5'v0.0.2
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r--[-rwxr-xr-x] | foreman_ansible_inventory.py | 16 |
2 files changed, 20 insertions, 7 deletions
@@ -60,9 +60,9 @@ would turn into the ansible group: foreman_hostgroup_myapp_webtier_datacenter1 -Furthermore groups can be created on the fly using the +Furthermore ansible groups can be created on the fly using the *group_patterns* variable in *foreman.ini* so that you can build up -hierarchies using parameters on the hostgroup. +hierarchies using parameters on the hostgroup and host variables. Lets assume you have a host that is built using this nested hostgroup: @@ -72,20 +72,23 @@ and each of the hostgroups defines a parameters respectively: myapp: app_param = myapp webtier: tier_param = webtier - datacenter1: dc_param = datacenter1 + datacenter1: dc_param = datacenter1 +The host is also in a subnet called "mysubnet" and provisioned via an image then *group_patterns* like: [ansible] group_patterns = ["{app_param}-{tier_param}-{dc_param}", "{app_param}-{tier_param}", - "{app_param}"] + "{app_param}", + "{subnet_name}-{provision_method}"] would put the host into the additional ansible groups: - myapp-webtier-datacenter1 - myapp-webtier - myapp + - mysubnet-image by recursively resolving the hostgroups, getting the parameter keys and values and doing a Python *string.format()* like replacement on diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py index 5bb16b6..461146c 100755..100644 --- a/foreman_ansible_inventory.py +++ b/foreman_ansible_inventory.py @@ -20,6 +20,7 @@ import argparse import ConfigParser +import copy import os import re from time import time @@ -200,12 +201,21 @@ class ForemanInventory(object): safe_key = self.to_safe('foreman_%s_%s' % (group, val.lower())) self.push(self.inventory, safe_key, dns_name) - # Ansible groups by parameters in host groups based - # on group_patterns in config params = self._resolve_params(host) + + # Ansible groups by parameters in host groups and Foreman host + # attributes. + groupby = copy.copy(params) + for k, v in host.items(): + if isinstance(v, basestring): + groupby[k] = self.to_safe(v) + elif isinstance(v, int): + groupby[k] = v + + # The name of the ansible groups is given by group_patterns: for pattern in self.group_patterns: try: - key = pattern.format(**params) + key = pattern.format(**groupby) self.push(self.inventory, key, dns_name) except KeyError: pass # Host not part of this group |