summaryrefslogtreecommitdiffstats
path: root/roles/lib_timedatectl/library/timedatectl.py
blob: b6eab59189769f33bacc29794f1e7503cfe44fc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
'''
    timedatectl ansible module

    This module supports setting ntp enabled
'''
import subprocess




def do_timedatectl(options=None):
    ''' subprocess timedatectl '''

    cmd = ['/usr/bin/timedatectl']
    if options:
        cmd += options.split()

    proc = subprocess.Popen(cmd, stdin=None, stdout=subprocess.PIPE)
    proc.wait()
    return proc.stdout.read()

def main():
    ''' Ansible module for timedatectl
    '''

    module = AnsibleModule(
        argument_spec=dict(
            #state=dict(default='enabled', type='str'),
            ntp=dict(default=True, type='bool'),
        ),
        #supports_check_mode=True
    )

    # do something
    ntp_enabled = False

    results = do_timedatectl()

    for line in results.split('\n'):
        if 'NTP enabled' in line:
            if 'yes' in line:
                ntp_enabled = True

    ########
    # Enable NTP
    ########
    if module.params['ntp']:
        if ntp_enabled:
            module.exit_json(changed=False, results="enabled", state="enabled")

        # Enable it
        # Commands to enable ntp
        else:
            results = do_timedatectl('set-ntp yes')
            module.exit_json(changed=True, results="enabled", state="enabled", cmdout=results)

    #########
    # Disable NTP
    #########
    else:
        if not ntp_enabled:
            module.exit_json(changed=False, results="disabled", state="disabled")

        results = do_timedatectl('set-ntp no')
        module.exit_json(changed=True, results="disabled", state="disabled")

    module.exit_json(failed=True, changed=False, results="Something went wrong", state="unknown")

# Pylint is getting in the way of basic Ansible
# pylint: disable=redefined-builtin,wildcard-import,unused-wildcard-import
from ansible.module_utils.basic import *

main()