I really wish Ansible would have chosen to use python instead of YAML (since that's what it ends up being anyways). Shoehorning actual programming logic into YAML files is just awful; Working with Ansible as a software engineer is without a doubt the worst work I've done in my professional career. Every otherwise simple logic structure in any programming language like loops, variable declaration, or conditional statements take 5x as much space and are very difficult to understand immediately.<p>My first love in this space was Chef, and honestly it remains my favorite in config management because you can write things in it that look very non-programmy, but you're still just writing in pure ruby. Obviously Ansible has the advantage being agentless, but I just cannot stand how popular it is.
Why would you use this instead of e.g.<p><pre><code> {{ "--quiet" if ansible_verbosity == 0 }}
</code></pre>
<a href="https://jinja.palletsprojects.com/en/3.1.x/templates/#if-expression" rel="nofollow">https://jinja.palletsprojects.com/en/3.1.x/templates/#if-exp...</a>
Cool. Ansible is still going to be around for some time.<p>Also reminds me again that yaml should have never been born. Look how unnatural this "language" is in this article's examples
I use it to conditionally add elements to array or dict in group_vars, real life example:<p>aws_tags_pio:
RoleImage: "pio"<p>aws_tags_role_app_tpl: [
{ Role: "app" },
"{{ (mageops_pio_worker_enable and not mageops_pio_worker_dedicated_asg) | ternary(aws_tags_pio, {}) }}"
]<p>aws_tags_role_app: "{{ aws_tags_role_app_tpl | combine }}"<p><a href="https://github.com/mageops/ansible-infrastructure/blob/5dcc321c8d2a918afd43e2a50665ccbd1159a674/group_vars/all.yml#L83-L96">https://github.com/mageops/ansible-infrastructure/blob/5dcc3...</a><p>for array you use flatten eg<p>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, []) }}"
]<p>aws_security_group_persistant_rules: "{{ aws_security_group_persistant_rules_tpl | flatten }}"<p>this pattern really helped to clean up conditionals that normally had to be done in tasks
Ansible is great as long as you keep Jinja use to a minimum. Basic variable substitution handles 90% of what you need. Ternary filter... doesn't qualify.<p>Everything else should just be handled by a python script to orchestrate the playbooks.