# Adding dict key to a list

**URL:** <https://forum.ansible.com/t/adding-dict-key-to-a-list/23075>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [November 22, 2016, 2:11pm UTC](https://forum.ansible.com/t/adding-dict-key-to-a-list/23075 "2016-11-22T14:11:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Hirsch](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@William\_Hirsch](https://forum.ansible.com/u/William_Hirsch)\
**Post date:** [November 22, 2016, 2:11pm UTC](https://forum.ansible.com/t/adding-dict-key-to-a-list/23075/1 "2016-11-22T14:11:23Z")

</div>

All,  
I need some help writing the following task.

1. Need to find all devices on a RHEL system, but only if they don’t match sr0
2. For each of those devices found, run a number of tasks against them

Thought this would be a simple matter of accessing items in a dict or list, using **when:** to match the devices I’m looking for, but I’m thoroughly lost.

Here is an example of the facts I’m looking for (I removed a lot of the elements for readability, denoted by the \<…\> )

“ansible\_devices”: {  
“sr0”: {  
“holders”: ,  
“host”: “”,  
\<…\>  
},  
“vda”: {  
“holders”: ,  
“host”: “”,  
\<…\>  
},

\<…\>

Now, here is what I want to do to each device I find that doesn’t match sr0. I’m struggling in figuring out how to set new\_dev

- name: Label new disk  
command: parted -s -a optimal “{{ new\_dev }}” mklabel gpt

I’ve tried the following to test loops and conditionals:

- name: print devices  
debug: msg=“Device name is “{{ item.key }}””  
with\_dict: “{{ ansible\_devices }}”

Which works, but when I add any number of attempts to apply conditionals, I have failure. Here is some of what I’ve tried, with the differences highlighted.

- name: print devices  
debug: msg=“Device name is “{{ item.key }}””  
with\_dict: “{{ ansible\_devices }}”

**when: ansible\_devices.key == “vdb”**

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 22, 2016, 6:18pm UTC](https://forum.ansible.com/t/adding-dict-key-to-a-list/23075/2 "2016-11-22T18:18:27Z")

</div>

when: item.key != "sr0"
