How to pass variable into role

828 views
Skip to first unread message

liubo

unread,
Jun 7, 2018, 9:45:03 PM6/7/18
to Ansible Project
SUMMARY
It seems hard to send a param into a role. The rule of `vars` is confusing.

# What I want is overwrite `version` in role `b` by `version` in role `a`
# roles/a/default/main.yml
version
: v1.0.1
# roles/a/tasks/main.yml
- name: include role
  include_role
:
    name
: b
  vars
:
    version
: "{{ version }}"


# roles/b/default/main.yml
version
: ""
# roles/b/tasks/main.yml
- name: ouput version
  debug
:
   
var: version


EXPECTED RESULTS
I can pass the `version` variable into role `b` without any variable name changes of role b and a

ACTUAL RESULTS
1. I have to change `version` to `_version` in role `a`.
Some problems will happened if `_version` is also defined by `b`. And I have to know all inner variables of `b`

2. I have to undefine `version` in role `b`
Role b can't have any default value of `version`.

It seems hard to keep independent of role.

Brian Coca

unread,
Jun 15, 2018, 2:09:27 PM6/15/18
to Ansible Project
'role defaults' are special, they do make them available for the rest
of the play but a role will always give priority to their own default
definition.



--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages