简体   繁体   English

Ansible Playbook-在for循环中传递变量

[英]Ansible Playbook- Pass variables in for loop

I am trying to create tomcat cluster based on number of hosts, and I am trying to pass variables to a for loop in template file. 我试图根据主机数创建tomcat集群,我试图将变量传递给模板文件中的for循环。

I want to control num_hosts from variables. 我想从变量控制num_hosts

The template file snippet is: 模板文件片段是:

{% for id in range(1,( {{ num_hosts }} )) %}
    <Member
       className="org.apache.catalina.tribes.membership.StaticMember"
       port="4110"
       host="${test.server.web.other{{ id }}.fqdn}"
       domain="delta-static"
    />
{% endfor %}

Got the below error 得到以下错误

fatal: [test-web01.aliceapp.com]: FAILED! =>
{"changed": false,
"failed": true,
"invocation": {"module_args": {"dest": "/home/tomcat/apache-tomcat/conf/server.xml", "src": "test/server.j2"}, "module_name": "template"},
"msg": "AnsibleError: an unexpected type error occurred. Error was an integer is required"}

I have tried multiple combination of syntaxes, bot none of them are working. 我尝试过多种语法组合,但是没有一种语法能够正常工作。

range accespts integers as it's parameters, but jinja will be default convert everything to string. range accesspts整数作为参数,但jinja将默认将所有内容转换为字符串。 You can use the int built in jinja2 filter to convert values to integer: 您可以使用内置的jinja2过滤器中的int将值转换为整数:

{% for id in range(1,( {{ num_hosts | int }} )) %}

Finally able to make it work with following syntax. 最后能够使用以下语法。

{% for id in range(1, ( num_hosts|int ) ) %} {%for range in range(1,(num_hosts | int))%}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM