How does the in operator let one condition check a host against a list of supported systems?
when: ansible_facts['distribution'] in supported_distros is true when the host's distribution fact appears as an element of the author's list.
- name: Demonstrate the "in" keyword
hosts: all
gather_facts: true
vars:
supported_distros:
- RedHat
- Fedora
tasks:
- name: Install httpd using dnf, where supported
ansible.builtin.dnf:
name: httpd
state: present
when: ansible_facts['distribution'] in supported_distros
Two different sources meet here. ansible_facts['distribution'] is a fact: Ansible discovers it on each managed host during the Gathering Facts step (so gather_facts must not be turned off). supported_distros is a variable the playbook author wrote. On a Fedora host the fact is "Fedora", which is in the list, so dnf runs; on an Ubuntu host it is "Ubuntu", so the task is skipped.
One list replaces a chain like distribution == "RedHat" or distribution == "Fedora", and supporting another system means adding one line to the list. The comparison is exact and case-sensitive: "redhat" would not match "RedHat".
Go deeper:
Commonly used facts: ansible_facts['distribution'] (official docs) โ the exact values the fact takes on each system.