0
我想有一个简单的,如果在ansibles神社模板else条件。对于普通的Pythonansible条件模板
cluster_name+'A' if isCondition is True else cluster_name +'B'
wors巨大的,如果下面的变量定义:
isSingleNode = True
cluster_name = 'example'
在ansible我看到下列错误:
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleError: template error while templating string: no test named 'True'. String: {\n\n \"key\" : \"{{ groups[cluster_name+'_mn01' if isSingleNode is True else cluster_name + '_mn02'] }}\"\n}\n"}
下面是一个小例子:
file_1:变量
---
isCondition: True
file_2:playbook.yml
---
- hosts: all
tasks:
- include_vars: variables
- debug: msg=" condition is {{ isCondition }} with cluster_name {{ cluster_name }}"
- name: copy file
template: src="bare_cluster.bp.j2" dest={{ cluster_name }}_blueprint.json backup=yes
file_4:库存
[examplecluster:children]
examplecluster_mn01
[mn01:children]
examplecluster_mn01
[examplecluster_mn01]
localhost ansible_connection=local
file_5:bare_cluster.bp.j2
{
"key" : "{{ groups[cluster_name+'_mn01' if isSingleNode is True else cluster_name + '_mn02'] }}"
}
命令来执行最小的例子是ansible-playbook -i inventory playbook.yml -e 'cluster_name=examplecluster'
@downvoter请解释原因。 –
你不能写在** **的Jinja2表达任意Python代码。 –