News for hackers. Stuff that matters.
Re:Interesting Uses of Ansible's ternary filter by Szpadel on Thursday February 22, 2024 @01:57PM (#39471423)
I use it to conditionally add elements to array or dict in group_vars, real life example:

aws_tags_pio: RoleImage: "pio"

aws_tags_role_app_tpl: [ { Role: "app" }, "{{ (mageops_pio_worker_enable and not mageops_pio_worker_dedicated_asg) | ternary(aws_tags_pio, {}) }}" ]

aws_tags_role_app: "{{ aws_tags_role_app_tpl | combine }}"

https://github.com/mageops/ansible-infrastructure/blob/5dcc3...

for array you use flatten eg

aws_security_group_persistant_rules_tpl: [ "{{ mageops_ssh_proxy_persistant | ternary(aws_security_group_persistant_rules_ssh_proxy, []) }}", "{{ mageops_tinyproxy_persistant_enabled | ternary(aws_security_group_persistent_rules_tinyproxy, []) }}" ]

aws_security_group_persistant_rules: "{{ aws_security_group_persistant_rules_tpl | flatten }}"

this pattern really helped to clean up conditionals that normally had to be done in tasks

There are no comments on this comment yet.