# restart service only if its successful

**URL:** <https://forum.ansible.com/t/restart-service-only-if-its-successful/38211>\
**Category:** Ansible Project\
**Created:** [July 10, 2023, 7:17pm UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211 "2023-07-10T19:17:04Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [July 10, 2023, 7:17pm UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/1 "2023-07-10T19:17:04Z")

</div>

Hello team,

I am trying to restart zookeeper service on all 3 nodes sequentially , we want restart zookeeper service one by one but it will be only run on 2nd server if 1st server zookeeper service was successful. How do i achieve this in 1 task .

task should fail if service is not properly restarted.

- name: restart zookeeper one by one on follower first and ensure all is good

throttle: 1

service:

name: ‘confluent-zookeeper’

state: restarted

when: not zkmode.stdout\_lines is search(‘leader’)

- name: check follower zookeeper are up and running

shell: ‘systemctl status confluent-zookeeper -l| grep -i error || systemctl status confluent-zookeeper | grep failed’

register: zkstatus

failed\_when: zkstatus.rc == 0

Now in this case , 1 task is getting executed on all hosts despite having error in logs . I want it to be failed as soon as it has error and should not continue on next server.

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [July 11, 2023, 2:26pm UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/2 "2023-07-11T14:26:27Z")

</div>

I have used shell module and failed when combination to achieve this.

- name: Get running processes list from remote host

shell: “ps -efw | grep -e zookeeper.properties | grep -v grep |awk ‘{print $2}’”

register: runningzkprc

- name: Kill running processes

throttle: 1

# ignore\_errors: yes

shell: |

kill -9 “{{ runningzkprc.stdout\_lines[0] }}”

sleep 3

sleep 3

systemctl start confluent-zookeeper510

systemctl status confluent-zookeeper510 -l| grep -i error

register: zkstart

failed\_when: zkstart.rc != 0

when: not zkmode.stdout\_lines is search(‘leader’)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 11, 2023, 4:29pm UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/3 "2023-07-11T16:29:43Z")

</div>

Regrettably, pgrep and pkill seem widely unknown.

```auto
- name: Kill zookeeper processes and restart service
  ansible.builtin.shell: |
    if pkill --signal 9 -f zookeeper.properties ; then
       sleep 6
       systemctl start confluent-zookeeper510
    fi
    systemctl status confluent-zookeeper510
  register: zkstart

```

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [July 12, 2023, 7:59am UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/4 "2023-07-12T07:59:58Z")

</div>

First of all Thanks a lot for introducing me pkill way otherwise i would have written one more task to register the process id.

Many thanks Todd.

Secondly, as its impossible to explore entire tool like ansible hence just asking you the question, Does it even possible to do above things using ansible module in single task???  
like find process id of process and kill if it doesnt exist do not proceed with other hosts as well.

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [July 12, 2023, 10:53am UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/5 "2023-07-12T10:53:06Z")

</div>

Hi Todd,

I tried to run script with shell module but it fails with below error,

[kafka-3: FAILED! =\> {“changed”: true, “cmd”: “if pkill --signal 9 -f zookeeper.properties ; then\nsystemctl start confluent-zookeeper510\nelse\nexit 1\nfi\nsystemctl status confluent-zookeeper510\n”, “delta”: “0:00:00.034111”, “end”: “2023-07-12 10:31:58.344951”, “failed\_when\_result”: true, “msg”: “non-zero return code”, “rc”: -9, “start”: “2023-07-12 10:31:58.310840”, “stderr”: “”, “stderr\_lines”: , “stdout”: “”, “stdout\_lines”: }

fatal: [kafka-4]: FAILED! =\> {“changed”: true, “cmd”: “if pkill --signal 9 -f zookeeper.properties ; then\nsystemctl start confluent-zookeeper510\nelse\nexit 1\nfi\nsystemctl status confluent-zookeeper510\n”, “delta”: “0:00:00.032830”, “end”: “2023-07-12 10:31:59.744091”, “failed\_when\_result”: true, “msg”: “non-zero return code”, “rc”: -9, “start”: “2023-07-12 10:31:59.711261”, “stderr”: “”, “stderr\_lines”: , “stdout”: “”, “stdout\_lines”: }

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [July 12, 2023, 10:57am UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/6 "2023-07-12T10:57:54Z")

</div>

Infact I tried code pasted by still gives same error.

- name: Kill running java processes and start newly configured systemd

throttle: 1

# ignore\_errors: yes

any\_errors\_fatal: true

shell: |

if pkill --signal 9 -f zookeeper.properties ; then

sleep 6

systemctl start confluent-zookeeper510

fi

systemctl status confluent-zookeeper510

register: zkstart

failed\_when: zkstart.rc != 0

when: not zkmode.stdout\_lines is search(‘leader’)

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [July 12, 2023, 11:30am UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/7 "2023-07-12T11:30:51Z")

</div>

Hii,

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 12, 2023, 11:34am UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/8 "2023-07-12T11:34:38Z")

