title: Ansible - using with_items and when conditional to
body: <p>I have a bunch of servers that have four physical drives on them (/dev/sda, sdb, sdc, and sdd). sda has the OS installed on it.</p>
  
  <p>I need to format each drive except sda. I need to check if each drive has data on it.  If it does, then I shouldn't format it.</p>
  
  <pre><code># This will get all physical disks (sda, sdb, sdc, etc) and assign them to disk_var
  - name: Get disks
    set_fact: disk_var="{{hostvars[inventory_hostname]["ansible_devices"].keys()|list}}"
  
  - name: Check if the disk is partitioned and also ignore sda
    stat: path=/dev/{{item}}1
    with_items: disk_var
    when: item != 'sda'
    register: base_secondary_partition_{{item}}
  
  - name: Create GPT partition table
    command: /sbin/parted -s /dev/{{item}} mklabel gpt
    with_items: disk_var
    when: item != 'sda' and base_secondary_partition_{{item}}.stat.exists == false
  </code></pre>
  
  <p>There's clearly more steps involved into formatting these drives but it fails at the last task when creating the GPT partition table.</p>
  
  <p>Here's what it looks like when it runs.  You'll see that it fails at the last task:</p>
  
  <pre><code>TASK: [role | Get disks] ******************************************************
  ok: [server1.com]
  
  TASK: [role | Check if the disk is partitioned] *******************************
  skipping: [server1.com] =&gt; (item=sda)
  ok: [server1.com] =&gt; (item=sdd)
  ok: [server1.com] =&gt; (item=sdb)
  ok: [server1.com] =&gt; (item=sdc)
  
  TASK: [role | Create GPT partition table] *************************************
  fatal: [server1.com] =&gt; error while evaluating conditional: base_secondary_partition_sdd.stat.exists == false
  
  FATAL: all hosts have already failed -- aborting
  </code></pre>
  
  <p>Any idea how I can check the conditional <code>base_secondary_partition_{{item}}.stat.exists</code>? I need to make sure that if there's data on the drive, it will not format it.</p>