title: Ansible run task multiple times based on groups
body: <p>How do you run a task on each group, not just once for the groups?</p>
  
  <p>I was excepting the <code>Run this on each host</code> tasks would run once for each group_var value.  Instead it seems to just be picking one and running it.</p>
  
  <p>I plan on breaking these across multiple servers later but for now it should be able to run on one autoscale, and then easy break it up into multiple autoscale groups later as demand increases.</p>
  
  <p>playbook.yml:</p>
  
  <pre><code>---
  # Run with: ansible-playbook -i localhost, playbook.yml
  - name: Register Groups
    hosts: localhost
    connection: local
    tasks:
      - name: Add the groups
        add_host:
          name: localhost
          ansible_connection: local
          groups: rest-api, msg-consumer
  
  - name: Run this on each host
    hosts:
      - rest-api
      - msg-consumer
    tasks:
      - name: Say type
        debug: var=item
        with_items: run_type
  </code></pre>
  
  <p>group_vars/rest-api:</p>
  
  <pre><code>---
  run_type: web
  </code></pre>
  
  <p>group_vars/msg-consumer:</p>
  
  <pre><code>---
  run_type: consumer
  </code></pre>
  
  <p>Output Ansible 1.8.2:</p>
  
  <pre><code>$ ansible-playbook -i localhost, playbook.yml
  
  PLAY [Register Groups] ********************************************************
  
  GATHERING FACTS ***************************************************************
  ok: [localhost]
  
  TASK: [Add the groups] ********************************************************
  ok: [localhost]
  
  PLAY [Run this on each host] **************************************************
  
  GATHERING FACTS ***************************************************************
  ok: [localhost]
  
  TASK: [Say type] **************************************************************
  ok: [localhost] =&gt; (item=consumer) =&gt; {
      "item": "consumer"
  }
  
  PLAY RECAP ********************************************************************
  localhost                  : ok=4    changed=0    unreachable=0    failed=0
  </code></pre>
  
  <p>Note: It may be something else.  I thought I could also clutter my playbook but breaking up the tasks like like follows:</p>
  
  <pre><code>---
  - name: Register Groups
    hosts: localhost
    connection: local
    tasks:
      - name: Add the groups
        add_host:
          name: localhost
          ansible_connection: local
          groups: rest-api, msg-consumer
  
  - name: Run this on each host
    hosts:
      - msg-consumer
    tasks:
      - name: Say type
        debug: var=item
        with_items: run_type
  
  - name: Run this on each host
    hosts:
      - rest-api
    tasks:
      - name: Say type
        debug: var=item
        with_items: run_type
  </code></pre>
  
  <p>But the output for the 2nd playbook is:</p>
  
  <pre><code>$ ansible-playbook -i localhost, playbook2.yml
  
  PLAY [Register Groups] ********************************************************
  
  GATHERING FACTS ***************************************************************
  ok: [localhost]
  
  TASK: [Add the groups] ********************************************************
  ok: [localhost]
  
  PLAY [Run this on each host] **************************************************
  
  GATHERING FACTS ***************************************************************
  ok: [localhost]
  
  TASK: [Say type] **************************************************************
  ok: [localhost] =&gt; (item=consumer) =&gt; {
      "item": "consumer"
  }
  
  PLAY [Run this on each host] **************************************************
  
  GATHERING FACTS ***************************************************************
  ok: [localhost]
  
  TASK: [Say type] **************************************************************
  ok: [localhost] =&gt; (item=consumer) =&gt; {
      "item": "consumer"
  }
  
  PLAY RECAP ********************************************************************
  localhost                  : ok=6    changed=0    unreachable=0    failed=0
  </code></pre>
  
  <h2>Edit: Yet Another attempt to access the data, it looks like group_vars isn't behaving like I expect. The following outputs <code>consumer</code> twice also.</h2>
  
  <pre><code>-
  # Run with: ansible-playbook -i localhost, playbook.yml
  - name: Register Groups
    hosts: localhost
    connection: local
    tasks:
      - name: Add the groups
        add_host:
          name: localhost
          ansible_connection: local
          groups: rest-api, msg-consumer
  
  - name: Run this on each host
    hosts:
      - msg-consumer
      - rest-api
    tasks:
      - name: What's your run type
        debug: var=hostvars[groups[item][0]]['run_type']
        with_items: group_names
  </code></pre>