Ansible: Run tasks on specific hosts with var/fact from another host
The condition works as expected. For example, the inventory and the playbook below
shell&gt; cat hosts
[managers]
k8s01
k8s02
k8s03

[workers]
worker1
worker2

shell&gt; cat playbook.yml 
- hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname
      when:
        - inventory_hostname != groups.managers.0
        - inventory_hostname in groups.managers

give
shell&gt; ansible-playbook -i hosts playbook.yml 

PLAY [all] ****

TASK [debug] ****
skipping: [worker1]
ok: [k8s02] =&gt; {
    &quot;inventory_hostname&quot;: &quot;k8s02&quot;
}
skipping: [k8s01]
skipping: [worker2]
ok: [k8s03] =&gt; {
    &quot;inventory_hostname&quot;: &quot;k8s03&quot;
}