Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include vars file in a vars file with ansible?

Tags:

ansible

Is it possible to include a vars file in Ansible into another vars file dynamically?

I.e. I have vars file:

--- definitions: - { product: web_v2, suite: mysuite, include: default_step.yml } - { product: prod2, suite: mysuite2, include: default_step.yml } 

I want the contents of default_step.yml to be added to the dictionary. Note this is in a vars file so the documentation on how to include a vars file from a task doesn't seem to apply.

All I can think of is to template this file with jinja and use its {% include %} function, and then afterwards use include_vars from the main task but that seems complicated...

like image 335
jbrown Avatar asked Jan 02 '15 17:01

jbrown


People also ask

How do you pass an var file in Ansible?

To pass a value to nodes, use the --extra-vars or -e option while running the Ansible playbook, as seen below. This ensures you avoid accidental running of the playbook against hardcoded hosts.

How do you pass multiple vars in Ansible?

Ansible treats values of the extra variables as strings. To pass values that are not strings, we need to use JSON format. To pass extra vars in JSON format we need to enclose JSON in quotation marks: ansible-playbook extra_var_json.

What is include VARS in Ansible?

include_vars module – Load variables from files, dynamically within a task. Note. This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name include_vars even without specifying the collections: keyword.

How do you call vars in Ansible?

To define a variable in a playbook, simply use the keyword vars before writing your variables with indentation. To access the value of the variable, place it between the double curly braces enclosed with quotation marks. In the above playbook, the greeting variable is substituted by the value Hello world!


1 Answers

Unfortunately, vars files do not have include statements.

You can either put all the vars into the definitions dictionary, or add the variables as another dictionary in the same file.

If you don't want to have them in the same file, you can include them at the playbook level by adding the vars file at the start of the play:

--- - hosts: myhosts    vars_files:     - default_step.yml 

or in a task:

--- - hosts: myhosts    tasks:     - name: include default step variables       include_vars: default_step.yml 
like image 88
jonatan Avatar answered Sep 22 '22 05:09

jonatan