</div>

> Hii,
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Hi Todd,
> 
> &nbsp;&nbsp;&nbsp;&nbsp;I tried to run script with shell module but it fails with below error,
> 
> &nbsp;&nbsp;&nbsp;&nbsp;[kafka-3: FAILED! =\> {"changed": true, "cmd": "if pkill --signal 9 -f zookeeper.properties ; then\nsystemctl start confluent-zookeeper510\nelse\nexit 1\nfi\nsystemctl status confluent-zookeeper510\n", "delta": "0:00:00.034111", "end": "2023-07-12 10:31:58.344951", "failed\_when\_result": true, "msg": "non-zero return code", "rc": -9, "start": "2023-07-12 10:31:58.310840", "stderr": "", "stderr\_lines": , "stdout": "", "stdout\_lines": }
> 
> &nbsp;&nbsp;&nbsp;&nbsp;fatal: [kafka-4]: FAILED! =\> {"changed": true, "cmd": "if pkill --signal 9 -f zookeeper.properties ; then\nsystemctl start confluent-zookeeper510\nelse\nexit 1\nfi\nsystemctl status confluent-zookeeper510\n", "delta": "0:00:00.032830", "end": "2023-07-12 10:31:59.744091", "failed\_when\_result": true, "msg": "non-zero return code", "rc": -9, "start": "2023-07-12 10:31:59.711261", "stderr": "", "stderr\_lines": , "stdout": "", "stdout\_lines": }
> 
> &nbsp;&nbsp;&nbsp;&nbsp;===========================================================================
> 
> &nbsp;&nbsp;&nbsp;&nbsp;code:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Kill running java processes and start newly configured systemd
> 
> &nbsp;&nbsp;&nbsp;&nbsp;throttle: 1
> 
> &nbsp;&nbsp;&nbsp;&nbsp;#ignore\_errors: yes
> 
> &nbsp;&nbsp;&nbsp;&nbsp;any\_errors\_fatal: true
> 
> &nbsp;&nbsp;&nbsp;&nbsp;shell: |
> 
> &nbsp;&nbsp;&nbsp;&nbsp;if [pkill --signal 9 -f zookeeper.properties] ; then
> 
> &nbsp;&nbsp;&nbsp;&nbsp;systemctl start confluent-zookeeper510; sleep 2; echo "stat" | nc localhost 2181;st="$?" systemctl status confluent-zookeeper510 -l | grep -v ERRROR;et="$?"
> 
> I feel we're going down a rabbit hole trying to fight ill designed systemd units with shell hacks (which include typos? ERRROR instead of ERROR).  
> My approach would be to make sure the systemd unit is doing what it should do, and then rely on that to do its job....

Certainly!

Regards

```
      Racke

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 12, 2023, 12:07pm UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/9 "2023-07-12T12:07:26Z")

</div>

Maybe you should be using the systemctl action “reload-or-restart”?

Killing service processes out from under systemd should not be a thing.

And as Dick Visser said, we’re straying from Ansible issues.

To your other question:

> Secondly, as its impossible to explore entire tool like ansible hence just asking you the question, Does it even possible to do above things using ansible module in single task???  
> like find process id of process and kill if it doesnt exist do not proceed with other hosts as well.

I know of no ansible module (besides “ansible.builtin.shell”) that would let you do all that in a single task. But again, unless the service is broken or the units are basically wrong, you shouldn’t need to do that. The service manager should be able to handle that for you, thus, a single task should suffice.

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [July 12, 2023, 2:47pm UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/10 "2023-07-12T14:47:09Z")

</div>

I get it . But this is specific case where my current process is not managed by systemd so i had to use pkill anyways.

Now issue is shell module with if else is not working for me correctly. Like its giving rc -9. if i copy same shell script and rung using shell module like bash /tmp/zkproc.sh it works.

Reason to post here is due to shell module multiple command not working as expected. Below is shell script. Now i had to use grep -v “ERROR;et=”$?" because sometime process does says its running but log has some errors which cant be ignored.

if pkill --signal 9 -f zookeeper.properties ; then

sleep 2;systemctl start confluent-zookeeper510; sleep 2; echo “stat” | nc localhost 2181;st=“$?”

systemctl status confluent-zookeeper5910 -l | grep -v ERROR;et=“$?”

[[$st -eq 0 && $et -eq 0]] && exit 0 || exit 1

else

exit 1

fi

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [July 13, 2023, 6:51pm UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/11 "2023-07-13T18:51:40Z")

</div>

Hello Todd/Dick/Stephan,

This is how i did, do u thnink any of below tasks can still be handled otherwise (more accurately) or this is the best way.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 13, 2023, 7:35pm UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/12 "2023-07-13T19:35:39Z")

</div>

Any reason you don’t want to follow [https://docs.confluent.io/ansible/current/overview.html](https://docs.confluent.io/ansible/current/overview.html) (“Ansible Playbooks for Confluent Platform”)?

It’s not clear how you got zookeeper and friends installed on these hosts without the benefit of a service manager. In any case, I’m surprised you’re putting the systemd unit files elsewhere from /etc/systemd/system. And I don’t see you enabling or starting those services.

Be sure to run ansible-lint and consider taking its recommendations.

Otherwise, without spinning up unmanaged instances myself, I don’t have anything else to comment on.

---

<div class="post-metadata">

**Author:** ![Sameer\_Modak](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sameer_modak/32/5180_2.png) [@Sameer\_Modak](https://forum.ansible.com/u/Sameer_Modak)\
**Post date:** [July 14, 2023, 8:20am UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/13 "2023-07-14T08:20:25Z")

</div>

Hi Todd,  
Morning!!!

Reason i didn’t follow confluent playbook is because i had to develop by our own for customized services. Obviously i can take hind out of it.

Second thing why i did nt use systemd is because playbook is getting bigger but yeah u made good point i forgot to enable the serivce(by the way its there in that shell script i m running to check services its all in one)

I would like to appreciate you on suggesting ansible-lint i knew but never thaught of using but seems now its the time.

Last thing how do I appreciate your replies/time please let me know. Your responses encourages people to use ansible more and more.

Obviously its hard to remember syntax some shorthand tricks when u dont use that tool for 3 months but your replies made it easy.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 14, 2023, 11:11am UTC](https://forum.ansible.com/t/restart-service-only-if-its-successful/38211/14 "2023-07-14T11:11:15Z")

</div>

The [cp-ansible](https://github.com/confluentinc/cp-ansible) project is a bit overwhelming, but it’s written by the people who know the software the best. At least reading through it is instructive, not only from a Confluent standpoint, but as an example of how a relatively large Ansible project can be structured.

As for “how do I appreciate your replies/time please let me know” — ironically, the best way to show your appreciation for people who answer questions on forums like this is to _ask interesting questions!_ Such people are here because they learn from the real-world problems actual users run into, and the challenge of producing solutions to those problems makes them better at their craft.

Good luck with your project.
