Ansible - Looping through a list of nested DictionariesWhat is the best way to iterate over a dictionary?What's the best way to break from nested (for) loops?Accessing the index in 'for' loops?How do I loop through or enumerate a JavaScript object?JavaScript closure inside loops – simple practical exampleHow do I break out of nested loops in Java?Iterate through a HashMapLooping through the content of a file in BashLoop through an array in JavaScriptIterate through object properties
How would a solely written language work mechanically
Echo with obfuscation
What is the smallest number n> 5 so that 5 ^ n ends with "3125"?
Would this string work as string?
Storage of electrolytic capacitors - how long?
"Oh no!" in Latin
Ways of geometrical multiplication
Is there a distance limit for minecart tracks?
Mimic lecturing on blackboard, facing audience
Did I make a mistake by ccing email to boss to others?
Determining multivariate least squares with constraint
Showing mass murder in a kid's book
What should be the ideal length of sentences in a blog post for ease of reading?
Should I assume I have passed probation?
How to preserve electronics (computers, iPads and phones) for hundreds of years
Proving an identity involving cross products and coplanar vectors
I'm just a whisper. Who am I?
How do I tell my boss that I'm quitting in 15 days (a colleague left this week)
In One Punch Man, is King actually weak?
How do I prevent inappropriate ads from appearing in my game?
How to leave product feedback on macOS?
What does the word 'upstream' mean in the context?
How to write Quadratic equation with negative coefficient
Confusion over Hunter with Crossbow Expert and Giant Killer
Ansible - Looping through a list of nested Dictionaries
What is the best way to iterate over a dictionary?What's the best way to break from nested (for) loops?Accessing the index in 'for' loops?How do I loop through or enumerate a JavaScript object?JavaScript closure inside loops – simple practical exampleHow do I break out of nested loops in Java?Iterate through a HashMapLooping through the content of a file in BashLoop through an array in JavaScriptIterate through object properties
I have some problems creating a loop that works for the following nested structures:
1st level:
DeviceList = [Device1, Device2, Device3, etc..]
2nd level: Each device is a dictionary structure as follows:
Device1 =
"description" : "string",
"Id": "Value",
"DeviceIPs": Dictionary
3rd level: DeviceIPs is a dictionary where the key is one of the IPs assigned to the device converted to a string.
"DeviceIPs" =
"IP1": "description": "string1", "interface": "eth0", "mask":"subnet_mask1" ,
"IP2": "description": "string2", "interface": "eth1", "mask":"subnet_mask2" ,
"IP3": "description": "string3", "interface": "eth2", "mask":"subnet_mask3" ,
etc..
Ideally, I need to create a loop for the the keys of "DeviceIPs" so:
"IP1"
"IP2"
"IP3"
possibly based on the inner value (for instance matching a when condition like interface == "eth1")
I have tried different ways but I really can't come out with anything that works.. any idea?
loops nested ansible nested-loops
add a comment |
I have some problems creating a loop that works for the following nested structures:
1st level:
DeviceList = [Device1, Device2, Device3, etc..]
2nd level: Each device is a dictionary structure as follows:
Device1 =
"description" : "string",
"Id": "Value",
"DeviceIPs": Dictionary
3rd level: DeviceIPs is a dictionary where the key is one of the IPs assigned to the device converted to a string.
"DeviceIPs" =
"IP1": "description": "string1", "interface": "eth0", "mask":"subnet_mask1" ,
"IP2": "description": "string2", "interface": "eth1", "mask":"subnet_mask2" ,
"IP3": "description": "string3", "interface": "eth2", "mask":"subnet_mask3" ,
etc..
Ideally, I need to create a loop for the the keys of "DeviceIPs" so:
"IP1"
"IP2"
"IP3"
possibly based on the inner value (for instance matching a when condition like interface == "eth1")
I have tried different ways but I really can't come out with anything that works.. any idea?
loops nested ansible nested-loops
1
Could you post the exact question? I guessjson_querywould fit, but I'm not sure, as you don't post what you really need.
– Konstantin Suvorov
Nov 27 '17 at 16:21
Hi Konstantin, I actually managed to make it work with json_query filter. My problem was to loop through the IPs used as keys in the DeviceIPs dict, given DeviceList. I changed the DeviceList to be a dictionary: DeviceDict and used json_query as follows: tasks: - debug: msg: "IP: item " with_items: " DeviceDict " This will loop through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs. Thanks!
– ValerioG
Nov 28 '17 at 17:15
add a comment |
I have some problems creating a loop that works for the following nested structures:
1st level:
DeviceList = [Device1, Device2, Device3, etc..]
2nd level: Each device is a dictionary structure as follows:
Device1 =
"description" : "string",
"Id": "Value",
"DeviceIPs": Dictionary
3rd level: DeviceIPs is a dictionary where the key is one of the IPs assigned to the device converted to a string.
"DeviceIPs" =
"IP1": "description": "string1", "interface": "eth0", "mask":"subnet_mask1" ,
"IP2": "description": "string2", "interface": "eth1", "mask":"subnet_mask2" ,
"IP3": "description": "string3", "interface": "eth2", "mask":"subnet_mask3" ,
etc..
Ideally, I need to create a loop for the the keys of "DeviceIPs" so:
"IP1"
"IP2"
"IP3"
possibly based on the inner value (for instance matching a when condition like interface == "eth1")
I have tried different ways but I really can't come out with anything that works.. any idea?
loops nested ansible nested-loops
I have some problems creating a loop that works for the following nested structures:
1st level:
DeviceList = [Device1, Device2, Device3, etc..]
2nd level: Each device is a dictionary structure as follows:
Device1 =
"description" : "string",
"Id": "Value",
"DeviceIPs": Dictionary
3rd level: DeviceIPs is a dictionary where the key is one of the IPs assigned to the device converted to a string.
"DeviceIPs" =
"IP1": "description": "string1", "interface": "eth0", "mask":"subnet_mask1" ,
"IP2": "description": "string2", "interface": "eth1", "mask":"subnet_mask2" ,
"IP3": "description": "string3", "interface": "eth2", "mask":"subnet_mask3" ,
etc..
Ideally, I need to create a loop for the the keys of "DeviceIPs" so:
"IP1"
"IP2"
"IP3"
possibly based on the inner value (for instance matching a when condition like interface == "eth1")
I have tried different ways but I really can't come out with anything that works.. any idea?
loops nested ansible nested-loops
loops nested ansible nested-loops
edited Mar 7 at 2:46
Cœur
19k9113155
19k9113155
asked Nov 27 '17 at 11:56
ValerioGValerioG
63
63
1
Could you post the exact question? I guessjson_querywould fit, but I'm not sure, as you don't post what you really need.
– Konstantin Suvorov
Nov 27 '17 at 16:21
Hi Konstantin, I actually managed to make it work with json_query filter. My problem was to loop through the IPs used as keys in the DeviceIPs dict, given DeviceList. I changed the DeviceList to be a dictionary: DeviceDict and used json_query as follows: tasks: - debug: msg: "IP: item " with_items: " DeviceDict " This will loop through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs. Thanks!
– ValerioG
Nov 28 '17 at 17:15
add a comment |
1
Could you post the exact question? I guessjson_querywould fit, but I'm not sure, as you don't post what you really need.
– Konstantin Suvorov
Nov 27 '17 at 16:21
Hi Konstantin, I actually managed to make it work with json_query filter. My problem was to loop through the IPs used as keys in the DeviceIPs dict, given DeviceList. I changed the DeviceList to be a dictionary: DeviceDict and used json_query as follows: tasks: - debug: msg: "IP: item " with_items: " DeviceDict " This will loop through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs. Thanks!
– ValerioG
Nov 28 '17 at 17:15
1
1
Could you post the exact question? I guess
json_query would fit, but I'm not sure, as you don't post what you really need.– Konstantin Suvorov
Nov 27 '17 at 16:21
Could you post the exact question? I guess
json_query would fit, but I'm not sure, as you don't post what you really need.– Konstantin Suvorov
Nov 27 '17 at 16:21
Hi Konstantin, I actually managed to make it work with json_query filter. My problem was to loop through the IPs used as keys in the DeviceIPs dict, given DeviceList. I changed the DeviceList to be a dictionary: DeviceDict and used json_query as follows: tasks: - debug: msg: "IP: item " with_items: " DeviceDict " This will loop through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs. Thanks!
– ValerioG
Nov 28 '17 at 17:15
Hi Konstantin, I actually managed to make it work with json_query filter. My problem was to loop through the IPs used as keys in the DeviceIPs dict, given DeviceList. I changed the DeviceList to be a dictionary: DeviceDict and used json_query as follows: tasks: - debug: msg: "IP: item " with_items: " DeviceDict " This will loop through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs. Thanks!
– ValerioG
Nov 28 '17 at 17:15
add a comment |
2 Answers
2
active
oldest
votes
Try reorganizing your variables to loop over eth1. For example:
---
- name: Test
hosts: localhost
connection: local
gather_facts: False
vars:
- DeviceList:
- Device:
description: string
Id: Value1
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
- Device:
description: string
Id: Value2
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
tasks:
- debug: var=item.1.eth1.ip
with_subelements:
- " DeviceList "
- Device.DeviceIPs
Hi Christina, Thanks for the advice but that structure does not really work for me becaue, in DeviceList, I need a device identifier (e.g. Device1, Device2, Device3, etc..) and not a fixed key value: "Device" and subelements does not work in that case..
– ValerioG
Nov 28 '17 at 10:33
add a comment |
Just for wider audience, I managed to do what I want in the end, using json_query filter.
I changed the fist level structure from a list (DeviceList) to a Dictionary (DeviceDict) and used the following json_query@
tasks:
- debug: msg: "IP: item "
with_items: " DeviceDict
This loops through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs.
thanks everyone!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47510439%2fansible-looping-through-a-list-of-nested-dictionaries%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try reorganizing your variables to loop over eth1. For example:
---
- name: Test
hosts: localhost
connection: local
gather_facts: False
vars:
- DeviceList:
- Device:
description: string
Id: Value1
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
- Device:
description: string
Id: Value2
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
tasks:
- debug: var=item.1.eth1.ip
with_subelements:
- " DeviceList "
- Device.DeviceIPs
Hi Christina, Thanks for the advice but that structure does not really work for me becaue, in DeviceList, I need a device identifier (e.g. Device1, Device2, Device3, etc..) and not a fixed key value: "Device" and subelements does not work in that case..
– ValerioG
Nov 28 '17 at 10:33
add a comment |
Try reorganizing your variables to loop over eth1. For example:
---
- name: Test
hosts: localhost
connection: local
gather_facts: False
vars:
- DeviceList:
- Device:
description: string
Id: Value1
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
- Device:
description: string
Id: Value2
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
tasks:
- debug: var=item.1.eth1.ip
with_subelements:
- " DeviceList "
- Device.DeviceIPs
Hi Christina, Thanks for the advice but that structure does not really work for me becaue, in DeviceList, I need a device identifier (e.g. Device1, Device2, Device3, etc..) and not a fixed key value: "Device" and subelements does not work in that case..
– ValerioG
Nov 28 '17 at 10:33
add a comment |
Try reorganizing your variables to loop over eth1. For example:
---
- name: Test
hosts: localhost
connection: local
gather_facts: False
vars:
- DeviceList:
- Device:
description: string
Id: Value1
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
- Device:
description: string
Id: Value2
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
tasks:
- debug: var=item.1.eth1.ip
with_subelements:
- " DeviceList "
- Device.DeviceIPs
Try reorganizing your variables to loop over eth1. For example:
---
- name: Test
hosts: localhost
connection: local
gather_facts: False
vars:
- DeviceList:
- Device:
description: string
Id: Value1
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
- Device:
description: string
Id: Value2
DeviceIPs:
- eth0: "description": "string1", "ip": "value1", "mask":"subnet_mask1"
- eth1: "description": "string2", "ip": "value2", "mask":"subnet_mask2"
- eth2: "description": "string3", "ip": "value3", "mask":"subnet_mask3"
tasks:
- debug: var=item.1.eth1.ip
with_subelements:
- " DeviceList "
- Device.DeviceIPs
answered Nov 27 '17 at 20:51
Christina AChristina A
1657
1657
Hi Christina, Thanks for the advice but that structure does not really work for me becaue, in DeviceList, I need a device identifier (e.g. Device1, Device2, Device3, etc..) and not a fixed key value: "Device" and subelements does not work in that case..
– ValerioG
Nov 28 '17 at 10:33
add a comment |
Hi Christina, Thanks for the advice but that structure does not really work for me becaue, in DeviceList, I need a device identifier (e.g. Device1, Device2, Device3, etc..) and not a fixed key value: "Device" and subelements does not work in that case..
– ValerioG
Nov 28 '17 at 10:33
Hi Christina, Thanks for the advice but that structure does not really work for me becaue, in DeviceList, I need a device identifier (e.g. Device1, Device2, Device3, etc..) and not a fixed key value: "Device" and subelements does not work in that case..
– ValerioG
Nov 28 '17 at 10:33
Hi Christina, Thanks for the advice but that structure does not really work for me becaue, in DeviceList, I need a device identifier (e.g. Device1, Device2, Device3, etc..) and not a fixed key value: "Device" and subelements does not work in that case..
– ValerioG
Nov 28 '17 at 10:33
add a comment |
Just for wider audience, I managed to do what I want in the end, using json_query filter.
I changed the fist level structure from a list (DeviceList) to a Dictionary (DeviceDict) and used the following json_query@
tasks:
- debug: msg: "IP: item "
with_items: " DeviceDict
This loops through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs.
thanks everyone!
add a comment |
Just for wider audience, I managed to do what I want in the end, using json_query filter.
I changed the fist level structure from a list (DeviceList) to a Dictionary (DeviceDict) and used the following json_query@
tasks:
- debug: msg: "IP: item "
with_items: " DeviceDict
This loops through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs.
thanks everyone!
add a comment |
Just for wider audience, I managed to do what I want in the end, using json_query filter.
I changed the fist level structure from a list (DeviceList) to a Dictionary (DeviceDict) and used the following json_query@
tasks:
- debug: msg: "IP: item "
with_items: " DeviceDict
This loops through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs.
thanks everyone!
Just for wider audience, I managed to do what I want in the end, using json_query filter.
I changed the fist level structure from a list (DeviceList) to a Dictionary (DeviceDict) and used the following json_query@
tasks:
- debug: msg: "IP: item "
with_items: " DeviceDict
This loops through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs.
thanks everyone!
answered Nov 28 '17 at 17:27
ValerioGValerioG
63
63
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47510439%2fansible-looping-through-a-list-of-nested-dictionaries%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Could you post the exact question? I guess
json_querywould fit, but I'm not sure, as you don't post what you really need.– Konstantin Suvorov
Nov 27 '17 at 16:21
Hi Konstantin, I actually managed to make it work with json_query filter. My problem was to loop through the IPs used as keys in the DeviceIPs dict, given DeviceList. I changed the DeviceList to be a dictionary: DeviceDict and used json_query as follows: tasks: - debug: msg: "IP: item " with_items: " DeviceDict " This will loop through all the possible device records in DeviceDict and get the keys in DeviceIPs which are indeed the IPs. Thanks!
– ValerioG
Nov 28 '17 at 17:15