1

i have this list of ansible inventory

[clickhouse]
server01 ansible_host=10.1.1.1 name=server01 
server02 ansible_host=10.1.1.2 name=server02
server03 ansible_host=10.1.1.3 name=server03 
server04 ansible_host=10.1.1.4 name=server04

i have this config.xml.j2

   <noc>
      <shard>
        <internal_replication>true</internal_replication>
        <replica>
            <host>server01 </host>
            <port>9000</port>
        </replica>
        <replica>
            <host>server02</host>
            <port>9000</port>
        </replica>        
      </shard>
    </noc>

**can you support with looping to be like the below ** enter image description here

ahmed
  • 39
  • 7

1 Answers1

0

create config.xml.j2 file in templates folder:

   <noc>
   {% for i in  range(0, (ansible_play_hosts_all) | length , 2) %}
      <shard>
        <internal_replication>true</internal_replication>
        <replica>
            <host>{{ ansible_play_hosts_all[i] }}</host>
            <port>9000</port>
        </replica>
        <replica>
            <host>{{ ansible_play_hosts_all[i | int + 1] }}</host>
            <port>9000</port>
        </replica>        
      </shard>
    {% endfor %}
    </noc>

and use it with this playbook:

- name: "tips4"
  hosts: all
  gather_facts: false
  tasks: 
    - name: Include vars
      template:
        src: config.xml.j2
        dest: config.xml
      delegate_to: localhost

following the version of ansible you have, you could write

ansible_play_hosts_all[i + 1] instead of ansible_play_hosts_all[i | int + 1]

Frenchy
  • 16,386
  • 3
  • 16
  • 39