How to pass variables from one role downstream to other dependency roles with ansible?

北城余情 提交于 2019-12-05 01:24:13

This scenario works with Ansible 2.2.
Vars for dependent roles are specified in main role's vars files:

./roles/role1/tasks/main.yml:

- debug: msg="{{ role_param }}"

./roles/role2/meta/main.yml:

allow_duplicates: yes
dependencies:
  - role: role1
    role_param: "{{ param1 }}"
  - role: role1
    role_param: "{{ param2 }}"

./roles/role2/tasks/main.yml:

- debug: msg=role2

./roles/role2/vars/main.yml:

param1: hello1
param2: hello2

Result:

PLAY [localhost] ***************************************************************

TASK [role1 : debug] ***********************************************************
ok: [localhost] => {
    "msg": "hello1"
}

TASK [role1 : debug] ***********************************************************
ok: [localhost] => {
    "msg": "hello2"
}

TASK [role2 : debug] ***********************************************************
ok: [localhost] => {
    "msg": "role2"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!