title: How to access status in ansible_facts.services?
body: <p>I am trying to Stop and/or Disable a systemd service if it is running and/or enabled on the remote host.</p>
  <p>Contents of tasks/main.yml:</p>
  <pre><code>---
  - name: Populate service facts
    service_facts:
  
  - name: Display selected service
    debug:
      var: ansible_facts.services[serviceToDisplay]
    loop: &quot;{{ disable_services }}&quot;
    loop_control:
      loop_var: serviceToDisplay
    vars:
      disable_services:
        - cups.service
        - cups
  
  - name: Stop and Disable service if it is running or enabled
    systemd:
      name: cups.service
      enabled: false
      state: stopped
    when:
      - &quot;serviceToDisable is defined&quot;
      - &quot;serviceToDisable.status == 'enabled'&quot;
    loop: &quot;{{ disable_services }}&quot;
    loop_control:
      loop_var: serviceToDisable
    vars:
      disable_services:
        - cups.service
        - cups
    become: true
  ...
  </code></pre>
  <p>Results:</p>
  <pre><code>PLAY [configServerGroup] *************************************************************************************************************************************
  
  TASK [Gathering Facts] ***************************************************************************************************************************************
  ok: [radicale.jlhimpel.net]
  
  TASK [example : Populate service facts] **********************************************************************************************************************
  ok: [radicale.jlhimpel.net]
  
  TASK [example : Display selected service] ********************************************************************************************************************
  ok: [radicale.jlhimpel.net] =&gt; (item=cups.service) =&gt; {
      &quot;ansible_facts.services[serviceToDisplay]&quot;: {
          &quot;name&quot;: &quot;cups.service&quot;,
          &quot;source&quot;: &quot;systemd&quot;,
          &quot;state&quot;: &quot;running&quot;,
          &quot;status&quot;: &quot;enabled&quot;
      },
      &quot;ansible_loop_var&quot;: &quot;serviceToDisplay&quot;,
      &quot;serviceToDisplay&quot;: &quot;cups.service&quot;
  }
  ok: [radicale.jlhimpel.net] =&gt; (item=cups) =&gt; {
      &quot;ansible_facts.services[serviceToDisplay]&quot;: {
          &quot;name&quot;: &quot;cups&quot;,
          &quot;source&quot;: &quot;sysv&quot;,
          &quot;state&quot;: &quot;running&quot;
      },
      &quot;ansible_loop_var&quot;: &quot;serviceToDisplay&quot;,
      &quot;serviceToDisplay&quot;: &quot;cups&quot;
  }
  
  TASK [example : Stop and Disable service if it is running or enabled] ****************************************************************************************
  fatal: [radicale.jlhimpel.net]: FAILED! =&gt; {&quot;msg&quot;: &quot;The conditional check 'serviceToDisable.status == 'enabled'' failed. The error was: error while evaluating conditional (serviceToDisable.status == 'enabled'): 'str object' has no attribute 'status'\n\nThe error appears to be in '/home/jwhimpel/ansible/roles/example/tasks/main.yml': line 16, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Stop and Disable service if it is running or enabled\n  ^ here\n&quot;}
  
  PLAY RECAP ***************************************************************************************************************************************************
  radicale.jlhimpel.net      : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
  </code></pre>
  <p>Ansible version: 2.9.14-1.fc32 on Fedora</p>
  <p>How should I go about checking if the status == enabled or the state == running?</p>
  <p>Note: The service may optionally not be present in ansible_facts.service.  It may also optionally not be running and/or not enabled.  I am attempting to make the tasks idempotent.</p>
  <p>Thanks</p>