title: Ansible nested loops and conditional together in the same task
body: <pre><code>---
  
      - hosts: leaf-1, leaf-2
        vars_files:
          - /vars/all.yml
  
        tasks:
        - name: collect the vlan databse
          raw: "show vlan brief"
          register: vlan_db
  
        - name: compare vlan_id(.*)Ports against the vlan_db 
          set_fact:
            fact1: "{{ item.1.vlan_id }}"
            fact2: "{{ item.1.sap.inventory_hostname | join(', ') }}" 
            fact3: "{{ fact1 }}(.*){{ fact2 }}"
          failed_when: not vlan_db.stdout is regex(fact3)
          when: inventory_hostname in item.1.sap
          with_subelements:
            -  "{{ customers }}"
            -  services
  
  </code></pre>
  
  <ol>
  <li><p>I collect an output from a switch</p></li>
  <li><p>I want to loop through the "customer" list and check if the "inventory_hostname" is listed in the "services.sap"</p></li>
  </ol>
  
  <p>I thought I can do this with following:
  <code>when: inventory_hostname in item.1.sap</code></p>
  
  <ol start="3">
  <li><p>If the above check is true, I want to make a regex compare between fact3 and the stdout of the first task and raise a failure if the regex is not present in the stdout of the first task:</p>
  
  <p><code>set_fact:
     fact1: "{{ item.1.vlan_id }}"
     fact2: "{{ item.1.sap.inventory_hostname | join(', ') }}"
     fact3: "{{ fact1 }}(.*){{ fact2 }}"
   failed_when: not vlan_db.stdout is regex(fact3)</code></p></li>
  </ol>
  
  <p>This is like nested loops and conditional together. Is there a way to do this with Ansible?
  What I am doing wrong?</p>
  
  <p>Just to make sure that my intention is clear:</p>
  
  <ul>
  <li>loop through the items in the customers list</li>
  <li>loop through the items in the services list in each customer</li>
  <li>make a check if item.1.sap has a key which matches inventory_hostname</li>
  <li>if the above check is true, make a comparison between the regex pattern above and the output of the first task.</li>
  </ul>
  
  <p>/vars/all.yml looks like this: </p>
  
  <pre><code>
      customers:
          - name: "cust-1"
            l3_vni: "101"
            services:
              - vlan_id: "10"
                vni: "1010"
                gw: "10.0.0.254/24"
                sap: 
                  leaf-1: ["Eth3"]
                  leaf-2: ["Eth3"]
  
              - vlan_id: "11"
                vni: "1011"
                gw: "10.0.1.254/24"
                sap: 
                  leaf-1: ["Eth3"]
                  leaf-2: ["Eth3"]
  
          - name: "cust-2"
            l3_vni: "102"
            services:
              - vlan_id: "20"
                vni: "1020"
                gw: "20.0.0.254/24"
                sap: 
                  leaf-3: ["Eth3", "Eth4"]
                  leaf-4: ["Eth3"]
  
              - vlan_id: "21"
                vni: "1021"
                gw: "20.0.1.254/24"
                sap: 
                  leaf-3: ["Eth3"]
                  leaf-4: ["Eth3"]
      ```
  
      a switch vlan database usually looks like this:
  
      ```
      leaf-1#show vlan brief
  
      VLAN Name                             Status    Ports
      ---- -------------------------------- --------- ----------------------
      1    default                          active    Eth5
      10   cust-1                           active    Eth3, Eth4
      20   cust-2                           active    Eth1, Eth2
      ```
  
  error log:
  
  </code></pre>
  
  <p>fatal: [leaf-1]: FAILED! => {
      "msg": "The task includes an option with an undefined variable. The error was: 'fact1' is undefined\n\nThe error appears to be in '/vagrant/Ansible Folder Setup/NetAutHardWay/Step4-CICD/vlan_test.yml': line 23, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: compare vlan_id(.*)Ports against the vlan_db\n      ^ here\n"
  }</p>
  
  <pre><code>
  </code></pre>