Hi everyone,
I have already read several post about this but haven’t found the answer.
Basically I would like to set some variables when a condition is met while looping over a list of dictionaries, and then stop the loop and keep the variables to use them afterwards.
This code shows when the condition is met (prints ok) but the loop keeps iterating and the content of the variables is lost :
`
- name: Check if the customer already exists in the Ops db
set_fact:
customer_found: true
customer_id: item[‘id’]
customer_name: item[‘customer_details’][‘name’]
customer_size: item[‘customer_details’][‘size’]
when: item[‘customer_details’][‘country’] == (tenant_country_code_2l | trim) and item[‘customer_details’][‘icn’] == tenant_icn_7d
with_items: “{{ customers_data }}”
`
This is the output:
`
TASK [… : Check if the customer already exists in the DB]
ok: [runner_ansible] => (item={u’id’: u’0’, u’customer_details’: {u’country’: u’DE’, u’size’: u’small’, u’icn’: u’1234569’, u’name’: u’CustomerTenant019’}})
skipping: [runner_ansible] => (item={u’id’: u’1’, u’customer_details’: {u’country’: u’FR’, u’size’: u’small’, u’icn’: u’999999’, u’name’: u’ODM team’}})
TASK [… : Print the customer id]
ok: [runner_ansible] => {}
MSG:
Customer id = item[‘id’]
`
The variable customer_id seems not to be assigned as ‘item[‘id’]’ is printed.
I’ve got the feeling there should be a better way to achieve this with ansible, and anyway, this code does not work.
Any help will be much appreciated.
Best,
Sabrina