群_-var-fi中的稳定条件

2024-04-25 22:58:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个场景,我需要根据运行ansible playbook时传递的名为“provider”的字符串参数在groupvar中设置一个变量。在

ansible 2.3.0.0,python版本=2.7.5 分组_变量.yml,这就是我要达到的目标

if provider=azure then
insecure_registrys: ['{{azure_ip.state.ip_address}}:5000'] else if
provider=vsphere then
insecure_registrys: ['11.168.25.xx:5000']

在github中检查了一个问题后,我正在尝试这种方法

^{pr2}$

上面的true将替换为provider check待会儿吧通过语法检查,但中途失败

FAILED! => {"failed": true, "msg": "{% if etcd_interface != '' %}{{ hostvars[inventory_hostname]['ansible_' + etcd_interface].ipv4.address }}{%- else %}{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}{% endif %}: template error while templating string: expected token ',', got '{'. String: {{ ( true | ternary('['{{azure_ip.state.ip_address}}:5000']','['11.168.25.xx:5000']'))}}"}

在这种情况下你能帮我吗?泰铢


Tags: iptrueifaddressansibleproviderazureelse
1条回答
网友
1楼 · 发布于 2024-04-25 22:58:19

不要使用嵌套的Jinja2表达式,更简单些:

insecure_registries: "{{ [azure_ip.state.ip_address+':5000'] if provider == 'azure' else ['11.168.25.xx:5000'] }}

三元过滤器:

^{pr2}$

相关问题 更多 >