title: Ansible: How to use "and" and "or" conditions in when clause for
body: <p>The following task should be executed if 3 AND conditions in when clause are meet. One of the AND conditions has an OR conditional inside. There is an error while evaluating OR clause. Could you please inform why the OR conditional is not working?</p>
  <pre><code>***- name: &quot;invoke playbook for control tables update&quot;
    include_tasks: sdp_purge_control_tbl.yml
    when:
      - job_exec_state.stdout.find('OK') == -1
      - data_in_main_a.stdout.find('created')!= -1
      - (rename_syn_result_chk_b.stdout.find('successfully')!= -1 or rename_syn_result_chk_a.stdout.find('successfully')!= -1)
    ignore_errors: True***
  </code></pre>
  <p>ERROR CODE:</p>
  <pre><code>***
  fatal: [sdp-test1a]: FAILED! =&gt; {}
  MSG:
  The conditional check 'rename_syn_result_chk_b.stdout.find('successfully')!= -1 or rename_syn_result_chk_a.stdout.find('successfully')!= -1' failed. The error was: error while evaluating conditional (rename_syn_result_chk_b.stdout.find('successfully')!= -1 or rename_syn_result_chk_a.stdout.find('successfully')!= -1): 'dict object' has no attribute 'stdout'
  The error appears to be in '/home/devops/ansible/roles/sdp/tasks/sdp_purge_scenario1.yml': line 125, column 7, but may
  be elsewhere in the file depending on the exact syntax problem.
  The offending line appears to be:
      - name: &quot;invoke playbook for control tables update&quot;
        ^ here
  ...ignoring***
  </code></pre>
  <p>Conditions that work ok:</p>
  <pre><code>job_exec_state.stdout.find('OK') == -1 
  data_in_main_a.stdout.find('created')!= -1
  </code></pre>
  <p>Condition that fails:</p>
  <pre><code>(rename_syn_result_chk_b.stdout.find('successfully')!= -1 or rename_syn_result_chk_a.stdout.find('successfully')!= -1)
  </code></pre>
  <p>If rename_syn_result_chk_b contains 'successfully' task runs ok because it is before the OR conditional, but if rename_syn_result_chk_b does not contain the string and then rename_syn_result_chk_a contains 'successfully', task still fails.</p>
  <p>I have tried with  and without parenthesis but no luck. OR clause alternatives attempted:</p>
  <pre><code>(rename_syn_result_chk_b.stdout.find('successfully')!= -1) or (rename_syn_result_chk_a.stdout.find('successfully')!= -1)
  
  rename_syn_result_chk_b.stdout.find('successfully')!= -1 or rename_syn_result_chk_a.stdout.find('successfully')!= -1
  
  (rename_syn_result_chk_b.stdout.find('successfully')!= -1 or rename_syn_result_chk_a.stdout.find('successfully')!= -1)
  </code></pre>
  <p>Code to understand the variables... it is basically an output of a grep from a file that is later used to find &quot;successfully&quot; string.</p>
  <pre><code>   - name: &quot;check rename to synonym A&quot;
        shell: |
          grep '{{ item.synA_name }}' `ls {{sql_log_dir}}/Rename_Hist2Syn*|tail -1`
        register: rename_syn_result_chk_a
  
      - name: &quot;check rename to synonym B&quot;
        shell: |
          grep  '{{ item.synB_name }}' `ls {{sql_log_dir}}/Rename_Hist2Syn*|tail -1`
        register: rename_syn_result_chk_b
  </code></pre>