Rescue is not run in ansible playbook

hii i have a playbook there my target host is amazon2 vm

---
- hosts: target1
  become: true
  tasks:
    - name: Install and configure httpd web server
      block:
        - yum:
            name: httpd
            state: present
        - service:
            name: httpd
            state: started
      when: ansible_facts['distribution'] == 'CentOS'
      rescue:
        - debug:
            msg: "Playbook has failed for {{ inventory_hostname }} node"

when i run this i got this logs

PLAY [target1] *******************************************************************************************
TASK [Gathering Facts] ***********************************************************************************[WARNING]: Platform linux on host 13.234.119.169 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
ok: [13.234.119.169]

TASK [yum] ***********************************************************************************************skipping: [13.234.119.169]

TASK [service] *******************************************************************************************skipping: [13.234.119.169]

PLAY RECAP ***********************************************************************************************13.234.119.169             : ok=1    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   

there when: ansible_facts[‘distribution’] == ‘CentOS’ should be
when: ansible_facts[‘distribution’] == ‘Amazon’ then i will run but when centos is there it is a error then i should run rescue but it is not running

Hi, @abhineetsaxena05 not sure if I understood what is your problem. But you can change distribution for os_family. This way, your playbook will run for a list of O.S. compatible:

# OS Family
when: ansible_facts['os_family'] == "RedHat"

This example will trigger for any of those O.S:

  • RedHat
  • CentOS
  • Fedora
  • Scientific
  • CloudLinux
  • OracleLinux
  • Amazon
  • XenServer

Regards,
Vitor Jr.
KodeKloud Support

Hi @abhineetsaxena05,

Based on your query, I would suggest to try this

when: (ansible_os_family == 'RedHat' or ansible_os_family == 'CentOS')

as I have faced the same issue earlier and this worked for me. You should try this too.

Regards,
Mohd Izhar