How can I pass a parameter to a role in Ansible from inventory file? -
i have set of roles unique , use common role pulled in via dependency perform bunch of same actions these other roles need have done. need able pass specific role parameter in case pull docker image registry, in other case save image out , other things. variables cause issues explicit host , might have several roles per host. how can structure ansible playbook this?
example: have common role pulled other roles dependency:
--- - name: included playbook - vars 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 primary role tasks\main.yml
--- - name: included playbook - vars see primary vars: myname: "{{ imagemainname }}" debug: msg: "name-primary: {{ myname }}" and meta\main.yml
--- dependencies: - { role: image-pull, imagename: "{{ imagemainname }}" } this same second role ---
- name: included playbook - vars see second vars: myname: "{{ secondname }}" debug: msg: "name-second: {{ myname }}" and meta\main.yml
--- dependencies: - { role: image-pull, imagename: "{{ secondname }}" } my main playbook calls both primary , second roles , 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 when want state option or b in "pulled" role.
if inventory file looks this:
[imagemaster] 127.0.0.1 [imagemaster:vars] localimage=false [second] 127.0.0.1 [second:vars] localimage=true it doesn't work whatever last entry localimage roles use.
what can pass in inventory/host_vars/etc means playbook doesn't change every iteration in setup?
if plan apply primary , secondary role same host (as in example (127.0.0.1), have no options:
within section, redefining var overwrite previous instance. if multiple groups have same variable, last 1 loaded wins. if define variable twice in play’s vars: section, 2nd 1 wins.
(from docs)
if plan apply them different hosts, test properly, e.g.:
[imagemaster] 127.0.0.1 [imagemaster:vars] localimage=false [second] 127.0.1.1 [second:vars] localimage=true in scenario, when role (primary/secondary) applied imagemaster group, localimage=false; when applied second group – localimage=true.
Comments
Post a Comment