Last active
November 9, 2017 11:39
-
-
Save halberom/568a8af23d689fe5dd35673711b59215 to your computer and use it in GitHub Desktop.
ansible - example of pulling elements from list of dicts, into a list
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
FROM: | |
[{"id": 1, "field2": "23@46"}, {"id": 2, "field2": "33333@45"}, {"id": 3, "field2": "23444@4634234234"}] | |
TO: | |
[23, 1, 46, 33333, 2, 45, 23444, 3, 4634234234] |
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
--- | |
- hosts: localhost | |
gather_facts: False | |
connection: local | |
vars: | |
mylistofdicts: [{"id": 1, "field2": "23@46"}, {"id": 2, "field2": "33333@45"}, {"id": 3, "field2": "23444@4634234234"}] | |
tasks: | |
- set_fact: | |
mylist: "{{ mylist|default([]) + [ | |
item.field2|regex_replace('(.*)@(.*)', '\\1'), | |
item.id, | |
item.field2|regex_replace('(.*)@(.*)', '\\2') | |
] }}" | |
with_items: "{{ mylistofdicts }}" | |
- debug: | |
var: mylist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment