Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

"service: enabled=yes" do not enable systemd service #3764

Closed
r0bj opened this issue May 27, 2016 · 11 comments
Closed

"service: enabled=yes" do not enable systemd service #3764

r0bj opened this issue May 27, 2016 · 11 comments

Comments

@r0bj
Copy link

r0bj commented May 27, 2016

ISSUE TYPE
  • Bug Report
COMPONENT NAME

service module

ANSIBLE VERSION
ansible 2.0.2.0
CONFIGURATION
[defaults]
timeout = 40
hash_behaviour = merge
gathering = smart
jinja2_extensions = jinja2.ext.do,jinja2.ext.loopcontrols
retry_files_enabled = False

[ssh_connection]
pipelining = True

[privilege_escalation]
become = True
become_ask_pass = True
OS / ENVIRONMENT

OS X 10.11.5
Managed OS: ubuntu 16.04

SUMMARY

"service: enabled=yes" do not enable systemd service in case when service is disabled in systemd and at the same time enabled in sysvinit scripts

STEPS TO REPRODUCE

Task:

- service: name=zabbix-server enabled=yes

Service is disabled in systemd:

# systemctl status zabbix-server.service
● zabbix-server.service - Zabbix Server (MySQL/MariaDB)
   Loaded: loaded (/lib/systemd/system/zabbix-server.service; disabled; vendor preset: enabled)

At the same time service is enabled in sysvinit:

# ls -la /etc/rc?.d/S??zabbix-server
lrwxrwxrwx 1 root root 23 May 30 17:34 /etc/rc2.d/S03zabbix-server -> ../init.d/zabbix-server
lrwxrwxrwx 1 root root 23 May 30 17:34 /etc/rc3.d/S03zabbix-server -> ../init.d/zabbix-server
lrwxrwxrwx 1 root root 23 May 30 17:34 /etc/rc4.d/S03zabbix-server -> ../init.d/zabbix-server
lrwxrwxrwx 1 root root 23 May 30 17:34 /etc/rc5.d/S03zabbix-server -> ../init.d/zabbix-server
EXPECTED RESULTS

Make service zabbix-server enabled - service should start on boot:

# systemctl status zabbix-server.service
● zabbix-server.service - Zabbix Server (MySQL/MariaDB)
   Loaded: loaded (/lib/systemd/system/zabbix-server.service; enabled; vendor preset: enabled)
ACTUAL RESULTS

Ansible task do not enable service. Task finished with changed: false status:

TASK [zabbix-server : service] *************************************************
task path: /Users/rob/ansible/sysops/roles/zabbix-server/tasks/main.yml:47
<z01> ESTABLISH SSH CONNECTION FOR USER: None
<z01> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=40 -o ControlPath=/Users/rob/.ansible/cp/ansible-ssh-%h-%p-%r z01 '/bin/sh -c '"'"'sudo -H -S  -p "[sudo via ansible, key=hhyazmleyrvcyzepgzvnakvfhcoazewd] password: " -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-hhyazmleyrvcyzepgzvnakvfhcoazewd; LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python'"'"'"'"'"'"'"'"''"'"''
ok: [z01] => {"changed": false, "enabled": true, "invocation": {"module_args": {"arguments": "", "enabled": true, "name": "zabbix-server", "pattern": null, "runlevel": "default", "sleep": null, "state": null}, "module_name": "service"}, "name": "zabbix-server"}
# systemctl status zabbix-server.service
● zabbix-server.service - Zabbix Server (MySQL/MariaDB)
   Loaded: loaded (/lib/systemd/system/zabbix-server.service; disabled; vendor preset: enabled)
@lihan
Copy link

lihan commented May 31, 2016

Same problem here with supervisor.

Walk around this problem by using this task

- name: Ensure supervisor launches on boot
  become: yes
  command: systemctl enable supervisor

Server Ubuntu 16.04 LTS,
local ansible 2.1.0.0

@ansibot
Copy link

ansibot commented Jul 30, 2016

@grossws, ping. This issue is still waiting on your response.
click here for bot help

@olfway
Copy link

olfway commented Aug 11, 2016

Actually that's pretty easy to fix

I hit this issue with zabbix-agent on debian 8 jessie with systemd with latest ansible release 2.1.1.0

zabbix-agent package has both sysv's /etc/init.d/zabbix-agent script and systemd's /lib/systemd/system/zabbix-agent.service service

there is a code in service.py:

(rc, out, err) = self.execute_command("%s is-enabled %s" % (self.enable_cmd, service_name,))
if rc == 0:
    return True
