Skip to content

Commit

Permalink
Add ability to append suffix to host names for Cloudforms Inventory (a…
Browse files Browse the repository at this point in the history
…nsible#33565)

* 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 Cloudforms has short names rather
than FQDN and there is a desire to to use the FQDN as the name.

* Add example line into Cloudforms INI file

* Clarify that leading fullstop needed

* Add validation to ensure leading fullstop for suffix
  • Loading branch information
rbywater authored and maxamillion committed Jan 17, 2018
1 parent 217ff44 commit a7e27b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions contrib/inventory/cloudforms.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ clean_group_keys = True
# Explode tags into nested groups / subgroups
nest_tags = False

# If set, ensure host name are suffixed with this value
# Note: This suffix *must* include the leading '.' as it is appended to the hostname as is
# suffix = .example.org

[cache]

# Maximum time to trust the cache in seconds
Expand Down
11 changes: 11 additions & 0 deletions contrib/inventory/cloudforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import requests
from requests.auth import HTTPBasicAuth
import warnings
from ansible.errors import AnsibleError

try:
import json
Expand Down Expand Up @@ -174,6 +175,13 @@ def read_settings(self):
else:
self.cloudforms_nest_tags = False

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

# Ansible related
try:
group_patterns = config.get('ansible', 'group_patterns')
Expand Down Expand Up @@ -280,6 +288,9 @@ def update_cache(self):
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:
Expand Down

0 comments on commit a7e27b7

Please sign in to comment.