From 1b99aa41c330b772ff7db8f0cccce96d608f9b74 Mon Sep 17 00:00:00 2001 From: Daniel Lobato Date: Wed, 11 May 2016 12:02:17 +0200 Subject: Set prefix for Ansible groups --- README.md | 20 ++++++++++++-------- foreman.ini | 1 + foreman_ansible_inventory.py | 8 +++++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fd1d241..fd8103d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ foreman_ansible_inventory ========================= -This script can be used as an ansible dynamic inventory[1]. +This script can be used as an Ansible dynamic inventory[1]. The connection parameters are set up via a configuration file *foreman.ini*. *foreman.ini* is found using the following order of discovery. @@ -40,7 +40,7 @@ of the host and it's hostgroups: ... } -and could therefore be used in ansible like: +and could therefore be used in Ansible like: - debug: msg="From Foreman host {{ foreman['uuid'] }}" @@ -51,20 +51,24 @@ Which yields "msg": "From Foreman host 50190bd1-052a-a34a-3c9c-df37a39550bf" } +## Automatic Ansible groups -## Automatic ansible groups +The inventory will provide a set of groups, by default prefixed by +'foreman_'. If you want to customize this prefix, change the +group_prefix option in /etc/ansible/foreman.ini. The rest of this +guide will assume the default prefix of 'foreman' -The hostgroup, location and organization of each host is created as -ansible group with a foreman_ prefix, all lowercase and +The hostgroup, location and organization of each host are created as +Ansible groups with a foreman_ prefix, all lowercase and problematic parameters removed. So e.g. the foreman hostgroup myapp / webtier / datacenter1 -would turn into the ansible group: +would turn into the Ansible group: foreman_hostgroup_myapp_webtier_datacenter1 -Furthermore ansible 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 and host variables. @@ -87,7 +91,7 @@ then *group_patterns* like: "{app_param}", "{subnet_name}-{provision_method}"] -would put the host into the additional ansible groups: +would put the host into the additional Ansible groups: - myapp-webtier-datacenter1 - myapp-webtier diff --git a/foreman.ini b/foreman.ini index d5cd56e..fb103a8 100644 --- a/foreman.ini +++ b/foreman.ini @@ -9,6 +9,7 @@ group_patterns = ["{app}-{tier}-{color}", "{app}-{color}", "{app}", "{tier}"] +group_prefix = foreman_ [cache] path = . diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py index 57cf50a..1e9d7ef 100755 --- a/foreman_ansible_inventory.py +++ b/foreman_ansible_inventory.py @@ -112,6 +112,11 @@ class ForemanInventory(object): self.group_patterns = eval(group_patterns) + try: + self.group_prefix = config.get('ansible', 'group_prefix') + except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): + self.group_prefix = "foreman_" + # Cache related try: cache_path = os.path.expanduser(config.get('cache', 'path')) @@ -207,7 +212,7 @@ class ForemanInventory(object): for group in ['hostgroup', 'location', 'organization']: val = host.get('%s_name' % group) if val: - safe_key = self.to_safe('foreman_%s_%s' % (group, val.lower())) + safe_key = self.to_safe('%s%s_%s' % (self.group_prefix, group, val.lower())) self.push(self.inventory, safe_key, dns_name) params = self._resolve_params(host) @@ -302,3 +307,4 @@ class ForemanInventory(object): return json.dumps(data) ForemanInventory() + -- cgit v1.2.3