summaryrefslogtreecommitdiff
path: root/lib/ansible
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2021-06-22 12:59:53 +0900
committerGitHub <noreply@github.com>2021-06-22 09:29:53 +0530
commit8e755707b9fd418ca2a097ce52ba8849c0181c14 (patch)
tree362d66eb8e89946587f8f921c58e2a7ea70dac13 /lib/ansible
parentc404a9003fbfc56785d32e3f6e6ab005d0467927 (diff)
downloadansible-8e755707b9fd418ca2a097ce52ba8849c0181c14.tar.gz
Add 'hash_behaviour' option to include_vars module (#72944)
Diffstat (limited to 'lib/ansible')
-rw-r--r--lib/ansible/modules/include_vars.py8
-rw-r--r--lib/ansible/plugins/action/include_vars.py10
-rw-r--r--lib/ansible/utils/vars.py4
3 files changed, 19 insertions, 3 deletions
diff --git a/lib/ansible/modules/include_vars.py b/lib/ansible/modules/include_vars.py
index 019d74680a..b1df96413c 100644
--- a/lib/ansible/modules/include_vars.py
+++ b/lib/ansible/modules/include_vars.py
@@ -70,6 +70,14 @@ options:
type: bool
default: no
version_added: "2.7"
+ hash_behaviour:
+ description:
+ - If set to C(merge), merges existing hash variables instead of overwriting them.
+ - If omitted C(null), the behavior falls back to the global I(hash_behaviour) configuration.
+ default: null
+ type: str
+ choices: ["replace", "merge"]
+ version_added: "2.12"
free-form:
description:
- This module allows you to specify the 'file' option directly without any other options.
diff --git a/lib/ansible/plugins/action/include_vars.py b/lib/ansible/plugins/action/include_vars.py
index 07234537ac..aec0f00443 100644
--- a/lib/ansible/plugins/action/include_vars.py
+++ b/lib/ansible/plugins/action/include_vars.py
@@ -7,10 +7,12 @@ __metaclass__ = type
from os import path, walk
import re
+import ansible.constants as C
from ansible.errors import AnsibleError
from ansible.module_utils.six import string_types
from ansible.module_utils._text import to_native, to_text
from ansible.plugins.action import ActionBase
+from ansible.utils.vars import combine_vars
class ActionModule(ActionBase):
@@ -20,7 +22,7 @@ class ActionModule(ActionBase):
VALID_FILE_EXTENSIONS = ['yaml', 'yml', 'json']
VALID_DIR_ARGUMENTS = ['dir', 'depth', 'files_matching', 'ignore_files', 'extensions', 'ignore_unknown_extensions']
VALID_FILE_ARGUMENTS = ['file', '_raw_params']
- VALID_ALL = ['name']
+ VALID_ALL = ['name', 'hash_behaviour']
def _set_dir_defaults(self):
if not self.depth:
@@ -46,6 +48,7 @@ class ActionModule(ActionBase):
def _set_args(self):
""" Set instance variables based on the arguments that were passed """
+ self.hash_behaviour = self._task.args.get('hash_behaviour', None)
self.return_results_as_name = self._task.args.get('name', None)
self.source_dir = self._task.args.get('dir', None)
self.source_file = self._task.args.get('file', None)
@@ -135,6 +138,11 @@ class ActionModule(ActionBase):
if failed:
result['failed'] = failed
result['message'] = err_msg
+ elif self.hash_behaviour is not None and self.hash_behaviour != C.DEFAULT_HASH_BEHAVIOUR:
+ merge_hashes = self.hash_behaviour == 'merge'
+ for key, value in results.items():
+ old_value = task_vars.get(key, None)
+ results[key] = combine_vars(old_value, value, merge=merge_hashes)
result['ansible_included_var_files'] = self.included_files
result['ansible_facts'] = results
diff --git a/lib/ansible/utils/vars.py b/lib/ansible/utils/vars.py
index 17ef2df9de..7545f1d435 100644
--- a/lib/ansible/utils/vars.py
+++ b/lib/ansible/utils/vars.py
@@ -79,12 +79,12 @@ def _validate_mutable_mappings(a, b):
)
-def combine_vars(a, b):
+def combine_vars(a, b, merge=None):
"""
Return a copy of dictionaries of variables based on configured hash behavior
"""
- if C.DEFAULT_HASH_BEHAVIOUR == "merge":
+ if merge or merge is None and C.DEFAULT_HASH_BEHAVIOUR == "merge":
return merge_hash(a, b)
else:
# HASH_BEHAVIOUR == 'replace'