Bug#873890: ansible: enabling systemd service fails in chroot

2017-09-01 Thread Harlan Lieberman-Berg
tag 873890 +pending +upstream +fixed-upstream
forwarded 873890 https://github.com/ansible/ansible/issues/21026
thanks

Thanks for the report!  I'll cherry pick this patch in tonight.



Bug#873890: ansible: enabling systemd service fails in chroot

2017-08-31 Thread JD Friedrikson
Package: ansible
Version: 2.2.1.0-2
Severity: normal

Hello,

To summarize, my issue is the same as the one here:
https://github.com/ansible/ansible/issues/21026

Fixes have already been merged:
https://github.com/ansible/ansible/pull/21072
https://github.com/ansible/ansible/pull/23904 # mildly related, but maybe 
should be brought up

and should be already included in 2.3.1.0+dfsg-2 (testing and unstable).

Here are the git commits that resolve this:

```
commit 2f0070639f3af9afca3b0280f89346c12e7b7e12
Author: Brian Coca 
Date:   Mon Feb 6 13:05:29 2017 -0500

attempt to fix systemd in chroot env

fixes #21026

diff --git a/lib/ansible/modules/system/systemd.py 
b/lib/ansible/modules/system/systemd.py
index c82bed60d..c970bd3ff 100644
--- a/lib/ansible/modules/system/systemd.py
+++ b/lib/ansible/modules/system/systemd.py
@@ -297,6 +297,7 @@ def main():
 
 # check service data, cannot error out on rc as it changes across 
versions, assume not found
 (rc, out, err) = module.run_command("%s show '%s'" % (systemctl, unit))
+
 if rc == 0:
 # load return of systemctl show into dictionary for easy access and 
return
 multival = []
@@ -327,6 +328,18 @@ def main():
 if is_systemd and 'LoadError' in result['status']:
 module.fail_json(msg="Error loading unit file '%s': %s" % 
(unit, result['status']['LoadError']))
 
+elif out.find('ignoring request') != -1:
+# fallback list-unit-files as show does not work on some systems 
(chroot)
+# not used as primary as it skips some services (like those using 
init.d) and requires .service notation
+if unit.endswith('.service'):
+service = unit
+else:
+service = '%s.service' % unit
+(rc, out, err) = module.run_command("%s list-unit-files '%s'" % 
(systemctl, service))
+if rc == 0:
+is_systemd = True
+
+
 # Does service exist?
 found = is_systemd or is_initd
 if is_initd and not is_systemd:
```
```
commit fa93bf70968f0b65422bd020a4adf9484dde8751
Author: Brian Coca 
Date:   Wed Feb 8 15:05:11 2017 -0500

allow more than .service, onus on user

diff --git a/lib/ansible/modules/system/systemd.py 
b/lib/ansible/modules/system/systemd.py
index c970bd3ff..831637064 100644
--- a/lib/ansible/modules/system/systemd.py
+++ b/lib/ansible/modules/system/systemd.py
@@ -33,7 +33,7 @@ options:
 name:
 required: true
 description:
-- Name of the service.
+- Name of the service. When using in a chroot environment you 
always need to specify the full name i.e. (crond.service).
 aliases: ['unit', 'service']
 state:
 required: false
@@ -330,12 +330,8 @@ def main():
 
 elif out.find('ignoring request') != -1:
 # fallback list-unit-files as show does not work on some systems 
(chroot)
-# not used as primary as it skips some services (like those using 
init.d) and requires .service notation
-if unit.endswith('.service'):
-service = unit
-else:
-service = '%s.service' % unit
-(rc, out, err) = module.run_command("%s list-unit-files '%s'" % 
(systemctl, service))
+# not used as primary as it skips some services (like those using 
init.d) and requires .service/etc notation
+(rc, out, err) = module.run_command("%s list-unit-files '%s'" % 
(systemctl, unit))
 if rc == 0:
 is_systemd = True
```

As this does affect usability for the chroot connector I feel that these fixes 
should be backported.

Please let me know if I'm not doing this correctly or if you need anything more 
from me.

Cheers,
JD


-- System Information:
Debian Release: 9.1
  APT prefers stable
  APT policy: (500, 'stable'), (1, 'unstable')
Architecture: amd64 (x86_64)

Kernel: Linux 4.9.0-3-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_US.utf8, LC_CTYPE=en_US.utf8 (charmap=UTF-8), 
LANGUAGE=en_US.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages ansible depends on:
ii  python2.7.13-2
ii  python-crypto 2.6.1-7
ii  python-httplib2   0.9.2+dfsg-1
ii  python-jinja2 2.8-1
ii  python-netaddr0.7.18-2
ii  python-paramiko   2.0.0-1
ii  python-pkg-resources  33.1.1-1
ii  python-yaml   3.12-1

Versions of packages ansible recommends:
ii  python-kerberos   1.1.5-2+b2
ii  python-selinux2.6-3+b1
pn  python-winrm  
ii  python-xmltodict  0.10.2-1

Versions of packages ansible suggests:
pn  cowsay   
pn  sshpass  

-- no debconf information