elif sysv_exists(service_name):
    return sysv_is_enabled(service_name)
else:
    return False

ie it checks if service is-enabled in systemd and then if it's enabled in sysv
that's logic correct if you have only systemd service file or sysv start script

for example:
systemctl is-enabled 'some-sysv-only-service' returns rc=1, err='Failed to get unit file state for some-sysv-only-service.service: No such file or directory' and then correct state from sysv returned

but, if you have both:
systemctl is-enabled zabbix-agent returns rc=1, out="disabled\n" and then sysv_is_enabled() return True. so ansible thinks it's enabled, but actually systemd will not start it

this patch fixes issue for me:

--- service.py.orig 2016-08-11 12:32:00.000000000 +0300
+++ service.py  2016-08-11 12:56:31.000000000 +0300
@@ -496,6 +496,8 @@
         (rc, out, err) = self.execute_command("%s is-enabled %s" % (self.enable_cmd, service_name,))
         if rc == 0:
             return True
+        elif out.startswith('disabled'):
+            return False
         elif sysv_exists(service_name):
             return sysv_is_enabled(service_name)
         else:

ie if 'systemctl is-enabled foo' returns 'disabled\n', then report it's 'disabled'

bcoca added a commit that referenced this issue Aug 11, 2016
when both unit file and sysv init script exist
Thanks to @olfway for fix.

fixes #3764

(cherry picked from commit 92b4ae4)
@bcoca bcoca closed this as completed in 92b4ae4 Aug 11, 2016
@lukasojd
Copy link

+1

trbs added a commit to trbs/ansible-modules-core that referenced this issue Nov 12, 2016
when both unit file and sysv init script exist

fixes ansible#3764
also see ansible@92b4ae4
trbs added a commit to trbs/ansible-modules-core that referenced this issue Nov 12, 2016
when both unit file and sysv init script exist

fixes ansible#3764
also see ansible@92b4ae4
@ghost
Copy link

ghost commented Nov 14, 2016

Nice! This is happening with many other services as well, for example, instead of running:

- service: name=dpdk enabled=yes

I have to run:

- shell: systemctl enable dpdk

Then, it works! With the patch applied against my local Ansible, problem solved! Sevice module back to business.

Please, make this change available on Ansible's Ubuntu PPA! :-D

@splitice
Copy link

+1 Agreed this bug is very annoying

@darkweaver87
Copy link

+1

@landro
Copy link

landro commented Feb 10, 2017

Reproduced in ansible 2.2.1.0
Fix was to delete /etc/init.d/xxxx
Please reopen @bcoca

@danopia
Copy link

danopia commented Feb 10, 2017

I worked around by having two steps:

- service: name=supervisor enabled=no
- service: name=supervisor enabled=yes

The disable step fixes enough state for the enable step to work properly

Was super annoying to find this issue.

@lukasojd
Copy link

lukasojd commented Feb 22, 2017

My worked aroud is
- name: Enabled service command: systemctl reenable my.service

@gaudenz
Copy link

gaudenz commented Mar 6, 2017

To everyone that still has this issue despite it being fixed this is probably because of ansible/ansible#22303. If the service action plugin detects an OS using systemd the service task does not use the service module but the systemd module unless you set the use: service parameter on the task.

As a workaround until the bug is fixed you can use:

- name: Enalbe my service
  service:
    name: myservice
    enabled: True
    use: service

While this is still a workaround it's a lot cleaner than just calling systemctl directly or disabling and the reenabling the service.

emielmolenaar added a commit to emielmolenaar/ansible-role-pptpd that referenced this issue Apr 7, 2017
andrewhowdencom added a commit to sitewards/ansible-role-varnish that referenced this issue Jul 11, 2017
Currently, there appears to be an issue with Ansible in which, if a
service is already enabled with sysvinit, the service will not be
enabled with Ansible. This appears to be the condition with this
repository; likely, the upstream package is bringing in the sysvinit
script (as it's not being deployed by ansible) and enabling it.

This commit implements a workaround as documented
ansible/ansible-modules-core#3764 (comment).
While, confusingly, the package is "using" the sysvinit style service
tool for enabling the service, systemd implements a compatibility layer
that allows enabling it via this shim. This correctly enables the
service.

See ansible/ansible#22303
See geerlingguy/ansible-role-php@84ab337
tobiasdam added a commit to usableprivacy/upribox that referenced this issue Jul 11, 2017
dj-wasabi added a commit to dj-wasabi/ansible-zabbix-agent that referenced this issue Aug 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.