From 60c4e41791c9517ad7201c8c04f0fee2bff8873c Mon Sep 17 00:00:00 2001 From: Andrew Butcher Date: Thu, 28 Apr 2016 14:50:04 -0400 Subject: Replace hostvars with vars for openshift env facts when ansible >= v2. --- roles/openshift_builddefaults/tasks/main.yml | 4 +++- roles/openshift_cloud_provider/tasks/main.yml | 4 +++- roles/openshift_hosted_facts/tasks/main.yml | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/roles/openshift_builddefaults/tasks/main.yml b/roles/openshift_builddefaults/tasks/main.yml index c82aebe72..b5e93f8a2 100644 --- a/roles/openshift_builddefaults/tasks/main.yml +++ b/roles/openshift_builddefaults/tasks/main.yml @@ -5,7 +5,9 @@ # 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(hostvars) + # | oo_merge_dicts(vars if (ansible_version.full + # | version_compare('2.0', 'ge') + # | bool) else hostvars) # | 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 d7442924f..4924f051c 100644 --- a/roles/openshift_cloud_provider/tasks/main.yml +++ b/roles/openshift_cloud_provider/tasks/main.yml @@ -3,7 +3,9 @@ openshift_facts: role: cloudprovider openshift_env: "{{ hostvars[inventory_hostname] - | oo_merge_dicts(hostvars) + | oo_merge_dicts(vars if (ansible_version.full + | version_compare('2.0', 'ge') + | bool) else hostvars) | 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 f595e1e81..897fc0b35 100644 --- a/roles/openshift_hosted_facts/tasks/main.yml +++ b/roles/openshift_hosted_facts/tasks/main.yml @@ -3,5 +3,7 @@ openshift_facts: role: hosted openshift_env: "{{ hostvars[inventory_hostname] - | oo_merge_dicts(hostvars) + | oo_merge_dicts(vars if (ansible_version.full + | version_compare('2.0', 'ge') + | bool) else hostvars) | oo_openshift_env }}" -- cgit v1.2.3 From f0c412a9ab50a9030d51f83c459a66451a7c344a Mon Sep 17 00:00:00 2001 From: Andrew Butcher Date: Tue, 3 May 2016 16:56:15 -0400 Subject: Add oo_merge_hostvars filter for merging host & play variables. --- filter_plugins/oo_filters.py | 41 ++++++++++++++++++++++++++- roles/openshift_builddefaults/tasks/main.yml | 6 ++-- roles/openshift_cloud_provider/tasks/main.yml | 6 ++-- roles/openshift_hosted_facts/tasks/main.yml | 6 ++-- 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 @@ -70,6 +72,42 @@ class FilterModule(object): merged.update(second_dict) 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= (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 @@ -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 }}" -- cgit v1.2.3