Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
53fd85e2
Commit
53fd85e2
authored
Jan 25, 2013
by
Daniel Hokka Zakrisson
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1903 from leucos/mergeable-hash-vars
Adds user-selectable hash merging support in vars
parents
6e0cb14b
8eb7d740
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
3 deletions
+43
-3
examples/ansible.cfg
+10
-0
lib/ansible/constants.py
+1
-0
lib/ansible/inventory/vars_plugins/group_vars.py
+11
-3
lib/ansible/utils/__init__.py
+21
-0
No files found.
examples/ansible.cfg
View file @
53fd85e2
...
...
@@ -76,6 +76,16 @@ remote_port=22
sudo_exe=sudo
# how to handle hash defined in several places
# hash can be merged, or replaced
# if you use replace, and have multiple hashes named 'x', the last defined
# will override the previously defined one
# if you use merge here, hash will cumulate their keys, but keys will still
# override each other
# replace is the default value, and is how ansible always handled hash variables
#
# hash_behaviour=replace
# if set, always use this private key file for authentication, same as if passing
# --private-key to ansible or ansible-playbook
...
...
lib/ansible/constants.py
View file @
53fd85e2
...
...
@@ -92,6 +92,7 @@ DEFAULT_MANAGED_STR = get_config(p, DEFAULTS, 'ansible_managed', None,
DEFAULT_SYSLOG_FACILITY
=
get_config
(
p
,
DEFAULTS
,
'syslog_facility'
,
'ANSIBLE_SYSLOG_FACILITY'
,
'LOG_USER'
)
DEFAULT_KEEP_REMOTE_FILES
=
get_config
(
p
,
DEFAULTS
,
'keep_remote_files'
,
'ANSIBLE_KEEP_REMOTE_FILES'
,
'0'
)
DEFAULT_SUDO_EXE
=
get_config
(
p
,
DEFAULTS
,
'sudo_exe'
,
'ANSIBLE_SUDO_EXE'
,
'sudo'
)
DEFAULT_HASH_BEHAVIOUR
=
get_config
(
p
,
DEFAULTS
,
'hash_behaviour'
,
'ANSIBLE_HASH_BEHAVIOUR'
,
'replace'
)
DEFAULT_ACTION_PLUGIN_PATH
=
shell_expand_path
(
get_config
(
p
,
DEFAULTS
,
'action_plugins'
,
None
,
'/usr/share/ansible_plugins/action_plugins'
))
DEFAULT_CALLBACK_PLUGIN_PATH
=
shell_expand_path
(
get_config
(
p
,
DEFAULTS
,
'callback_plugins'
,
None
,
'/usr/share/ansible_plugins/callback_plugins'
))
...
...
lib/ansible/inventory/vars_plugins/group_vars.py
View file @
53fd85e2
...
...
@@ -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
lib/ansible/utils/__init__.py
View file @
53fd85e2
...
...
@@ -19,6 +19,7 @@ import sys
import
os
import
shlex
import
yaml
import
copy
import
optparse
import
operator
from
ansible
import
errors
...
...
@@ -273,6 +274,26 @@ def parse_kv(args):
options
[
k
]
=
v
return
options
def
merge_hash
(
a
,
b
):
''' merges hash b into a
this means that if b has key k, the resulting has will have a key k
which value comes from b
said differently, all key/value combination from b will override a's '''
# and iterate over b keys
for
k
,
v
in
b
.
iteritems
():
if
k
in
a
and
isinstance
(
a
[
k
],
dict
):
# if this key is a hash and exists in a
# we recursively call ourselves with
# the key value of b
a
[
k
]
=
merge_hash
(
a
[
k
],
v
)
else
:
# k is not in a, no need to merge b, we just deecopy
# or k is not a dictionnary, no need to merge b either, we just deecopy it
a
[
k
]
=
v
# finally, return the resulting hash when we're done iterating keys
return
a
def
md5s
(
data
):
''' Return MD5 hex digest of data. '''
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment