I’m using the latest version of Ansible, and I am trying to use a default variable in role-one
used on host one
, in role-two
, used on host two
, but I can’t get it to work.
Nothing I have found in the documentation or on StackOverflow has really helped. I’m not sure what I am doing wrong. Ideally I want to set the value of the variable once, and be able to use it in another role for any host in my playbook.
I’ve broken it down below.
In my inventory I have a hosts group called [test]
which has two hosts aliased as one
and two
.
[test]
one ansible_host=10.0.1.10 ansible_connection=ssh ansible_user=centos ansible_ssh_private_key_file=<path_to_key>
two ansible_host=10.0.1.20 ansible_connection=ssh ansible_user=centos ansible_ssh_private_key_file=<path_to_key>
I have a single playbook with a play for each of these hosts and I supply the hosts:
value as "{{ host_group }}[0]"
for host one
and "{{ host_group }}[1]"
for host two
.
The play for host one
uses a role called role-one
and the play for host two
uses a role called role-two
.
- name: Test Sharing Role Variables
hosts: "{{ host_group }}[0]"
roles:
- ../../ansible-roles/role-one
- name: Test Sharing Role Variables
hosts: "{{ host_group }}[1]"
roles:
- ../../ansible-roles/role-two
In role-one
I have set a variable variable-one
.
---
# defaults file for role-one
variable_one: Role One Variable
I want to use the value of variable_one
in a template in role-two
but I haven’t had any luck. I’m using the below as a task in role-two
to test and see if the variable is getting "picked-up".
---
# tasks file for role-two
- debug:
msg: "{{ variable_one }}"
When I run the playbook with ansible-playbook test.yml --extra-vars "host_group=test"
I get the below failure.
TASK [../../ansible-roles/role-two : debug] ***********************************************************************************************************************************************************************************************
fatal: [two]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: "hostvars['test']" is undefinednnThe error appears to be in 'ansible-roles/role-two/tasks/main.yml': line 3, column 3, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nn# tasks file for role-twon- debug:n ^ heren"}
2
Answers
Variables declared in roles are scoped to the play. If you want to access a variable from
role-one
inrole-two
, they would both need to be in the same play. For example, you could write:Alternatively, you could restructure your roles so that the variables can be imported separately from your actions. That is, have a
role_one_vars
role that does nothing but define variables, and then you can import that in bothrole-one
androle-two
. That is, you would have a structure something like:And
role-one/tasks/main.yml
would look like:role-two/tasks/main.yml
would look like:And
role-one-vars/vars/main.yml
would look like:Putting this all together, the output looks like:
Q: "Access variable from one role in another role in an Ansible playbook with multiple hosts"
A: Short answer: Use set_fact and put the variable into the hostvars.
Details: Given the roles
The playbook
gives (abridged)
As expected, the variable variable_one is visible to the tasks in the first play. But, there is no reason the variable should be visible to the host two in the second play. The variable is not visible also to the same host in the third play because it hasn’t been stored in the hostvars aka "instantiated". The playbook below
gives (abridged)
Now, the variable is visible to the host one in the whole playbook and can be visible to other hosts using hostvars as well. For example, the playbook below
gives (abridged)
The problem with the above setting is that the host referencing hostvars is hardcoded. A better approach is to "instantiate" the variable in the first play for all hosts. For example, add a dummy task to the role
Then, in the first play, include all hosts, run_once import the role, run the dummy task only, and "instantiate" the variable for all hosts. For example
gives (abridged)