Reputation: 382
I have this part of code that is responsible for adding rule to Imperva for specific server.
There is a lof of nested variables so rather than explaining all parts of it I will just focus on a problem.
The issue is there can't be two loops inside of this task.
I added loop: "{{ list_of_codes }}" and there is also old part of code using with_items -> with_items: "{{ ic_dc_stat_list.json | default({}) | json_query('DCs[?name==Fontend_USA
].id') }}"
This is the task I am talking about:
- name: "Add [USA] Rule"
run_once: true
delegate_to: 127.0.0.1
uri:
url: "{{ ic_api_url_sites_rules }}/add"
method: POST
validate_certs: false
body: "{{ ic_auth }}&\
site_id={{ _site_id }}&\
dc_id={{ ic_dc_stat_id }}&\
name=USA&\
filter=CountryCode == {{ item }}&\
{{ rules.action.frw_dc }}"
loop: "{{ list_of_codes }}"
vars:
ic_dc_stat_id: "{{ item }}"
_site_id: "{{ ic_site_stat.json.site_id | default(incapsula_brand_domain.site_id) | default(site_id)}}"
with_items: "{{ ic_dc_stat_list.json | default({}) | json_query('DCs[?name==`Fontend_USA`].id') }}"
when: ( not ic_rules.json.delivery_rules | json_query('Forward[?name==`USA`]') )
and ( ic_abigail and not ic_cs )
register: ic_rules_latam_stat
changed_when: "'skipped' not in ic_rules_latam_stat"
Loop list_of_codes should adding country codes here filter=CountryCode == {{ item }}.
This is details of mentioned loop:
list_of_codes:
- "MX" # Mexico
- "GT" # Guatemala
- "HN" # Honduras
- "NI" # Nicaragua
- "CU" # Cuba
- "DO" # Dominican Republic
- "CR" # Costa Rica
- "SV" # El Salvador
- "PA" # Panama
I am looking for the best way to implement list_of_codes, but how to do this when there is already one loop in task -> with_items: "{{ ic_dc_stat_list.json | default({}) | json_query('DCs[?name==Fontend_USA
].id') }}" which is used here: ic_dc_stat_id: "{{ item }}"
I though about jinja2, but now sure how to implement it here.
Please, help me to understand how to fix it.
Upvotes: 0
Views: 1220
Reputation: 68034
To focus on the problem, let's assume we have the lists below
list_of_codes:
- MX
- GT
list_of_ids:
- 1A
- 1B
If the cartesian product is needed the playbook below
shell> cat playbook.yml
- hosts: localhost
vars:
list_of_codes:
- MX
- GT
list_of_ids:
- 1A
- 1B
tasks:
- debug:
msg: "{{ item.0 }} - {{ item.1 }}"
with_nested:
- "{{ list_of_codes }}"
- "{{ list_of_ids }}"
gives
shell> ansible-playbook playbook.yml | grep msg:
msg: MX - 1A
msg: MX - 1B
msg: GT - 1A
msg: GT - 1B
Upvotes: 1