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
465f3b1c
Commit
465f3b1c
authored
Oct 31, 2013
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
This allows type=dict in a module to allow passing in a real dict or JSON.
parent
b5209028
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
2 deletions
+46
-2
lib/ansible/module_utils/basic.py
+46
-2
No files found.
lib/ansible/module_utils/basic.py
View file @
465f3b1c
...
...
@@ -616,6 +616,39 @@ class AnsibleModule(object):
else
:
self
.
fail_json
(
msg
=
"internal error: do not know how to interpret argument_spec"
)
def
safe_eval
(
self
,
str
,
locals
=
None
,
include_exceptions
=
False
):
# do not allow method calls to modules
if
not
isinstance
(
str
,
basestring
):
# already templated to a datastructure, perhaps?
if
include_exceptions
:
return
(
str
,
None
)
return
str
if
re
.
search
(
r'\w\.\w+\('
,
str
):
if
include_exceptions
:
return
(
str
,
None
)
return
str
# do not allow imports
if
re
.
search
(
r'import \w+'
,
str
):
if
include_exceptions
:
return
(
str
,
None
)
return
str
try
:
result
=
None
if
not
locals
:
result
=
eval
(
str
)
else
:
result
=
eval
(
str
,
None
,
locals
)
if
include_exceptions
:
return
(
result
,
None
)
else
:
return
result
except
Exception
,
e
:
if
include_exceptions
:
return
(
str
,
e
)
return
str
def
_check_argument_types
(
self
):
''' ensure all arguments have the requested type '''
for
(
k
,
v
)
in
self
.
argument_spec
.
iteritems
():
...
...
@@ -640,7 +673,18 @@ class AnsibleModule(object):
elif
wanted
==
'dict'
:
if
not
isinstance
(
value
,
dict
):
if
isinstance
(
value
,
basestring
):
self
.
params
[
k
]
=
dict
([
x
.
split
(
"="
,
1
)
for
x
in
value
.
split
(
","
)])
if
value
.
startswith
(
"{"
):
try
:
self
.
params
[
k
]
=
json
.
loads
(
value
)
except
:
(
result
,
exc
)
=
self
.
safe_eval
(
value
,
dict
(),
include_exceptions
=
True
)
if
exc
is
not
None
:
self
.
fail_json
(
msg
=
"unable to evaluate dictionary for
%
s"
%
k
)
self
.
params
[
k
]
=
result
elif
'='
in
value
:
self
.
params
[
k
]
=
dict
([
x
.
split
(
"="
,
1
)
for
x
in
value
.
split
(
","
)])
else
:
self
.
fail_json
(
msg
=
"dictionary requested, could not parse JSON or key=value"
)
else
:
is_invalid
=
True
elif
wanted
==
'bool'
:
...
...
@@ -682,7 +726,7 @@ class AnsibleModule(object):
try
:
(
k
,
v
)
=
x
.
split
(
"="
,
1
)
except
Exception
,
e
:
self
.
fail_json
(
msg
=
"this module requires key=value arguments (
%
s)"
%
items
)
self
.
fail_json
(
msg
=
"this module requires key=value arguments (
%
s)"
%
(
items
)
)
params
[
k
]
=
v
params2
=
json
.
loads
(
MODULE_COMPLEX_ARGS
)
params2
.
update
(
params
)
...
...
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