# Syntax error while running Firewall playbook

**URL:** <https://forum.ansible.com/t/syntax-error-while-running-firewall-playbook/29310>\
**Category:** Ansible Project\
**Created:** [December 20, 2018, 6:41am UTC](https://forum.ansible.com/t/syntax-error-while-running-firewall-playbook/29310 "2018-12-20T06:41:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prashant\_Jaiswal](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@Prashant\_Jaiswal](https://forum.ansible.com/u/Prashant_Jaiswal)\
**Post date:** [December 20, 2018, 6:41am UTC](https://forum.ansible.com/t/syntax-error-while-running-firewall-playbook/29310/1 "2018-12-20T06:41:40Z")

</div>

Hi,

While running a playbook to enable multiple TCP & UDP ports on a remote m/c I am getting some syntax error which I can’t figure it out. The error says FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘item’ is undefined\n\nThe error appears to have been in ‘/etc/ansible/playbooks/enable\_firewall\_ports.yml’: line 13, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Enable TCP and UDP ports\n ^ here\n”}

Below is the code

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [December 20, 2018, 7:52am UTC](https://forum.ansible.com/t/syntax-error-while-running-firewall-playbook/29310/2 "2018-12-20T07:52:23Z")

</div>

Hi Prashant,

It looks like your “with\_items” is not lined up correctly with firewalld so the list is not passed to {{item.port}}

i.e. try this

- name: Enable TCP and UDP ports  
firewalld:  
port: ‘{{item.port}}/{{item.protocol}}’  
immediate: true

permanent: true  
state: enabled

with\_items:

- { port: “5301,1302,5903,5672”, protocol: tcp }
- { port: “25238,55692”, protocol: udp}

tags: port

---

<div class="post-metadata">

**Author:** ![Ravi\_Ranjan](https://avatars.discourse-cdn.com/v4/letter/r/ecc23a/32.png) [@Ravi\_Ranjan](https://forum.ansible.com/u/Ravi_Ranjan)\
**Post date:** [December 20, 2018, 8:32am UTC](https://forum.ansible.com/t/syntax-error-while-running-firewall-playbook/29310/3 "2018-12-20T08:32:17Z")

</div>

It should be like this and it will work for sure .

- name: Enable TCP and UDP ports  
firewalld:  
port: “{{item.port}}”/“{{item.protocol}}”  
immediate: true

permanent: true  
state: enabled

with\_items:

- { port: “5301,1302,5903,5672”, protocol: tcp }
- { port: “25238,55692”, protocol: udp}

tags: port

Ravi

---

<div class="post-metadata">

**Author:** ![Prashant\_Jaiswal](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@Prashant\_Jaiswal](https://forum.ansible.com/u/Prashant_Jaiswal)\
**Post date:** [December 20, 2018, 9:28am UTC](https://forum.ansible.com/t/syntax-error-while-running-firewall-playbook/29310/4 "2018-12-20T09:28:59Z")

</div>

Hi Tony,

That issue got fixed. Corrected the indentation. Now while running the playbook I am facing this error. Unable to pass multiple ports. Below is the error.

failed: [0.0.0.0] (item={u’protocol’: u’tcp’, u’port’: [5301, 1302, 5903, 5672, 5672]}) =\> {“changed”: false, “item”: {“port”: [5301, 1302, 5903, 5672, 5672], “protocol”: “tcp”}, “msg”: “ERROR: Exception caught: org.fedoraproject.FirewallD1.Exception: INVALID\_PORT: ‘[5301, 1302, 5903, 5672, 5672]’ is invalid port range Permanent and Non-Permanent(immediate) operation”}

My playbook look like this

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 20, 2018, 2:31pm UTC](https://forum.ansible.com/t/syntax-error-while-running-firewall-playbook/29310/5 "2018-12-20T14:31:56Z")

</div>

> failed: [0.0.0.0] (item={u'protocol': u'tcp', u'port': [5301, 1302, 5903,  
> 5672, 5672]}) =\> {"changed": false, "item": {"port": [5301, 1302, 5903,  
> 5672, 5672], "protocol": "tcp"}, "msg": "ERROR: Exception caught:  
> org.fedoraproject.FirewallD1.Exception: INVALID\_PORT: '[5301, 1302, 5903,  
> 5672, 5672]' is invalid port range Permanent and Non-Permanent(immediate)  
> operation"}

It says invalid port, no place in the documentation say that you can send a list in the port section.  
Only individual port or range with a dash.

> My playbook look like this  
> ---  
> - hosts: test  
> &nbsp;&nbsp;become : True  
> &nbsp;&nbsp;become\_user : root  
> &nbsp;&nbsp;become\_method : sudo  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Start and enable firewalld  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: firewalld  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: started  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enabled: yes
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Enable TCP and UDP ports  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firewalld:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: ' {{item.port}}/{{item.protocol}}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;immediate: true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;permanent: true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: enabled  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { port: [5301, 1302, 5903, 5672, 5672], protocol: tcp }  
> # - { port: [25238,55692], protocol: udp }

The easiest is to just list them like so and use {{ item }} instead

with\_items:  
&nbsp;&nbsp;- 5301/tcp  
&nbsp;&nbsp;- 1302/tcp  
&nbsp;&nbsp;- 25238(udp  
&nbsp;&nbsp;...  
&nbsp;&nbsp;...

---

<div class="post-metadata">

**Author:** ![Prashant\_Jaiswal](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@Prashant\_Jaiswal](https://forum.ansible.com/u/Prashant_Jaiswal)\
**Post date:** [December 21, 2018, 6:59am UTC](https://forum.ansible.com/t/syntax-error-while-running-firewall-playbook/29310/6 "2018-12-21T06:59:25Z")

</div>

Hi ,

Works now. Thank you 🙂
