Adds user-selectable hash merging support in vars

Hash variables are currently overriden if they are redefined. This
doesn't let the user refine hash entries or overriding selected keys,
which can, for some, be a desirable feature.
This patch let the user force hash merging by setting the
hash_behaviour value to "merge" (without the quotes) in ansible.cfg

However, by default, ansible behaves like it always did and if any value
besides "merge" is used ("replace" is suggested in the example ansible.cfg
file), it will also behave as always.
This commit is contained in:
Michel Blanc 2013-01-23 21:39:34 +01:00
parent 51180fa344
commit e28e538c6e
4 changed files with 45 additions and 3 deletions

View file

@ -19,6 +19,7 @@ import os
import glob
from ansible import errors
from ansible import utils
import ansible.constants as C
class VarsModule(object):
@ -48,7 +49,11 @@ class VarsModule(object):
data = utils.parse_yaml_from_file(path)
if type(data) != dict:
raise errors.AnsibleError("%s must be stored as a dictionary/hash" % path)
results.update(data)
if C.DEFAULT_HASH_BEHAVIOUR == "merge":
# let data content override results if needed
results = utils.merge_hash(results, data)
else:
results.update(data)
# load vars in playbook_dir/group_vars/name_of_host
path = os.path.join(basedir, "host_vars/%s" % host.name)
@ -56,7 +61,10 @@ class VarsModule(object):
data = utils.parse_yaml_from_file(path)
if type(data) != dict:
raise errors.AnsibleError("%s must be stored as a dictionary/hash" % path)
results.update(data)
if C.DEFAULT_HASH_BEHAVIOUR == "merge":
# let data content override results if needed
results = utils.merge_hash(results, data)
else:
results.update(data)
return results