sanitar4eg
sanitar4eg

Reputation: 163

How to get access to the default variable of the role from another role in ansible

I have a role with default calculating variables like:

port: {{prefix}}{{postfix}}

When I try to get access to this variable from another role with {{hostvars[broker]['port']}} , I catch error:

AnsibleUndefinedVariable: 'dict object' has no attribute 'port'

In most cases, I need to calculate port, but in rare cases, I need to override this var from host-var. What is the best way to get access to default var from another role?

Ansible version: 2.2.0.0

Upvotes: 5

Views: 2224

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68229

With {{hostvars[broker]['port']}} you can access only host facts and role defaults are not facts, so this will work only if you define port as host-var.

If you absolutely need to query port value from other plays/hosts after applying your role to broker host, you may add to your role:

- set_fact:
    port: "{{ port }}"

this will ensure that there is port fact exist so you can access it via hostvars.

Upvotes: 3

Related Questions