title: Ansible conditionally delegate_to local or remote?
body: <p>I have a few <code>ansible</code> playbooks which sometimes make sense in the local context, otherwise they are executed remotely. In order to do that I use <code>delegate_to</code> directive, but that also means that I have to double all my tasks, eg.:</p>
  
  <pre><code>---
  - hosts: all
    gather_facts: no
  
    tasks:
  
    - name: Local command
      command: hostname
      register: target_host
      when: vhost is undefined
      delegate_to: 127.0.0.1
  
  # ---    
  
    - name: Remote command
      command: hostname
      register: target_host
      when: vhost is defined
  </code></pre>
  
  <p><strong><em>Exec locally</em></strong>:</p>
  
  <pre><code>$ ansible-playbook -i inv.d/test.ini play.d/delegate.yml
  
  PLAY [all] ******************************************************************** 
  
  TASK: [Local command] ********************************************************* 
  changed: [new-server -&gt; 127.0.0.1]
  
  TASK: [Remote command] ******************************************************** 
  skipping: [new-server]
  
  PLAY RECAP ******************************************************************** 
  new-server                 : ok=1    changed=1    unreachable=0    failed=0
  </code></pre>
  
  <p><strong><em>Exec on remote</em></strong>:</p>
  
  <pre><code>$ ansible-playbook -i inv.d/test.ini play.d/delegate.yml -e vhost=y
  
  PLAY [all] ******************************************************************** 
  
  TASK: [Local command] ********************************************************* 
  skipping: [new-server]
  
  TASK: [Remote command] ******************************************************** 
  changed: [new-server]
  
  PLAY RECAP ******************************************************************** 
  new-server                 : ok=1    changed=1    unreachable=0    failed=0
  </code></pre>
  
  <p>Is there a smarter way to tell <code>ansible</code> when to fallback to the local environment? Currently I'm using <code>ansible==1.9.2</code>.</p>