Search code examples
ansibleansible-inventory

From an Ansible inventory file, is it possible to include variables from another file?


My Ansible hosts file:

mybox.mydomain.com ansible_ssh_pass={{mypassword}}

The variable 'mypassword' is defined in a file "variables.yml":

mypassword = somepassword

Is there a way I can include the variables from "variables.yml" in my hosts file? Otherwise when I try to run some Ansible command, I get the error

the field 'password' has an invalid value, which appears to include a variable that is undefined. The error was: 'mypassword' is undefined

I'm asking this because ultimately I want to use a Vault encrypted variable from a .yml file (since that is the only way Vault encrypts individual variables)


Solution

  • Put variables.yml into group_vars/all/variables.yml near your inventory.

    This will load everything in variables.yml as group variables for every host in your inventory.