title: Ansible Conditional - Compare registered variable to a value
body: <p>I need your help since I'm at a complete loss here.
  We started checking Ansible as a configuration manager (the AWX Project). We are currently based on a Chef Infra configuration manager.
  In chef when you want to control a resource from happening you can use <code>only_if</code> or <code>not_if</code></p>
  <pre><code>filesystem &quot;Test mount&quot; do
      fstype &quot;xfs&quot;
      mount &quot;/tmp/test&quot;
      device &quot;/dev/sda&quot;
      action [:create, :enable]
      not_if { File.exists?(&quot;/tmp/test&quot;) }
  end
  </code></pre>
  <p>After checking in Ansible I saw that conditionals are the equivalent for Chef guards.</p>
  <p>My problem is this:
  In my example I'm trying to see if a service exists and if it does, do action A, if not, do action B.
  This is my playbook:</p>
  <pre><code>---
  - name: Test of powershell
    hosts: all
    tasks:
    - name: test if ABCD service exists
      win_shell: |
        $a = Get-Service -Name ABCD -ErrorAction SilentlyContinue
        if ($a) {
          return $(Write-Host -NoNewline $true)
        }
        return $(Write-Host -NoNewline $false)
      register: ABCDExists
    
    - name: print ABCDExists value
      debug:
        var: ABCDExists
        verbosity: 3
    
    - name: Write service info if service exists
      win_shell: |
        $a = Get-Service -Name ABCD
        New-Item -Path c:\temp\ -Name &quot;text.txt&quot; -ItemType File -Force
        Set-Content -Path C:\temp\test.txt -Value &quot;$($a | select-object -property *)&quot;
      when: ABCDExists is &quot;True&quot;
  
    - name: Write service doesn't exists
      win_shell: |
        New-Item -Path c:\temp\ -Name &quot;text.txt&quot; -ItemType File -Force
        Set-Content -Path C:\temp\test.txt -Value &quot;Service ABCD doesn't exists&quot;
      when: ABCDExists is &quot;False&quot;
  
  </code></pre>
  <p>As you can see, the latest try for the conditional is <code>ABCDExists is &quot;True&quot;</code>\ <code>ABCDExists is &quot;False&quot;</code>.</p>
  <p>This if after I tried doing:
  <code>ABCDExists == False</code>
  <code>ABCDExists == &quot;False&quot;</code>
  <code>ABCDExists|string() == &quot;False&quot;</code></p>
  <p>I really at a loss here, I cannot understand where to look in Jinja documentation for the comparison operators or such.</p>
  <p>Any help would be greatly appreciated.</p>
  <p>UPDATE:
  After testing <code>ABCDExists | bool</code> I managed to skip one of the tasks, but not the one I expected.</p>
  <p>These are the logs from the running job (After the change):</p>
  <pre><code>ansible-playbook 2.9.10
    config file = /etc/ansible/ansible.cfg
    configured module search path = ['/var/lib/awx/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/lib/python3.6/site-packages/ansible
    executable location = /usr/bin/ansible-playbook
    python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
  Using /etc/ansible/ansible.cfg as config file
  SSH password: 
  host_list declined parsing /tmp/awx_183_ngdrbth5/tmph5taum3q as it did not pass its verify_file() method
  Parsed /tmp/awx_183_ngdrbth5/tmph5taum3q inventory source with script plugin
  
  PLAYBOOK: powershell_test.yml **************************************************
  1 plays in powershell_test.yml
  
  PLAY [Test of powershell] ******************************************************
  
  TASK [Gathering Facts] *********************************************************
  task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:2
  Using module file /usr/lib/python3.6/site-packages/ansible/modules/windows/setup.ps1
  Pipelining is enabled.
  &lt;server01&gt; ESTABLISH WINRM CONNECTION FOR USER: User@Domain.com on PORT 5985 TO server01
  EXEC (via pipeline wrapper)
  ok: [server01]
  META: ran handlers
  
  TASK [test if ABCD service exists] *****************************************
  task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:5
  Using module file /usr/lib/python3.6/site-packages/ansible/modules/windows/win_shell.ps1
  Pipelining is enabled.
  &lt;server01&gt; ESTABLISH WINRM CONNECTION FOR USER: User@Domain.com on PORT 5985 TO server01
  EXEC (via pipeline wrapper)
  changed: [server01] =&gt; {
      &quot;changed&quot;: true,
      &quot;cmd&quot;: &quot;$a = Get-Service -Name ABCD -ErrorAction SilentlyContinue\\nif ($a) {\\n  return $(Write-Host -NoNewline $true)\\n}\\nreturn $(Write-Host -NoNewline $false)&quot;,
      &quot;delta&quot;: &quot;0:00:00.484379&quot;,
      &quot;end&quot;: &quot;2020-09-01 01:34:23.412182&quot;,
      &quot;rc&quot;: 0,
      &quot;start&quot;: &quot;2020-09-01 01:34:22.927802&quot;,
      &quot;stderr&quot;: &quot;&quot;,
      &quot;stderr_lines&quot;: [],
      &quot;stdout&quot;: &quot;True&quot;,
      &quot;stdout_lines&quot;: [
          &quot;True&quot;
      ]
  }
  
  TASK [print ABCDExists value] **********************************************
  task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:14
  ok: [server01] =&gt; {
      &quot;ABCDExists&quot;: {
          &quot;changed&quot;: true,
          &quot;cmd&quot;: &quot;$a = Get-Service -Name ABCD -ErrorAction SilentlyContinue\\nif ($a) {\\n  return $(Write-Host -NoNewline $true)\\n}\\nreturn $(Write-Host -NoNewline $false)&quot;,
          &quot;delta&quot;: &quot;0:00:00.484379&quot;,
          &quot;end&quot;: &quot;2020-09-01 01:34:23.412182&quot;,
          &quot;failed&quot;: false,
          &quot;rc&quot;: 0,
          &quot;start&quot;: &quot;2020-09-01 01:34:22.927802&quot;,
          &quot;stderr&quot;: &quot;&quot;,
          &quot;stderr_lines&quot;: [],
          &quot;stdout&quot;: &quot;True&quot;,
          &quot;stdout_lines&quot;: [
              &quot;True&quot;
          ]
      }
  }
  
  TASK [Write service info if service exists] ************************************
  task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:19
  skipping: [server01] =&gt; {
      &quot;changed&quot;: false,
      &quot;skip_reason&quot;: &quot;Conditional result was False&quot;
  }
  
  TASK [Write service doesn't exists] ********************************************
  task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:26
  Using module file /usr/lib/python3.6/site-packages/ansible/modules/windows/win_shell.ps1
  Pipelining is enabled.
  &lt;server01&gt; ESTABLISH WINRM CONNECTION FOR USER: User@Domain.com on PORT 5985 TO server01
  EXEC (via pipeline wrapper)
  changed: [server01] =&gt; {
      &quot;changed&quot;: true,
      &quot;cmd&quot;: &quot;New-Item -Path c:\\\\temp\\\\ -Name \\&quot;text.txt\\&quot; -ItemType File -Force\\nSet-Content -Path C:\\\\temp\\\\test.txt -Value \\&quot;Service ABCD doesn't exists\\&quot;&quot;,
      &quot;delta&quot;: &quot;0:00:00.531259&quot;,
      &quot;end&quot;: &quot;2020-09-01 01:34:26.380055&quot;,
      &quot;rc&quot;: 0,
      &quot;start&quot;: &quot;2020-09-01 01:34:25.848796&quot;,
      &quot;stderr&quot;: &quot;&quot;,
      &quot;stderr_lines&quot;: [],
      &quot;stdout&quot;: &quot;\\r\\n\\r\\n    Directory: C:\\\\temp\\r\\n\\r\\n\\r\\nMode                LastWriteTime         Length Name                                                                  \\r\\n----                -------------         ------ ----                                                                  \\r\\n-a----         9/1/2020   9:34 AM              0 text.txt                                                              \\r\\n\\r\\n\\r\\n&quot;,
      &quot;stdout_lines&quot;: [
          &quot;&quot;,
          &quot;&quot;,
          &quot;    Directory: C:\\\\temp&quot;,
          &quot;&quot;,
          &quot;&quot;,
          &quot;Mode                LastWriteTime         Length Name                                                                  &quot;,
          &quot;----                -------------         ------ ----                                                                  &quot;,
          &quot;-a----         9/1/2020   9:34 AM              0 text.txt                                                              &quot;,
          &quot;&quot;,
          &quot;&quot;
      ]
  }
  META: ran handlers
  META: ran handlers
  
  PLAY RECAP *********************************************************************
  server01    : ok=4    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
  
  
  </code></pre>