Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
configuration
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
configuration
Commits
21832855
Commit
21832855
authored
Sep 16, 2014
by
e0d
Committed by
Feanil Patel
May 11, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ansible module for running functions over input from plays
parent
559404d0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
128 additions
and
0 deletions
+128
-0
playbooks/library/util_map
+128
-0
No files found.
playbooks/library/util_map
0 → 100755
View file @
21832855
#!/usr/bin/env python
DOCUMENTATION
=
"""
---
module: util_map
short_description: Applies a function to input and returns the result with the key function_output
description:
- Applies functions to data structures returning a new data structure.
version_added: "1.8"
author: Edward Zarecor
options:
function:
description:
- The function to apply, currently ['zipToDict','flatten']
required: true
input:
description:
- The input
required: true
args
description:
- Arguments to the function other than the input, varies by function.
"""
EXAMPLES
=
'''
- name: Apply function to results from ec2_scaling_policies
util_map:
function: 'zipToDict'
input: "{{ created_policies.results }}"
args:
- "name"
- "arn"
register: policy_data
- name: Apply function to policy data
util_map:
function: 'flatten'
input:
- 'a'
- 'b'
- 'c'
- ['d','e','f']
register: flat_list
'''
from
ansible.module_utils.basic
import
*
import
ast
import
itertools
class
ArgumentError
(
Exception
):
pass
def
flatten
(
module
,
input
):
"""
Takes an iterable and returns a flat list
With the input of
[['a','b','c'],'d',['e','f','g'],{'a','b'}]
this function will return
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'a', 'b']
:param module: The ansible module
:param input: An iterable
:return: A flat list.
"""
try
:
flat
=
list
(
itertools
.
chain
.
from_iterable
(
input
))
except
TypeError
as
te
:
raise
ArgumentError
(
"Flatten resulted in a type error, {0}."
.
format
(
te
.
message
))
module
.
exit_json
(
function_output
=
flat
)
def
zipToDict
(
module
,
input
,
key_key
,
value_key
):
"""
Takes an array of dicts and flattens it to a single dict by extracting the values
of a provided key as the keys in the new dict and the values of the second
provided key as the corresponding values.
For example, the input dict of
[{'name':'fred', 'id':'123'},['name':'bill', 'id':'321'}]
with an args array of ['id','name']
would return
{'123':'fred','321':'bill'}
:param input: an array of dicts, typically the results of an ansible module
:param key_key: a key into the input dict returning a value to be used as a key in the flattened dict
:param value_key: a key into the input dict returning a value to be used as a value in the flattened dict
:return: the flattened dict
"""
results
=
{}
for
item
in
input
:
results
[
item
[
unicode
(
key_key
)]]
=
item
[
unicode
(
value_key
)]
module
.
exit_json
(
function_output
=
results
)
def
main
():
arg_spec
=
dict
(
function
=
dict
(
required
=
True
,
choices
=
[
'zipToDict'
,
'flatten'
]),
input
=
dict
(
required
=
True
,
type
=
'str'
),
args
=
dict
(
required
=
False
,
type
=
'list'
),
)
module
=
AnsibleModule
(
argument_spec
=
arg_spec
,
supports_check_mode
=
False
)
target
=
module
.
params
.
get
(
'function'
)
# reify input data
input
=
ast
.
literal_eval
(
module
.
params
.
get
(
'input'
))
args
=
module
.
params
.
get
(
'args'
)
if
target
==
'zipToDict'
:
zipToDict
(
module
,
input
,
*
args
)
elif
target
==
'flatten'
:
flatten
(
module
,
input
)
else
:
raise
NotImplemented
(
"Function {0} is not implemented."
.
format
(
target
))
main
()
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