python - ansible 条件模板

标签 python ansible jinja2 ansible-2.x ansible-template

我想在 ansibles jinja 模板中有一个简单的 if else 条件。对于普通的 python

cluster_name+'A' if isCondition is True else cluster_name +'B'

如果定义了以下变量,效果会更好:

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"}

这是一个最小的例子:

文件_1:变量

---
isCondition: True

file_2: 剧本.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'

最佳答案

你尝试了吗?

{% if isCondition %} {{ cluster_name }} A {% else %} {{ cluster_name }} B {% endif %}

关于python - ansible 条件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42140942/

相关文章:

docker - 使用 docker 的 Ansible 分子 - 如何指定内存限制

debian - 使用 Ansible 使用 debconf-set-selections 自动化*仅* exim4 配置

python - 如何在 Flask 的扩展名为 .jhtml 的模板中启用自动转义?

python - 在 Visual Basic 中动态导入类

python - 在 Python 中覆盖 "remaining elements truncated"

python - 使用 pip 安装包时出现错误 403

c++11正则表达式比python慢

linux - 在 Linux 上从 Ansible 连接 winrm 模块

flask - 扩展 Flask-Security 登录模板时遇到问题

node.js - 撇号 CMS 幻灯片单例无法访问