From 9b05a41eece3104a508f4166cbc83de7ca0ff4f3 Mon Sep 17 00:00:00 2001 From: Richard Bywater Date: Tue, 5 Dec 2017 14:47:33 +1300 Subject: [PATCH 1/3] Add ability to append suffix to host names for Cloudforms Inventory Allows for use of a suffix that will be appended to host names returned from Cloudforms API if that suffix is not present. For example with a suffix of 'example.org', the following results would be shown for a particular Cloudforms host name: someexample -> someexample.example.org someexample.example.org -> someexample.example.org The main use-case for this is, when one Inventory Source is returning names that have a FQDN name whilst others are returning a shortname, to ensure that the hosts in an inventory aren't effectively duplicated. --- awx/main/tasks.py | 2 +- awx/plugins/inventory/cloudforms.ini.example | 3 +++ awx/plugins/inventory/cloudforms.py | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/awx/main/tasks.py b/awx/main/tasks.py index 48e1a15509..129e06b854 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -1756,7 +1756,7 @@ class RunInventoryUpdate(BaseTask): cp.set(section, 'ssl_verify', "false") cloudforms_opts = dict(inventory_update.source_vars_dict.items()) - for opt in ['version', 'purge_actions', 'clean_group_keys', 'nest_tags']: + for opt in ['version', 'purge_actions', 'clean_group_keys', 'nest_tags', 'suffix']: if opt in cloudforms_opts: cp.set(section, opt, cloudforms_opts[opt]) diff --git a/awx/plugins/inventory/cloudforms.ini.example b/awx/plugins/inventory/cloudforms.ini.example index 142514a65e..1d81588410 100644 --- a/awx/plugins/inventory/cloudforms.ini.example +++ b/awx/plugins/inventory/cloudforms.ini.example @@ -27,6 +27,9 @@ clean_group_keys = True # Explode tags into nested groups / subgroups nest_tags = False +# If set, ensure host name are suffixed with this value +# suffix = example.org + [cache] # Maximum time to trust the cache in seconds diff --git a/awx/plugins/inventory/cloudforms.py b/awx/plugins/inventory/cloudforms.py index 69c149bfc5..25bc032efd 100755 --- a/awx/plugins/inventory/cloudforms.py +++ b/awx/plugins/inventory/cloudforms.py @@ -174,6 +174,11 @@ class CloudFormsInventory(object): else: self.cloudforms_nest_tags = False + if config.has_option('cloudforms', 'suffix'): + self.cloudforms_suffix = config.get('cloudforms', 'suffix') + else: + self.cloudforms_suffix = None + # Ansible related try: group_patterns = config.get('ansible', 'group_patterns') @@ -280,6 +285,9 @@ class CloudFormsInventory(object): print("Updating cache...") for host in self._get_hosts(): + if self.cloudforms_suffix is not None and not host['name'].endswith(self.cloudforms_suffix): + host['name'] = host['name'] + self.cloudforms_suffix + # Ignore VMs that are not powered on if host['power_state'] != 'on': if self.args.debug: From b5db65205045b81041b1f2b7d3943e831c9d36aa Mon Sep 17 00:00:00 2001 From: Richard Bywater Date: Sun, 14 Jan 2018 14:05:34 +1300 Subject: [PATCH 2/3] Clarify that leading fullstop needed --- awx/plugins/inventory/cloudforms.ini.example | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/awx/plugins/inventory/cloudforms.ini.example b/awx/plugins/inventory/cloudforms.ini.example index 1d81588410..dc055c1fb7 100644 --- a/awx/plugins/inventory/cloudforms.ini.example +++ b/awx/plugins/inventory/cloudforms.ini.example @@ -28,7 +28,8 @@ clean_group_keys = True nest_tags = False # If set, ensure host name are suffixed with this value -# suffix = example.org +# Note: This suffix *must* include the leading '.' as it is appended to the hostname as is +# suffix = .example.org [cache] From b8c76301debe965924b57b7881fcb8f3046429e5 Mon Sep 17 00:00:00 2001 From: Richard Bywater Date: Wed, 17 Jan 2018 13:20:59 +1300 Subject: [PATCH 3/3] Add validation to ensure leading fullstop for suffix --- awx/plugins/inventory/cloudforms.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/awx/plugins/inventory/cloudforms.py b/awx/plugins/inventory/cloudforms.py index 25bc032efd..25b8d23159 100755 --- a/awx/plugins/inventory/cloudforms.py +++ b/awx/plugins/inventory/cloudforms.py @@ -29,6 +29,7 @@ from time import time import requests from requests.auth import HTTPBasicAuth import warnings +from ansible.errors import AnsibleError try: import json @@ -176,6 +177,8 @@ class CloudFormsInventory(object): if config.has_option('cloudforms', 'suffix'): self.cloudforms_suffix = config.get('cloudforms', 'suffix') + if self.cloudforms_suffix[0] != '.': + raise AnsibleError('Leading fullstop is required for Cloudforms suffix') else: self.cloudforms_suffix = None