# need to detect if a service/daemon is running or not

**URL:** <https://forum.ansible.com/t/need-to-detect-if-a-service-daemon-is-running-or-not/21247>\
**Category:** Ansible Project\
**Created:** [April 22, 2016, 4:06pm UTC](https://forum.ansible.com/t/need-to-detect-if-a-service-daemon-is-running-or-not/21247 "2016-04-22T16:06:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [April 22, 2016, 4:06pm UTC](https://forum.ansible.com/t/need-to-detect-if-a-service-daemon-is-running-or-not/21247/1 "2016-04-22T16:06:40Z")

</div>

I am trying to determine if iptables/firewalld is running or not. If the service/daemon is not running, I want to leave it that way. Otherwise, I would like to bounce it. Below is my attempt to perform this. If there is another/better way to do what I am trying to do, I am open to learn. 🙂

In vars…

net\_lst6:

- { portnum: “135”, inetprot: “tcp” }

- { portnum: “137:138”, inetprot: “udp” }

- { portnum: “139”, inetprot: “tcp” }

- { portnum: “445”, inetprot: “tcp” }

In tasks…

- name: check iptables status  
command: ‘service iptables status | grep -i “not” | wc -l’  
when: ansible\_distribution\_major\_version == “6”  
register: service\_status

- name: setup iptables  
iptables:  
chain: INPUT  
protocol: “{{item.inetprot}}”  
state: present  
destination\_port: “{{item.portnum}}”  
when: ( “ansible\_distribution\_major\_version” == “6” ) and ( “service\_status” != “1” )  
with\_items: “{{net\_lst6}}”  
notify: restart iptables

I am getting the error below.

TASK [test : check iptables status] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [server1]: FAILED! =\> {“changed”: true, “cmd”: [“service”, “iptables”, “status”, “|”, “grep”, “-i”, “not”, “|”, “wc”, “-l”], “delta”: “0:00:00.066634”, “end”: “2016-04-22 08:51:36.866972”, “failed”: true, “rc”: 3, “start”: “2016-04-22 08:51:36.800338”, “stderr”: “”, “stdout”: “iptables: Firewall is not running.”, “stdout\_lines”: [“iptables: Firewall is not running.”], “warnings”: [“Consider using service module rather than running service”]}

Help.

Thank you in advance.

Regards,  
J

---

<div class="post-metadata">

**Author:** ![Moshe\_Suberri](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@Moshe\_Suberri](https://forum.ansible.com/u/Moshe_Suberri)\
**Post date:** [April 22, 2016, 4:42pm UTC](https://forum.ansible.com/t/need-to-detect-if-a-service-daemon-is-running-or-not/21247/2 "2016-04-22T16:42:39Z")

</div>

In general, would a problem like this be better solved by creating a bash/python script and have Ansible run it on the target machine?

Just a thought.

M

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [April 25, 2016, 12:51pm UTC](https://forum.ansible.com/t/need-to-detect-if-a-service-daemon-is-running-or-not/21247/3 "2016-04-25T12:51:42Z")

</div>

If firewalld then you are using systemd and then it is as simple as:

`systemctl is-active firewalld`

Edgars

piektdiena, 2016. gada 22. aprīlis 18:07:51 UTC+2, Jerome Yanga rakstīja:
