Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable from one Ansible var file in a second var file

In using Ansible, I'm trying to use a vaulted vars file to store private variables, and then using those in another vars file, in the same role. (The idea from 'Vault Pseudo leaf encryption' here.)

e.g. I have one standard vars file, roles/myrole/vars/main.yml:

---
my_variable: '{{ my_variable_vaulted }}'

and then one which is encrypted, roles/myrole/vars/vaulted_vars.yml:

---
my_variable_vaulted: 'SECRET!'

But when I run the playbook I always get '"ERROR! ERROR! 'my_variable_vaulted' is undefined"'.

I've tried it without encrypting the second file, to make sure it's not an issue with encryption, and I'm getting the same error.

like image 990
Phil Gyford Avatar asked Mar 14 '23 06:03

Phil Gyford


1 Answers

The reason why my_variable_vaulted wasn't available was because I hadn't included the variable file. I'd assumed that all files in a role's vars/ directory were picked up automatically, but I think that's only the case with vars/main.yml.

So, to make the vaulted variables available to all tasks within the role, in roles/myrole/tasks/main.yml I added this before all the tasks:

- include_vars: vars/vaulted_vars.yml
like image 165
Phil Gyford Avatar answered Apr 27 '23 23:04

Phil Gyford