Try Ansible’s conditional branching.
Ansible’s strength is its simplicity, and it is not good at verification and complex processing. Conditional branching is a process that can become complicated and should not be actively incorporated, but there are times when you really want to do conditional branching. Let’s try conditional branching.

Stores the result in a variable and executes it when it matches when.
Conditional branches can be specified with when. The following is an example of executing cat /etc/redhat-release when ansible_os_family is RedHat.
---
- name: Test Playbook
hosts: test_servers
tasks:
- name: Check OS Family
debug:
var: ansible_os_family
- name: Check OS Version
command: cat /etc/redhat-release
when: ansible_os_family == "RedHat"
Conclusion
I tried Ansible’s conditional branching, which is simple, but allows you to conditional branch on when and have the command executed.