summaryrefslogtreecommitdiffstats
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
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.
-rw-r--r--filter_plugins/oo_filters.py41
-rw-r--r--roles/openshift_builddefaults/tasks/main.yml6
-rw-r--r--roles/openshift_cloud_provider/tasks/main.yml6
-rw-r--r--roles/openshift_hosted_facts/tasks/main.yml6
4 files changed, 46 insertions, 13 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,
}
diff --git a/roles/openshift_builddefaults/tasks/main.yml b/roles/openshift_builddefaults/tasks/main.yml
index b5e93f8a2..6a4e919e8 100644
--- a/roles/openshift_builddefaults/tasks/main.yml
+++ b/roles/openshift_builddefaults/tasks/main.yml
@@ -4,10 +4,8 @@
role: builddefaults
# TODO: add ability to define builddefaults env vars sort of like this
# may need to move the config generation to a filter however.
- # openshift_env: "{{ hostvars[inventory_hostname]
- # | oo_merge_dicts(vars if (ansible_version.full
- # | version_compare('2.0', 'ge')
- # | bool) else hostvars)
+ # openshift_env: "{{ hostvars
+ # | oo_merge_hostvars(vars, inventory_hostname)
# | oo_openshift_env }}"
# openshift_env_structures:
# - 'openshift.builddefaults.env.*'
diff --git a/roles/openshift_cloud_provider/tasks/main.yml b/roles/openshift_cloud_provider/tasks/main.yml
index 4924f051c..6111d1207 100644
--- a/roles/openshift_cloud_provider/tasks/main.yml
+++ b/roles/openshift_cloud_provider/tasks/main.yml
@@ -2,10 +2,8 @@
- name: Set cloud provider facts
openshift_facts:
role: cloudprovider
- openshift_env: "{{ hostvars[inventory_hostname]
- | oo_merge_dicts(vars if (ansible_version.full
- | version_compare('2.0', 'ge')
- | bool) else hostvars)
+ openshift_env: "{{ hostvars
+ | oo_merge_hostvars(vars, inventory_hostname)
| oo_openshift_env }}"
openshift_env_structures:
- 'openshift.cloudprovider.aws.*'
diff --git a/roles/openshift_hosted_facts/tasks/main.yml b/roles/openshift_hosted_facts/tasks/main.yml
index 897fc0b35..2a11e6cbd 100644
--- a/roles/openshift_hosted_facts/tasks/main.yml
+++ b/roles/openshift_hosted_facts/tasks/main.yml
@@ -2,8 +2,6 @@
- name: Set hosted facts
openshift_facts:
role: hosted
- openshift_env: "{{ hostvars[inventory_hostname]
- | oo_merge_dicts(vars if (ansible_version.full
- | version_compare('2.0', 'ge')
- | bool) else hostvars)
+ openshift_env: "{{ hostvars
+ | oo_merge_hostvars(vars, inventory_hostname)
| oo_openshift_env }}"