TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Interesting Uses of Ansible's ternary filter

45 pointsby zufallsheldabout 1 year ago

6 comments

kimbernatorabout 1 year ago
I really wish Ansible would have chosen to use python instead of YAML (since that&#x27;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&#x27;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&#x27;re still just writing in pure ruby. Obviously Ansible has the advantage being agentless, but I just cannot stand how popular it is.
评论 #39468104 未加载
评论 #39468600 未加载
评论 #39468092 未加载
评论 #39468720 未加载
评论 #39469706 未加载
评论 #39468251 未加载
评论 #39469755 未加载
raziel2pabout 1 year ago
Why would you use this instead of e.g.<p><pre><code> {{ &quot;--quiet&quot; if ansible_verbosity == 0 }} </code></pre> <a href="https:&#x2F;&#x2F;jinja.palletsprojects.com&#x2F;en&#x2F;3.1.x&#x2F;templates&#x2F;#if-expression" rel="nofollow">https:&#x2F;&#x2F;jinja.palletsprojects.com&#x2F;en&#x2F;3.1.x&#x2F;templates&#x2F;#if-exp...</a>
评论 #39466215 未加载
评论 #39470182 未加载
评论 #39466194 未加载
kunleyabout 1 year ago
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 &quot;language&quot; is in this article&#x27;s examples
评论 #39467559 未加载
评论 #39466604 未加载
评论 #39466282 未加载
Szpadelabout 1 year ago
I use it to conditionally add elements to array or dict in group_vars, real life example:<p>aws_tags_pio: RoleImage: &quot;pio&quot;<p>aws_tags_role_app_tpl: [ { Role: &quot;app&quot; }, &quot;{{ (mageops_pio_worker_enable and not mageops_pio_worker_dedicated_asg) | ternary(aws_tags_pio, {}) }}&quot; ]<p>aws_tags_role_app: &quot;{{ aws_tags_role_app_tpl | combine }}&quot;<p><a href="https:&#x2F;&#x2F;github.com&#x2F;mageops&#x2F;ansible-infrastructure&#x2F;blob&#x2F;5dcc321c8d2a918afd43e2a50665ccbd1159a674&#x2F;group_vars&#x2F;all.yml#L83-L96">https:&#x2F;&#x2F;github.com&#x2F;mageops&#x2F;ansible-infrastructure&#x2F;blob&#x2F;5dcc3...</a><p>for array you use flatten eg<p>aws_security_group_persistant_rules_tpl: [ &quot;{{ mageops_ssh_proxy_persistant | ternary(aws_security_group_persistant_rules_ssh_proxy, []) }}&quot;, &quot;{{ mageops_tinyproxy_persistant_enabled | ternary(aws_security_group_persistent_rules_tinyproxy, []) }}&quot; ]<p>aws_security_group_persistant_rules: &quot;{{ aws_security_group_persistant_rules_tpl | flatten }}&quot;<p>this pattern really helped to clean up conditionals that normally had to be done in tasks
IncreasePostsabout 1 year ago
Never thought I&#x27;d see a blog post describing an if statement.
评论 #39470026 未加载
pton_xdabout 1 year ago
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&#x27;t qualify.<p>Everything else should just be handled by a python script to orchestrate the playbooks.