Specifying a default value to a dictionary element when the dictionary variable does not exist

12 views
Skip to first unread message

ishan jain

unread,
Oct 27, 2017, 5:45:01 AM10/27/17
to Ansible Project
Hi,

I am using a variables file for assigning values to the variables which i would then be using in my playbooks. Sometimes the values to these variables are further variables whose values are resolved at runtime, and sometimes they have default values as well.

vars.yaml

---

varA:"A"

VarB: 200

varC: "{{ dynamicVar | default("default_value") }}"

varDictA: "{{ Dynamic_Var.child | default("Child_value") }}"


I am using Ansible 2.3 and loading this vars file at the playbook level

---
- name: My playbook

  hosts: host1
  vars_files:
    - vars.yaml

  tasks:
    
    - debug: var=varDictA


Now the problem is that the playbook doesn't work if the dictionary var "Dynamic_Var" is not defined. I do understand that i will be referencing a null in case it is not defined, but in my case i need it to be in a way that either i use a var inside dictionary or its default value, thus have to use it directly. In the var file, i do not even have options to use conditionals.

What is the best way to use a dictionary member directly or the default value when the whole dictionary is undefined ?
Reply all
Reply to author
Forward
0 new messages