Default filter issue

Hello,

Im trying to use default filter:

hosts: [“{{ groups[‘examples’][0] | default(‘localhost’) }}”:5045]

But it return error:

FAILED! => {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘examples’”}.

I read something about default can use only 1 level deep, but I cannot write more levels properly.

Hello,

Im trying to use default filter:

hosts: ["{{ groups['examples'][0] | default('localhost') }}":5045]

But it return error:

FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'examples'"}.

I read something about default can use only 1 level deep, but I cannot write more levels properly.

I suppose you have to accommodate the case where there is no examples entry in "groups" (untested):

{% if 'examples' in groups %}groups['examples'][0] | default('localhost'){% else %}localhost{% endif %}

Regards
        Racke

Hello,

Im trying to use default filter:

hosts: ["{{ groups['examples'][0] | default('localhost') }}":5045]

You should be able to use: groups.examples.0 | default(...)

This is the one case where dot notation is simpler/better :slight_smile:

Sebastian

This could work, but only if there is no such group. When the group exists, but its empty, it throws error.

Dne úterý 14. května 2019 9:50:36 UTC+2 Stefan Hornburg (Racke) napsal(a):