How to include vars file in a vars file with ansib

2019-03-14 06:57发布

问题:

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...

回答1:

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


回答2:

I know it's an old post but I had the same issue today, what I did is simple : changing my script that send my playbook from my local host to the server, before sending it with maven command, I did this :

cat common_vars.yml > vars.yml
cat snapshot_vars.yml >> vars.yml
# or 
#cat release_vars.yml >> vars.yml
mvn ....


标签: ansible