คุณสามารถตรวจสอบกับfailedตัวกรอง Jinja2 หลังจากรันคำสั่งของคุณที่ตรวจสอบว่ากระบวนการกำลังทำงานอยู่หรือไม่
นี่คือตัวอย่างที่ใช้เอาต์พุตของคำสั่งsystemctl status apache2เพื่อตัดสินใจว่า Apache กำลังรันอยู่หรือไม่:
- name: Check if Apache is running
  command: systemctl status apache2
  ignore_errors: yes
  changed_when: false
  register: service_apache_status
- name: Report status of Apache
  fail:
    msg: |
      Service apache2 is not running.
      Output of `systemctl status apache2`:
      {{ service_apache_status.stdout }}
      {{ service_apache_status.stderr }}
  when: service_apache_status | failed
หากคำสั่งของงานแรกล้มเหลวงานที่สองจะล้มเหลวและแสดงสาเหตุที่งานแรกล้มเหลว
รหัสส่งคืนถูกเก็บไว้ใน
service_apache_status.rc
ตัวอย่างผลลัพธ์ของความล้มเหลว:
TASK: [Check if Apache is running] *********************** 
failed: [localhost] => {"changed": false, "cmd": ["systemctl", "status", "apache2"], "delta": "0:00:00.009379", "end": "2016-06-06 15:17:27.827172", "rc": 3, "start": "2016-06-06 15:17:27.817793", "stdout_lines": ["* apache2.service", "   Loaded: not-found (Reason: No such file or directory)", "   Active: inactive (dead)"], "warnings": []}
stdout: * apache2.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)
...ignoring
TASK: [Report status of Apache] ***************************
failed: [localhost] => {"failed": true}
msg: apache2 is not running
systemctl status apache2 output:
* apache2.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)
นี่คือวิธีที่แตกต่างกัน (แม้ว่าจะมีความน่าเชื่อถือน้อยกว่า) ใช้pgrepเพื่อตรวจสอบว่ากระบวนการกำลังทำงานอยู่หรือไม่:
- name: Check if Apache is running
  shell: pgrep apache2
  ignore_errors: yes
  changed_when: false
  register: service_apache_status
- name: Report status of Apache
  fail:
    msg: |
      Service apache2 is not running.
      Return code from `pgrep`:
      {{ service_apache_status.rc }}
  when: service_apache_status.rc != 0
               
              
when: service_apache_status | failedทำงานยังไง? มองหาfailedโทเค็นservice_apache_statusหรือไม่