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.
- 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
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.
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....
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.
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
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.
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.
The 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.