I’m pretty new to ansible and I’m not sure how to get it right.
To start with, I have implemented this dictionnary var for an exemple. There is 2 values but it could be a greater number:
tf_ETCD:
1:
hostname: Etcd01
ip: 192.168.1.141
2:
hostname: Etcd02
ip: 192.168.1.142
I’d like the result to be formatted like that :
{{ tf_ETCD1_Host }}=https://{{ tf_ETCD1_IP }}:2380,{{ tf_ETCD2_Host }}=https://{{ tf_ETCD2_IP }}:2380
So I kidda get it by looping over the list via this code:
- name: Format each values
set_fact:
etcd: "{{ item.value.hostname }}=https://{{ item.value.ip }}:2380,"
loop: "{{ lookup('ansible.builtin.dict', tf_ETCD) }}"
register: etcd_query
- name: Examine the joined string
debug:
msg: "{{ etcd_query['results'][0]['ansible_facts']['etcd'] }}{{ etcd_query['results'][1]['ansible_facts']['etcd'] }}"
The result gave me :
TASK [Format each values] **********************************************************************************************
ok: [] => (item={'key': 1, 'value': {'hostname': 'Etcd01', 'ip': '192.168.1.141'}})
ok: [] => (item={'key': 2, 'value': {'hostname': 'Etcd02', 'ip': '192.168.1.142'}})
TASK [Examine the joined string] ***************************************************************************************
ok: [] => {
"msg": "Etcd01=https://192.168.1.141:2380,Etcd02=https://192.168.1.142:2380,"
}
I cannot figure out how to do a “foreach loop result” in my “Examine the joined string” play.
If you can give me a hint or the concept I’m looking for, you’ll save my day.
New contributor
brett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.