I have a template
tasks like
- name: template 'etcd.env'
ansible.builtin.template:
src: ../templates/templates/etcd.env.j2
dest: "{{ etcd_bin_dir }}/.etcd.env"
owner: "{{ ansible_become_user }}"
group: "{{ ansible_become_user }}"
mode: '0600'
become: true
the template is:
ETCD_NAME="{{ etcd_name }}"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://{{ ansible_default_ipv4.address }}:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_ADVERTISE_CLIENT_URLS="http://{{ ansible_default_ipv4.address }}:2379"
- I send this to 3 hosts.
- The value for
"{{ etcd_name }}"
should be different based on the hostname (identified byinventory_hostname
, oranisble_hostname
) of the target?
how to I set this up?
3 x set_fact
with a when
condtion for each host comes to mind, but I have a feeling having heard people mentionen set_fact
not being really ansible-ish.
setting up a host_var_dict
was also thrown at me, but I do not quite get how this practically would look like.
what I need is
etcd_host0: rd5428.dbinfra.internal
→ etcd_name: etcd_0
etcd_host1: rd5429.dbinfra.internal
→ etcd_name: etcd_1
etcd_host2: rd5230.dbinfra.internal
→ etcd_name: etcd_2
any hint how to approach this?
the hostnames would be set into variable like
etcd0_host: d5428.dbinfra.internal
inside a vars file loaded into the play.