Ansible YAML 中的条件列表元素 [英] Conditional list element in Ansible YAML
本文介绍了Ansible YAML 中的条件列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个剧本可以在我们服务器上的 docker 容器中部署 consul.我基本上是这样的:
I have a playbook to deploy consul in docker containers on our servers. I basically looks like this:
- name: run consul
hosts: all
tasks:
- name: consul | run consul servers and agents
docker_container:
name: consul
network_mode: bridge
published_ports:
- "{{ docker_host_ip }}:8301:8301"
- "{% if consul_server is defined %}{{ docker_host_ip }}:8300:8300{% endif %}"
...
... 除了最后一行不起作用.如果 consul 被部署为存储在每个主机的 consul_server
变量中的服务器,我只想公开端口 8300.
... except the last line isn't working. I only want to expose port 8300 if consul is deployed as server which is stored in the consul_server
variable for each host.
如何有条件地将元素附加到 YAML 列表/数组?
How do I conditionally append an element to a YAML list/array?
推荐答案
过滤器 是你的朋友.
- set_fact:
myTrue : true
myFalse : false
myList :
- first
myAdd :
- second
- debug:
msg: "When TRUE: {{ myTrue |
ternary( myList | union( myAdd ), myList ) }}"
- debug:
msg: "When FALSE: {{ myFalse |
ternary( myList | union( myAdd ), myList ) }}"
输出(稍微修改以去除不相关):
Output (slightly modified to remove irrelevancies):
TASK [test : debug]
"msg": "When TRUE: [u'first', u'second']"
TASK [test : debug]
"msg": "When FALSE: [u'first']"
这篇关于Ansible YAML 中的条件列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文