From a1c9b13d81d8c94c7764524809f5419a5d3ff73d Mon Sep 17 00:00:00 2001 From: Matt Woodson Date: Mon, 18 Jan 2016 15:24:28 -0500 Subject: added the lib_timedate role --- roles/chrony/README.md | 4 +- roles/chrony/meta/main.yml | 2 +- roles/chrony/templates/chrony.conf.j2 | 2 +- roles/lib_timedatectl/library/timedatectl.py | 74 ++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 roles/lib_timedatectl/library/timedatectl.py diff --git a/roles/chrony/README.md b/roles/chrony/README.md index 688bec86a..bf15d9669 100644 --- a/roles/chrony/README.md +++ b/roles/chrony/README.md @@ -10,12 +10,12 @@ Requirements Role Variables -------------- -opschrony_ntp_servers: a list of ntp servers to use the chrony.conf file +chrony_ntp_servers: a list of ntp servers to use the chrony.conf file Dependencies ------------ -ops_roles/lib_timedatectl +roles/lib_timedatectl Example Playbook ---------------- diff --git a/roles/chrony/meta/main.yml b/roles/chrony/meta/main.yml index 2c61018ad..85595d7c3 100644 --- a/roles/chrony/meta/main.yml +++ b/roles/chrony/meta/main.yml @@ -15,4 +15,4 @@ galaxy_info: categories: - system dependencies: -- ops_roles/lib_timedatectl +- roles/lib_timedatectl diff --git a/roles/chrony/templates/chrony.conf.j2 b/roles/chrony/templates/chrony.conf.j2 index ab50ce1b1..de43b6364 100644 --- a/roles/chrony/templates/chrony.conf.j2 +++ b/roles/chrony/templates/chrony.conf.j2 @@ -1,6 +1,6 @@ # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). -{% for server in opschrony_ntp_servers %} +{% for server in chrony_ntp_servers %} server {{ server }} iburst {% endfor %} diff --git a/roles/lib_timedatectl/library/timedatectl.py b/roles/lib_timedatectl/library/timedatectl.py new file mode 100644 index 000000000..b6eab5918 --- /dev/null +++ b/roles/lib_timedatectl/library/timedatectl.py @@ -0,0 +1,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() -- cgit v1.2.3