Rescue is not run in ansible playbook

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

- hosts: target1
  become: true
    - name: Install and configure httpd web server
        - yum:
            name: httpd
            state: present
        - service:
            name: httpd
            state: started
      when: ansible_facts['distribution'] == 'CentOS'
        - 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 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See for more
ok: []

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

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

PLAY RECAP ***********************************************************************************************             : 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

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.

Mohd Izhar