Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Ansible ternary filter

Tags:

ansible

jinja2

I assume I'm doing something wrong here, but it would be helpful if someone could at least point me in the right direction. I have the following task:

- name: Set up users
  user: name={{ item.username }}
        groups={{ item.groups|join(',') }}
        comment={{ item.full_name }}
        shell=/bin/bash
  with_items: "{{ (extra_users is defined) | ternary(users + extra_users, users) }}"

The list extra_users is only sometimes defined, and when it isn't, the task fails with the error "'extra_users' is undefined", which I thought the ternary would guard against. What's going wrong?

like image 326
Leon Aves Avatar asked Oct 20 '25 14:10

Leon Aves


1 Answers

I guess that the parameters of the ternary filter are evaluated no matter what.

Anyway, there is a simplier solution:

with_items: "{{ users | union(extra_users | default ([])) }}"
like image 189
zigarn Avatar answered Oct 22 '25 03:10

zigarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!