Repository: ansible/ansible

Issue:
timezone cannot change timezone in systemd based containers
_From @kayhayen on December 3, 2016 8:49_

In my playbook, I got the following:

```
  - name: Set timezone to CEST.
    timezone:
        name: Europe/Berlin
    when: "'{{ansible_virtualization_role}}' != 'guest'"
```

The condition was added as a workaround, because in the Debian Stretch, systemd based LXC container, timedatectl reports:

> Failed to create bus connection: No such file or directory

This is likely because systemd-timesyncd and systemd-timedated are disabled in virtual machines, but Ansible insists on using them to change the timezone.

##### ISSUE TYPE

 - Bug Report

##### COMPONENT NAME
 - timezone

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
```
  - name: Set timezone to CEST.
    timezone:
        name: Europe/Berlin
```

##### OS / ENVIRONMENT

Running on Debian Testing, managing Debian Testing container running under LXC on Turris Omnia, OpenWRT kernel.

##### SUMMARY

Changing the timezone is definitely possible without timedatectl. It could also be that Debian falsely disables systemd-timedated inside the container when there is virtualization, of that I am not entirely sure.


##### STEPS TO REPRODUCE

Use example above on LXC Debian Stretch container.


##### EXPECTED RESULTS

Expected to change /etc/timezone


##### ACTUAL RESULTS

Crashed ansible playbook.


_Copied from original issue: ansible/ansible-modules-extras#3641_


Discussion:
_From @ansibot on December 4, 2016 14:21_

<!-- @kayhayen: Thanks for filing this bug report! -->
@tmshn ping, this issue is waiting for your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: issue_bug_new --->
_From @ansibot on December 7, 2016 15:50_

This repository has been locked. All new issues and pull requests should be filed in https://github.com/ansible/ansible

Please read through the <a href="http://docs.ansible.com/ansible/dev_guide/repomerge.html#move-issues-and-prs-to-new-repo">repomerge</a> page in the dev guide. The guide contains links to tools which automatically move your issue or pull request to the ansible/ansible repo.

<!-- boilerplate: repomerge -->
@kayhayen So the situation is: with LXC container,
- Debian virtual machine **has** `timedatectl` command
- but the command **doesn't** work,

right?

Then, could you tell me
1. What will happen if you simply run `timedatectl` in the container (stdout, stderr and exit code)? Display "Failed to create bus connection: No such file or directory" and exit with 2?
1. Does the module works with the following patch (just force to use `NosystemdTimezone` rather than `SystemdTimezone`)?
```patch
diff --git a/lib/ansible/modules/system/timezone.py b/lib/ansible/modules/system/timezone.py
index 7d8d9ae..057becb 100644
--- a/lib/ansible/modules/system/timezone.py
+++ b/lib/ansible/modules/system/timezone.py
@@ -98,10 +98,11 @@ class Timezone(object):
             module: The AnsibleModule.
         """
         if get_platform() == 'Linux':
-            if module.get_bin_path('timedatectl') is not None:
-                return super(Timezone, SystemdTimezone).__new__(SystemdTimezone)
-            else:
-                return super(Timezone, NosystemdTimezone).__new__(NosystemdTimezone)
+            # if module.get_bin_path('timedatectl') is not None:
+            #     return super(Timezone, SystemdTimezone).__new__(SystemdTimezone)
+            # else:
+            #     return super(Timezone, NosystemdTimezone).__new__(NosystemdTimezone)
+            return super(Timezone, NosystemdTimezone).__new__(NosystemdTimezone)
         else:
             # Not supported yet
             return super(Timezone, Timezone).__new__(Timezone)
```
Hello,

yes indeed this is the error and yes, your patch works. I switched timezone with dpkg-reconfigure tzdata, and with the patch, this works.

Yours,
Kay

Resolving patch:
diff --git a/lib/ansible/modules/system/timezone.py b/lib/ansible/modules/system/timezone.py
--- a/lib/ansible/modules/system/timezone.py
+++ b/lib/ansible/modules/system/timezone.py
@@ -92,13 +92,14 @@ def __new__(cls, module):
         """Return the platform-specific subclass.
 
         It does not use load_platform_subclass() because it need to judge based
-        on whether the `timedatectl` command exists.
+        on whether the `timedatectl` command exists and available.
 
         Args:
             module: The AnsibleModule.
         """
         if get_platform() == 'Linux':
-            if module.get_bin_path('timedatectl') is not None:
+            timedatectl = module.get_bin_path('timedatectl')
+            if timedatectl is not None and module.run_command(timedatectl)[0] == 0:
                 return super(Timezone, SystemdTimezone).__new__(SystemdTimezone)
             else:
                 return super(Timezone, NosystemdTimezone).__new__(NosystemdTimezone)


Test patch: