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?
Example: I have a common role that is pulled into other roles as a dependency:
---
- 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"
Then the primary role tasks\main.yml
---
- name: Included Playbook - What vars do I see from Primary
vars:
myname: "{{ imagemainname }}"
debug:
msg: "Name-primary: {{ myname }}"
and its meta\main.yml
---
dependencies:
- { role: image-pull, imagename: "{{ imagemainname }}" }
This is the same for a second role ---
- name: Included Playbook - What vars do I see from Second
vars:
myname: "{{ secondname }}"
debug:
msg: "Name-second: {{ myname }}"
and its meta\main.yml
---
dependencies:
- { role: image-pull, imagename: "{{ secondname }}" }
My main playbook calls both primary and second roles and the role specific vars works fine.
---
- 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
What doesn't work is when I want to state the do option a or b in the "pulled" role.
If my inventory file looks like this:
[imagemaster]
127.0.0.1
[imagemaster:vars]
localimage=false
[second]
127.0.0.1
[second:vars]
localimage=true
It doesn't work as whatever is the last entry for localimage is what all roles will use.
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?