How Do You Get Variables That Are Assigned a Value in an Ansible Playbook (i.e., a .yaml file) to Be Assigned from a Different File?

Problem scenario
You have a complex Ansible playbook (a .yaml file) that you want to be more modular with discrete component files.  You want to assign variables in a different file.  This will make your Ansible playbook have fewer lines.  Your playbook must  read in the variables from a separate file and inject them into the playbook when it is run.  You appreciate Unix philosophy of having things be modular.

You tried creating a vars file and placed it in a group_vars directory.  But when you run the playbook you get "The task includes an option with an undefined variable."

How do you have a separate file for variables for your playbook?

Solution
1.  In the directory with the playbook, create a subdirectory called "group_vars". 
2.  In group_vars create a subdirectory called "all". 
3.  Create the file named "vars" in this subdirectory named "all". 
4.  The variables can be assigned in this "vars" file.  The formatting is the same as the Ansible playbook.

Leave a comment

Your email address will not be published. Required fields are marked *