Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active November 9, 2017 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halberom/568a8af23d689fe5dd35673711b59215 to your computer and use it in GitHub Desktop.
Save halberom/568a8af23d689fe5dd35673711b59215 to your computer and use it in GitHub Desktop.
ansible - example of pulling elements from list of dicts, into a list
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]
---
- 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