# firewalld loop through services/ports/sources within zones

**URL:** <https://forum.ansible.com/t/firewalld-loop-through-services-ports-sources-within-zones/30704>\
**Category:** Ansible Project\
**Created:** [July 3, 2019, 9:52am UTC](https://forum.ansible.com/t/firewalld-loop-through-services-ports-sources-within-zones/30704 "2019-07-03T09:52:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingfisher77](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@kingfisher77](https://forum.ansible.com/u/kingfisher77)\
**Post date:** [July 3, 2019, 9:52am UTC](https://forum.ansible.com/t/firewalld-loop-through-services-ports-sources-within-zones/30704/1 "2019-07-03T09:52:08Z")

</div>

Hi, we aim for a scenario which seems to be not so easy achievable.

We would like to kind of reset/automate the setup of firewalld on CentOS7. With walking through defined zones setting services/ports/sources.

What we have so far:

FIREWALLD\_ZONE\_SERVICE:

- ZONE: public  
SERVICES:

- SERVICE: http  
SERVICE\_STATE: disabled

- SERVICE: https  
SERVICE\_STATE: disabled

- ZONE: trusted

SERVICES:

- SERVICE: http  
SERVICE\_STATE: disabled

- SERVICE: https  
SERVICE\_STATE: disabled

- name: Debug FIREWALLD\_ZONE\_SERVICE

debug:  
msg: “{{ item.0.ZONE }} {{ item.1.SERVICE }} {{ item.1.SERVICE\_STATE }}”  
loop: “{{ FIREWALLD\_ZONE\_SERVICE|subelements(‘SERVICES’)|list }}”

Works like expected. Each zone gets configured for all given services with the related state.

We would like to condense things further and loop through zones. Something like that:

FIREWALLD\_ZONE\_SERVICE:

- ZONE:

- public

- trusted

- SERVICES:

- SERVICE: http  
SERVICE\_STATE: enabled

- SERVICE: https  
SERVICE\_STATE: enabled

How would the loop directive look like to loop through all zones looping through all services?

---

<div class="post-metadata">

**Author:** ![kingfisher77](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@kingfisher77](https://forum.ansible.com/u/kingfisher77)\
**Post date:** [July 4, 2019, 7:24am UTC](https://forum.ansible.com/t/firewalld-loop-through-services-ports-sources-within-zones/30704/2 "2019-07-04T07:24:07Z")

</div>

> Solved it:

`  
vars:  
FIREWALLD\_ZONE\_SERVICE\_CLEAR:  
ZONES:

- public
- trusted  
SERVICES:
- SERVICE: http  
STATE: disabled
- SERVICE: https  
STATE: disabled

`

Strokes at ZONES and SERVICES were wrong. They are not lists.

`  
task:

- name: Debug FIREWALLD\_ZONE\_SERVICE  
debug:  
msg: “{{ item.0 }} {{ item.1.SERVICE }}/{{ item.1.STATE }}”  
loop: “{{ FIREWALLD\_ZONE\_SERVICE.ZONES|product(FIREWALLD\_ZONE\_SERVICE.SERVICES)|list }}”  
`
