title: How to run delegate_to task only on one host in group with if/when condition?
body: <p>My host file looks like below where I am using specific host variables <code>smca_role</code>. </p>
  
  <pre><code>[smca]
  smca1 ansible_host=10.66.69.194 smca_role=True
  smca2 ansible_host=10.66.69.193 smca_role=False
  </code></pre>
  
  <p>My playbook looks like below. I am delegating a task to the above <code>smca</code> group and it is going to run on both the hosts which I don't want.</p>
  
  <pre><code>---
  - hosts:smsm
    tasks:
      - name: Check the flag for smca role
        shell: *****Some Command*****
        register: enable_result
        delegate_to: '{{ item }}'
        with_items: '{{ groups.smca }}'
  </code></pre>
  
  <p><strong>My problem:</strong> I want to run it on only one host which matches the condition <code>smca_role=True</code>.  How can I use Jinja2 template &amp; put if/when condition inside <code>with_items: {{ groups.smca }}</code> so that it runs only on the required host?</p>
  
  <p><strong>Notes:</strong>  </p>
  
  <ol>
  <li><p>Using plain when condition won't work in my case as the playbook is run on another host group <code>smsm</code> where I'm gathering/setting facts and I'm using those facts when delegating it to <code>smca</code> host group.</p></li>
  <li><p><code>{{ groups.smca[0] }}</code> won't work in my case as there is no guarantee that always the first host will have that condition true it can keep changing &amp; <code>smca</code> group can have more than 2 hosts.</p></li>
  </ol>