2

I am trying configure a attribute only if my_int_http is defined else I dont want it. So I coded it like below:

profiles:
      - "{{ my_int_L4 }}"
      - "{{ my_int_http | default(omit) }}"

However my execution fail and when check the arguments passed by the code in actual configuration it shows like below:

 "profiles": [
                "my_example.internal_tcp",
                "__omit_place_holder__ef8c5b99e9707c044ac07fda72fa950565f248a4"

So how to pass absolutely no value where it is passing __omit_place_holder_****?

seshadri_c
  • 6,906
  • 2
  • 10
  • 24

2 Answers2

1

Q: "How to pass absolutely no value where it is passing omit_place_holder ?"

A1: Some filters also work with omit as expected. For example, the play

- hosts: localhost
  vars:
    test:
      - "{{ var1|default(false) }}"
      - "{{ var1|default(omit) }}"
  tasks:
    - debug:
        msg: "{{ {'a': item}|combine({'b': true}) }}"
      loop: "{{ test }}"

gives

  msg:
    a: false
    b: true

  msg:
    b: true

As a sidenote, default(omit) is defined type string

    - debug:
        msg: "{{ item is defined }}"
      loop: "{{ test }}"
    - debug:
        msg: "{{ item|type_debug }}"
      loop: "{{ test }}"

give

TASK [debug] *************************************************************
ok: [localhost] => (item=False) => 
  msg: true
ok: [localhost] => (item=__omit_place_holder__6e56f2f992faa6e262507cb77410946ea57dc7ef) => 
  msg: true

TASK [debug] *************************************************************
ok: [localhost] => (item=False) => 
  msg: bool
ok: [localhost] => (item=__omit_place_holder__6e56f2f992faa6e262507cb77410946ea57dc7ef) => 
  msg: str

A2: No value in Ansible is YAML null. Quoting:

This is typically converted into any native null-like value (e.g., undef in Perl, None in Python).

(Given my_int_L4=bob). If the variable my_int_http defaults to null instead of omit

profiles:
      - "{{ my_int_L4 }}"
      - "{{ my_int_http | default(null) }}"

the list profiles will be undefined

  profiles: VARIABLE IS NOT DEFINED!

Use None instead

profiles:
      - "{{ my_int_L4 }}"
      - "{{ my_int_http | default(None) }}"

The variable my_int_http will default to an empty string

  profiles:
  - bob
  - ''

See also section "YAML tags and Python types" in PyYAML Documentation.

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

You can try something like this,

profiles:
      - "{{ my_int_L4 }}"
      - "{{ my_int_http | default(None) }}"

This will give you an empty string. And you can add a check while iterating over the profiles.

Please have a look at this GitHub Issue to get more understanding.

Shubham Vaishnav
  • 1,637
  • 6
  • 18