title: Ansible 2 conditionals using with_items in loop
body: <p>I'm trying to run a few Ansible commands depending on the outcome of a conditional check in my tasks based on the presence of a boolean and am tying myself up in knots trying to get it to work.</p>
  
  <p>The desired outcome is as follows, and <strong>should be run for each item in the <code>hosts</code> array</strong>:</p>
  
  <ul>
  <li>Check if the <code>lynchburg</code> variable is <code>true</code> or <code>false</code> </li>
  <li>If the <code>lynchburg</code> variable is <code>true</code>:
  
  <ul>
  <li>Setup the folder structure <strong>if it doesn't already exist</strong></li>
  <li>Create <code>gulpfile.js</code> from the included template file <strong>if it doesn't already exist</strong></li>
  </ul></li>
  <li>If the <code>lynchburg</code> variable is <code>false</code>:
  
  <ul>
  <li>Don't setup the folder structure</li>
  <li>Don't create <code>gulpfile.js</code></li>
  </ul></li>
  <li>Regardless of whether the <code>lynchburg</code> is <code>true</code> or <code>false</code>, skip all tasks on subsequent reprovisions, as the folder structure and <code>gulpfile.js</code> will either be there or not (although this should be taken care of by the logic in the conditionals) </li>
  </ul>
  
  <p>From the code included below, I'm seeing the following results:</p>
  
  <ul>
  <li>When <code>lynchburg</code> is <code>true</code>, the provision runs through as expected
  the first time around and any subsequent reprovisions also work as
  expected.</li>
  <li>When <code>lynchburg</code> is <code>false</code>, the <code>inc</code> folder structure <strong>is</strong> created, however <code>gulpfile.js</code> <strong>isn't</strong>.</li>
  </ul>
  
  <p>The main loop the task should run through is the following:</p>
  
  <pre><code>vhosts:
    - site_name: sample
      lynchburg: true
  </code></pre>
  
  <p>and the tasks are as follows:</p>
  
  <pre><code># Create variable to check whether Lynchburg has already been installed
  - name: Check if Lynchburg assets folders have been previously created
    stat:
      path: /var/www/{{ item.site_name }}/inc
    with_items: "{{ vhosts }}"
    when: item.lynchburg == true
    register: lynchburg_assets
  
  - name: Check if Lynchburg gulpfile.js has been previously created
    stat:
      path: /var/www/{{ item.site_name }}/gulpfile.js
    with_items: "{{ vhosts }}"
    when: item.lynchburg == true
    register: lynchburg_gulpfile
  
  - name: Create inc folder
    with_items: "{{ lynchburg_assets.results }}"
    file:
      path: /var/www/{{ item.item.site_name }}/inc
      state: directory
    when: item.stat.isdir is undefined and item.item.lynchburg == true
  
  - name: Create scss folder
    with_items: "{{ lynchburg_assets.results }}"
    file:
      path: /var/www/{{ item.item.site_name }}/inc/scss
      state: directory
    when: item.stat.isdir is undefined and item.item.lynchburg == true
  
  - name: Create js folder
    with_items: "{{ lynchburg_assets.results }}"
    file:
      path: /var/www/{{ item.item.site_name }}/inc/js
      state: directory
    when: item.stat.isdir is undefined and item.item.lynchburg == true
  
  - name: Create img folder
    with_items: "{{ lynchburg_assets.results }}"
    file:
      path: /var/www/{{ item.item.site_name }}/inc/img
      state: directory
    when: item.stat.isdir is undefined and item.item.lynchburg == true
  
  - name: Create fonts folder
    with_items: "{{ lynchburg_assets.results }}"
    file:
      path: /var/www/{{ item.item.site_name }}/inc/fonts
      state: directory
    when: item.stat.isdir is undefined and item.item.lynchburg == true
  
  - name: Create gulpfile.js
    with_items: "{{ lynchburg_gulpfile.results }}"
    template:
      src: gulpfile.js
      dest: /var/www/{{ item.item.site_name }}/gulpfile.js
    when: item.stat.exists is defined and item.stat.exists == false and item.item.lynchburg == true
  </code></pre>
  
  <p><strong>N.B.</strong> If there's any other way to create the full folder structure easily without running five different tasks - one for each folder/subfolder - and someone could advise what that is, that'd also be appreciated!</p>