summaryrefslogtreecommitdiffstats
path: root/filter_plugins
diff options
context:
space:
mode:
authorAndrew Butcher <abutcher@redhat.com>2016-05-03 16:56:15 -0400
committerAndrew Butcher <abutcher@redhat.com>2016-05-04 11:21:07 -0400
commitf0c412a9ab50a9030d51f83c459a66451a7c344a (patch)
treecb0363a5c89cbb55cd22e3173da7cbe5ef45a773 /filter_plugins
parent60c4e41791c9517ad7201c8c04f0fee2bff8873c (diff)
downloadopenshift-f0c412a9ab50a9030d51f83c459a66451a7c344a.tar.gz
openshift-f0c412a9ab50a9030d51f83c459a66451a7c344a.tar.bz2
openshift-f0c412a9ab50a9030d51f83c459a66451a7c344a.tar.xz
openshift-f0c412a9ab50a9030d51f83c459a66451a7c344a.zip
Add oo_merge_hostvars filter for merging host & play variables.
Diffstat (limited to 'filter_plugins')
-rw-r--r--filter_plugins/oo_filters.py41
1 files changed, 40 insertions, 1 deletions
diff --git a/filter_plugins/oo_filters.py b/filter_plugins/oo_filters.py
index b08670678..402103b09 100644
--- a/filter_plugins/oo_filters.py
+++ b/filter_plugins/oo_filters.py
@@ -7,10 +7,12 @@ Custom filters for use in openshift-ansible
from ansible import errors
from collections import Mapping
+from distutils.version import LooseVersion
from operator import itemgetter
import OpenSSL.crypto
import os
import pdb
+import pkg_resources
import re
import json
import yaml
@@ -71,6 +73,42 @@ class FilterModule(object):
return merged
@staticmethod
+ def oo_merge_hostvars(hostvars, variables, inventory_hostname):
+ """ Merge host and play variables.
+
+ When ansible version is greater than or equal to 2.0.0,
+ merge hostvars[inventory_hostname] with variables (ansible vars)
+ otherwise merge hostvars with hostvars['inventory_hostname'].
+
+ Ex: hostvars={'master1.example.com': {'openshift_variable': '3'},
+ 'openshift_other_variable': '7'}
+ variables={'openshift_other_variable': '6'}
+ inventory_hostname='master1.example.com'
+ returns {'openshift_variable': '3', 'openshift_other_variable': '7'}
+
+ hostvars=<ansible.vars.hostvars.HostVars object> (Mapping)
+ variables={'openshift_other_variable': '6'}
+ inventory_hostname='master1.example.com'
+ returns {'openshift_variable': '3', 'openshift_other_variable': '6'}
+ """
+ if not isinstance(hostvars, Mapping):
+ raise errors.AnsibleFilterError("|failed expects hostvars is dictionary or object")
+ if not isinstance(variables, dict):
+ raise errors.AnsibleFilterError("|failed expects variables is a dictionary")
+ if not isinstance(inventory_hostname, basestring):
+ raise errors.AnsibleFilterError("|failed expects inventory_hostname is a string")
+ # pylint: disable=no-member
+ ansible_version = pkg_resources.get_distribution("ansible").version
+ merged_hostvars = {}
+ if LooseVersion(ansible_version) >= LooseVersion('2.0.0'):
+ merged_hostvars = FilterModule.oo_merge_dicts(hostvars[inventory_hostname],
+ variables)
+ else:
+ merged_hostvars = FilterModule.oo_merge_dicts(hostvars[inventory_hostname],
+ hostvars)
+ return merged_hostvars
+
+ @staticmethod
def oo_collect(data, attribute=None, filters=None):
""" This takes a list of dict and collects all attributes specified into a
list. If filter is specified then we will include all items that
@@ -870,5 +908,6 @@ class FilterModule(object):
"oo_image_tag_to_rpm_version": self.oo_image_tag_to_rpm_version,
"oo_merge_dicts": self.oo_merge_dicts,
"oo_oc_nodes_matching_selector": self.oo_oc_nodes_matching_selector,
- "oo_oc_nodes_with_label": self.oo_oc_nodes_with_label
+ "oo_oc_nodes_with_label": self.oo_oc_nodes_with_label,
+ "oo_merge_hostvars": self.oo_merge_hostvars,
}