4

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.

I want to control num_hosts from variables.

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.

1
  • please include the part of the playbook you are using here as well Commented Apr 18, 2016 at 17:46

2 Answers 2

3

range accespts integers as it's parameters, but jinja will be default convert everything to string. You can use the int built in jinja2 filter to convert values to integer:

{% for id in range(1,( {{ num_hosts | int }} )) %}
Sign up to request clarification or add additional context in comments.

Comments

2

Finally able to make it work with following syntax.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.