Adding dict key to a list

17 views
Skip to first unread message

William Hirsch

unread,
Nov 22, 2016, 12:07:15 PM11/22/16
to Ansible Project
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"
-------------------------------------------------------------------------------
- name: print devices
  debug: msg="Device name is "{{ item.key }}""
  with_dict: "{{ ansible_devices }}"
  when: ansible_devices['key'] == "vdb"
------------------------------------------------------------------------------
- name: print devices
  debug: msg="Device name is "{{ item.key }}""
  with_dict: "{{ ansible_devices }}"
  when: ansible_devices | search('vdb')


Kai Stian Olstad

unread,
Nov 22, 2016, 1:19:02 PM11/22/16
to ansible...@googlegroups.com
On 22. nov. 2016 15:11, William Hirsch wrote:
> 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"*

when: item.key != "sr0"

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages