簡體   English   中英

Ansible Playbook-在for循環中傳遞變量

[英]Ansible Playbook- Pass variables in for loop

我試圖根據主機數創建tomcat集群,我試圖將變量傳遞給模板文件中的for循環。

我想從變量控制num_hosts

模板文件片段是:

{% 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 %}

得到以下錯誤

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

我嘗試過多種語法組合,但是沒有一種語法能夠正常工作。

range accesspts整數作為參數,但jinja將默認將所有內容轉換為字符串。 您可以使用內置的jinja2過濾器中的int將值轉換為整數:

{% 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