-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sihui Han <[email protected]>
- Loading branch information
Sihui Han
committed
Feb 6, 2018
1 parent
2746f16
commit 9b62f0f
Showing
6 changed files
with
435 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/python | ||
from netaddr import * | ||
import sys | ||
import ipaddress | ||
|
||
DOCUMENTATION = ''' | ||
module: combine_list_to_dict | ||
version_added: "1.0" | ||
short_description: Combine a list of key and list of value to a key-value dictionary | ||
description: | ||
- Generate the dictionary based on key and value list | ||
- key and value are 1:1 mapping in sequence, and values with the same key will combined into a list. | ||
- Generated dict will be inserted into the 'combined_dict' key | ||
options: | ||
keys: | ||
description: | ||
- the required key list | ||
required: true | ||
values: | ||
description: | ||
- the required value list. | ||
required: true | ||
''' | ||
|
||
EXAMPLES = ''' | ||
- name: Combine list to dict | ||
get_ip_in_range: keys={{keys}} values={{values}} | ||
''' | ||
|
||
|
||
class CombineListModule(object): | ||
def __init__(self): | ||
self.module = AnsibleModule( | ||
argument_spec=dict( | ||
keys=dict(required=True, type='list'), | ||
values=dict(required=True, type='list'), | ||
), | ||
supports_check_mode=True) | ||
|
||
self.out = None | ||
self.facts = {} | ||
|
||
return | ||
|
||
def run(self): | ||
""" | ||
Main method of the class | ||
""" | ||
m_args = self.module.params | ||
keys = m_args['keys'] | ||
values = m_args['values'] | ||
combined_dict = {} | ||
for key, value in zip(keys, values): | ||
if key not in combined_dict: | ||
combined_dict[key] = [value] | ||
else: | ||
combined_dict[key].append(value) | ||
self.facts['combined_dict'] = combined_dict | ||
print "get the combined dict" | ||
self.module.exit_json(ansible_facts=self.facts) | ||
|
||
|
||
def main(): | ||
combine_list = CombineListModule() | ||
combine_list.run() | ||
|
||
return | ||
|
||
|
||
from ansible.module_utils.basic import * | ||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.