Repository: ansible/ansible

Issue:
firewalld module fails with "global name 'fw_offline' is not defined" error
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!---
Insert below this comment the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path
-->
firewalld

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.0
  config file = /home/torben/development/local-fedora-setup/ansible.cfg
  configured module search path = [u'/home/torben/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.14 (default, Mar 14 2018, 16:45:33) [GCC 8.0.1 20180222 (Red Hat 8.0.1-0.16)]
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
```
DEFAULT_BECOME(/home/torben/development/local-fedora-setup/ansible.cfg) = True
```

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
```
[torben@localhost local-fedora-setup]$ uname -a
Linux localhost.localdomain 4.16.0-0.rc7.git0.1.fc28.x86_64 #1 SMP Mon Mar 26 15:01:02 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[torben@localhost local-fedora-setup]$ cat /etc/fedora-release 
Fedora release 28 (Twenty Eight)
[torben@localhost local-fedora-setup]$ 
```

##### SUMMARY
<!--- Explain the problem briefly -->
```yaml
- name: open glusterfs client tcp firewall ports
  firewalld:
    permanent: yes
    immediate: yes
    port: 24007-24008/tcp
    state: enabled
```
fails with
```
TASK [nas : open glusterfs client tcp firewall ports] ************************************************************************************************************************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NameError: global name 'fw_offline' is not defined
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_CI2pJm/ansible_module_firewalld.py\", line 1020, in <module>\n    main()\n  File \"/tmp/ansible_CI2pJm/ansible_module_firewalld.py\", line 814, in main\n    if fw_offline:\nNameError: global name 'fw_offline' is not defined\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
```
##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
see above

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: open glusterfs client tcp firewall ports
  firewalld:
    permanent: yes
    immediate: yes
    port: 24007-24008/tcp
    state: enabled
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
no error, firewalld ports got opened

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
TASK [nas : open glusterfs client tcp firewall ports] ************************************************************************************************************************************************************************************************************************************************************************
task path: /home/torben/development/local-fedora-setup/playbooks/roles/nas/tasks/main.yml:8
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/firewalld.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: torben
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/torben/.ansible/tmp/ansible-tmp-1522509661.54-148889351364616 `" && echo ansible-tmp-1522509661.54-148889351364616="` echo /home/torben/.ansible/tmp/ansible-tmp-1522509661.54-148889351364616 `" ) && sleep 0'
<127.0.0.1> PUT /home/torben/.ansible/tmp/ansible-local-4337yth7ww/tmpmAW_uv TO /home/torben/.ansible/tmp/ansible-tmp-1522509661.54-148889351364616/firewalld.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/torben/.ansible/tmp/ansible-tmp-1522509661.54-148889351364616/ /home/torben/.ansible/tmp/ansible-tmp-1522509661.54-148889351364616/firewalld.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-yxcdkerskztokdvxiouambfrzitvigxr; /usr/bin/python2 /home/torben/.ansible/tmp/ansible-tmp-1522509661.54-148889351364616/firewalld.py'"'"' && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/torben/.ansible/tmp/ansible-tmp-1522509661.54-148889351364616/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_LC7BCv/ansible_module_firewalld.py", line 1020, in <module>
    main()
  File "/tmp/ansible_LC7BCv/ansible_module_firewalld.py", line 814, in main
    if fw_offline:
NameError: global name 'fw_offline' is not defined

fatal: [localhost]: FAILED! => {
    "changed": false, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_LC7BCv/ansible_module_firewalld.py\", line 1020, in <module>\n    main()\n  File \"/tmp/ansible_LC7BCv/ansible_module_firewalld.py\", line 814, in main\n    if fw_offline:\nNameError: global name 'fw_offline' is not defined\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE", 
    "rc": 1
}
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/system/firewalld.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/system/firewalld.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @maxamillion
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@j1cken Thanks for reporting this issue. This PR https://github.com/ansible/ansible/pull/37603 will resolve this issue. 
@Akasurde did you test that #37603 does fix this?
@maxamillion From my preliminary testing PR works for this issue.
@Akasurde awesome, greatly appreciated!
I'm affected by this as well, is there a test build with the PR for users to test? Maybe from CI?

Resolving patch:
diff --git a/lib/ansible/modules/system/firewalld.py b/lib/ansible/modules/system/firewalld.py
--- a/lib/ansible/modules/system/firewalld.py
+++ b/lib/ansible/modules/system/firewalld.py
@@ -155,6 +155,8 @@
 
 # globals
 module = None
+fw_offline = False
+FW_VERSION = None
 
 # Imports
 try:
@@ -165,7 +167,6 @@
     from firewall.client import FirewallClient
     from firewall.client import FirewallClientZoneSettings
     fw = None
-    fw_offline = False
     import_failure = False
 
     try:


Test patch: