title: How can I pass a parameter to a role in Ansible from inventory file?
body: <p>I have a set of roles that are all unique and use a common role that is pulled in via a dependency to perform a bunch of the same actions that all these other roles need to have done.   I need to be able to pass into a specific role a parameter to say in this case pull a docker image from a registry, in that other case save the image out and do some other things.  Variables cause issues as they are explicit for a host and I might have several roles per host.  How can I structure my ansible playbook to do this?  </p>
  
  <p>Example:
  I have a common role that is pulled into other roles as a dependency:</p>
  
  <pre><code>---
  
  - name: Included Playbook - What vars do I see
    debug:
          msg: "Name Pull: {{ imagename }}"
  
  - name: Local Running
    debug:
          msg: "Local option selected"
    when: localimage == "true"
  
  - name: Not local
    debug:
          msg: "Not Local remote"
    when: localimage == "false"
  </code></pre>
  
  <p>Then the primary role tasks\main.yml</p>
  
  <pre><code>---
  
  - name: Included Playbook - What vars do I see from Primary
    vars:
          myname: "{{ imagemainname }}"
    debug:
          msg: "Name-primary: {{ myname }}"
  </code></pre>
  
  <p>and its meta\main.yml</p>
  
  <pre><code>---
  
  dependencies:
     - { role: image-pull, imagename: "{{ imagemainname }}" }
  </code></pre>
  
  <p>This is the same for a second role
      ---</p>
  
  <pre><code>- name: Included Playbook - What vars do I see from Second
    vars:
          myname: "{{ secondname }}"
    debug:
          msg: "Name-second: {{ myname }}"
  </code></pre>
  
  <p>and its meta\main.yml</p>
  
  <pre><code>---
  
  dependencies:
     - { role: image-pull, imagename: "{{ secondname }}" }
  </code></pre>
  
  <p>My main playbook calls both primary and second roles and the role specific vars works fine.   </p>
  
  <pre><code>---
  
  - name: Master
    hosts: imagemaster
    remote_user: root
    vars:
           imagemainname: "Top Dog"
    roles:
          - image-master
  
  - name: Second
    hosts: second
    remote_user: root
    vars:
           imagemainname: "Second Dog"
    roles:
          - second
  </code></pre>
  
  <p>What doesn't work is when I want to state the do option a or b in the "pulled" role.</p>
  
  <p>If my inventory file looks like this:</p>
  
  <pre><code>[imagemaster]
  127.0.0.1
  
  [imagemaster:vars]
  localimage=false
  
  [second]
  127.0.0.1
  
  [second:vars]
  localimage=true
  </code></pre>
  
  <p>It doesn't work as whatever is the last entry for localimage is what all roles will use.   </p>
  
  <p>What can I do to pass in something from the inventory/host_vars/etc that means my playbook doesn't change for every iteration in this setup?   </p>