1
votes

I have three hosts in my invenotry file to configure HAPROXY setup. using below ansible tempalte I was able populate the IP address in the configurtion file. I also want to change the name with index value. how can I change kube-1 to variable like (kube-1, kube-2, kube-3)

template

{% for host in groups['master'] %}
        server kube-1 {{ hostvars[host]["ansible_default_ipv4"]["address"] }}:30549 check verify none
{% endfor %}

sample output

server kube-1 x.x.x.x:30549 check verify none
server kube-1 x.x.x.x:30549 check verify none
server kube-1 x.x.x.x:30549 check verify none

Required output

server kube-1 x.x.x.x:30549 check verify none
server kube-2 x.x.x.x:30549 check verify none
server kube-3 x.x.x.x:30549 check verify none

Thanks SR

1

1 Answers

0
votes

There is a loop.index variable available inside a for control structure in jinja2. The following should meet your expectations.

{% for host in groups['master'] %}
        server kube-{{ loop.index }} {{ hostvars[host]["ansible_default_ipv4"]["address"] }}:30549 check verify none
{% endfor %}

Ref: https://jinja.palletsprojects.com/en/2.11.x/templates/#for