user14511228
user14511228

Reputation: 11

Using vars_files conditionally in ansible playbook

What I am trying to achieve is something like following

vars_files: - "{{ 'vars/vars.yml' if condition==True else 'vars/vars1.yml' }}"

Can someone help me with the correct syntax?

Upvotes: 1

Views: 3097

Answers (2)

Titou
Titou

Reputation: 394

You can do something like this:

vars_files: 
- vars/{{ 'vars' if condition==True else 'vars1' }}.yml

Upvotes: 1

Vladimir Botka
Vladimir Botka

Reputation: 68034

vars_files is a playbook keyword. The conditions can't be applied to keywords. Instead, an option would be using include_vars which is a task. For example

- hosts: all
  tasks:
    - include_vars: vars/vars1.yml
      when: condition|bool

Notes

  • The directory vars in the path is not necessary. See The magic of ‘local’ paths
  • See CONDITIONAL_BARE_VARS
  • Make sure there is no problem with the higher precedence. If there is a problem with the higher precedence of include_vars open a new question with mcve details. There are many options for how to solve it depending on the use-case.

Upvotes: 4

Related Questions