Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Ansible fail when the systemd service fails to start?

I have a systemd service that I deploy and want to be started by Ansible.

My systemd service unit file is this:

[Unit]
Description=Collector service
After=network.target mariadb.service
Requires=mariadb.service

[Service]
Type=simple
ExecStart=/opt/collector/app.py
WorkingDirectory=/opt/collector
Restart=on-abort
User=root

[Install]
WantedBy=multi-user.target

I am using Type=simple since this looks like the correct solution (also the preferred one in this Question).

I tried using Type=oneshot as well (as suggested by the initial user making this question as duplicate of this question) but the problem is that the /opt/collector/app.py script is a long running process:

while True:
    t = threading.Thread(...)
    t.start()
    t.join()
    time.sleep(15)

and with Type=oneshot, Ansible will block forever.

And my Ansible starting code is:

- name: start Collector service
  systemd:
    name: collector
    state: started
    enabled: yes

On the target system, systemctl will display:

[root@srv01 /]# systemctl
  UNIT                           LOAD   ACTIVE     SUB       DESCRIPTION
  dev-sda1.device                loaded activating tentative /dev/sda1
  -.mount                        loaded active     mounted   /
  dev-mqueue.mount               loaded active     mounted   POSIX Message Queue File System
  etc-hostname.mount             loaded active     mounted   /etc/hostname
  etc-hosts.mount                loaded active     mounted   /etc/hosts
  etc-resolv.conf.mount          loaded active     mounted   /etc/resolv.conf
  run-user-0.mount               loaded active     mounted   /run/user/0
  session-73.scope               loaded active     running   Session 73 of user root
  crond.service                  loaded active     running   Command Scheduler
  dbus.service                   loaded active     running   D-Bus System Message Bus
  haproxy.service                loaded active     running   HAProxy Load Balancer
<E2><97><8F> collector.service          loaded failed     failed   Collector service
....

The service fails because of the Python process exception (using un undefined variable).

But my Ansible playbook run does not fail:

TASK [inventory : start Collector service] *********************************
changed: [srv01]

I tried with both systemd and service Ansible modules and the behavior is the same.

How can I make Ansible:

  • fail when the systemd unit fails to start?
  • not block and systemd getting in active running status with a while True process?
like image 688
4 revs, 2 users 76% Avatar asked Jul 29 '18 19:07

4 revs, 2 users 76%


People also ask

How do you control command failure in ansible?

Ignoring failed commands By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.

How do you fail ansible tasks?

In ansible, fail module is avialble to fail ansible playbook with custom message. fail module is used to exit the progress when certain condition is met. msg parameter is used to specify the custome message. msg parameter is mandatory and default value is 'Failed as requested from task.

What is systemd in ansible?

The systemd module in Ansible is straightforward to use. For example, to start a service, pass the name of the service and the state you want it to be, in this case, started. The following example playbook shows how to start a service using the systemd module. --- - name: Ansible start service.

How do you stop ansible playbook?

You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely. To continue early: press ctrl+c and then c . To abort a playbook: press ctrl+c and then a . Or just a straight - fail: if you will certainly not want to continue.


1 Answers

I stumbled over this while I had the same problem with silently failing serives. I also found a bug report describing this issue and after some research I found a workaround:

- name: start Collector service
  systemd:
    name: collector
    state: started
    enabled: yes

- name: make sure Collector service is really running
  command: systemctl is-active collector

Note that for Type=simple services this will only fail if the service itself fails immediately after it was started.

like image 55
harmathy Avatar answered Sep 28 '22 00:09

harmathy