I have wrote the jinja2 template for ansible playbook. However, i dont want the comma at the end of last line for that reason, i have used the "if" condition with "loop.last". Since there is "for" loop and "if" loop, last line is executing two times - one with comma and another without comma. any help would be appreciated for the last line to be executed once without comma.
[{% for ip in range %}
"127.0.0.1:{{ ip }}",
{% if loop.last %}
"127.0.0.1:{{ ip }}"
{% endif %}
{% endfor %}]
Below is the output that i am getting,
[ "127.0.0.1:6000",
"127.0.0.1:6001",
"127.0.0.1:6002",
"127.0.0.1:6003",
"127.0.0.1:6004",
"127.0.0.1:6005",
"127.0.0.1:6006",
"127.0.0.1:6006"
]
Expected output:
["127.0.0.1:6000", "127.0.0.1:6001", "127.0.0.1:6002", "127.0.0.1:6003", "127.0.0.1:6004", "127.0.0.1:6005", "127.0.0.1:6006" ]
Thanks
you could add an else
clause for the non last iterations. try this template file:
[{% for ip in range %}
{% if loop.last %}
"127.0.0.1:{{ ip }}"{% else %}
"127.0.0.1:{{ ip }}", {% endif %}
{% endfor %}]
produced file:
[root@greenhat-30 tests]$ cat /tmp/test.out
["127.0.0.1:6001", "127.0.0.1:6002", "127.0.0.1:6003", "127.0.0.1:6004", "127.0.0.1:6005", "127.0.0.1:6006"]
[root@greenhat-30 tests]$
hope it helps