In Ansible, run only one item in a loop based on a variable
<p>In my Ansible playbook I set up a number of websites on one server. Each website has some configuration set in an <code>apps</code> variable:</p>

<pre class="lang-yaml prettyprint-override"><code>apps:
  - name: 'app1'
    git_repo: 'https://github.com/philgyford/app1.git'
  - name: 'app2'
    git_repo: 'https://github.com/philgyford/app2.git'
</code></pre>

<p>Then certain tasks are run in a loop for every app/site:</p>

<pre class="lang-yaml prettyprint-override"><code>- name: Set up git repos for apps
  git:
    repo: '{{ item.git_repo }}'
    version: 'master'
    dest: "/webapps/{{ item.name }}"
    accept_hostkey: yes
  with_items: apps
</code></pre>

<p>That's all fine. But ideally I'd like to be able to run the playbook and only run these tasks for a specific app.</p>

<p>One way would be to pass in <code>--extra-vars "app=app1"</code> on the command line. In the playbook I'd set a default value for <code>app</code> of <code>false</code>. And then on every app-related task add:</p>

<pre class="lang-yaml prettyprint-override"><code>  when: item.name == app or app == false
</code></pre>

<p>This should work, but it seems laborious and error-prone to have to add that to every single task. Is there a more elegant way that avoids so much repetition?</p>