0
votes

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?

1
Are you aware of this statement: "expected behavior is that the var will have the value of the last group loaded"? You need to rethink your playbook, or wait for some cynophilist who would grasp your objectives.techraf
That's why I'm asking. How do I restructure the playbook to accomplish something like this or is it even possible to pass in some kind of parameter to a playbook that is held to the role?IguyKing

1 Answers

0
votes

If you plan to apply primary and secondary role to the same host (as in your example (127.0.0.1), then you have no options:

Within any section, redefining a var will overwrite the previous instance. If multiple groups have the same variable, the last one loaded wins. If you define a variable twice in a play’s vars: section, the 2nd one wins.

(from the docs)

If you plan to apply them to different hosts, then test is properly, e.g.:

[imagemaster]
127.0.0.1

[imagemaster:vars]
localimage=false

[second]
127.0.1.1

[second:vars]
localimage=true

In this scenario, when role (primary/secondary) is applied to imagemaster group, then localimage=false; when applied to second group – localimage=true.