title: Ansible can we use if condition at hosts
body: <p>How to run the playbook on a specific set of hosts with a conditional variable</p>
  <p><strong>vars_file.yml</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>deployment: blue
  </code></pre>
  <p><strong>hosts_file.yml</strong></p>
  <pre><code>[east1]
  127.0.0.1
  127.0.0.2
  
  [west2]
  127.0.0.3
  127.0.0.4
  </code></pre>
  <p><strong>playbook.yml</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>---
  hosts: all
  vars_files:
    - 'vars_file.yml'
  tasks:
    - copy: src=config dest=/tmp/
  
  hosts: {{ east1[0] if deployment == &quot;blue&quot; else west2[0]}}
  vars_files:
    - 'vars_file.yml'
  tasks:
    - shell: &quot;./startup_script restart&quot;
  
  </code></pre>
  <p>Note: I cant pass variables through the command line and I cant segregate task to a new playbook.</p>