From 35f065b2488962bae4de16f8f5fb4aac80cdd898 Mon Sep 17 00:00:00 2001 From: Craig R Webster Date: Mon, 17 Oct 2016 11:34:00 +0100 Subject: [PATCH] Support Python < 2.7 --- README.md | 30 ++++++++++++++++++++---------- library/chage.py | 27 ++++++++++++++++----------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 2f1c228..0f355a6 100644 --- a/README.md +++ b/README.md @@ -7,21 +7,31 @@ this role. Requirements ------------ -The `chage` command. +The `chage` command needs to be installed and configured on the hosts being +targetted by your playbook. Example Playbook ---------------- + # playbook.yml - hosts: servers - tasks: - - name: Set password expiry - chage: - user: "{{ item }}" - password_maximum_days: 90 - password_warn_days: 76 - with_items: - - craig - - charlotte + roles: + your-role + + # roles/your-role/tasks/main.yml + tasks: + - name: Set password expiry + chage: + user: "{{ item }}" + password_maximum_days: 90 + password_warn_days: 76 + with_items: + - craig + - charlotte + + # roles/your-role/meta/main.yml + dependencies: + - barkingiguana.chage License ------- diff --git a/library/chage.py b/library/chage.py index 48be71c..7276947 100755 --- a/library/chage.py +++ b/library/chage.py @@ -45,7 +45,7 @@ import sys import glob import shlex -import subprocess +import os import json args_file = sys.argv[1] @@ -68,12 +68,14 @@ # ignore any arguments without an equals in it if "=" in arg: (key, value) = arg.split("=") - if value != 'False': - if key == 'user': - user = value - else: - keyname = argnames.get(key, False) - if keyname != False: + if key == 'user': + user = value + else: + keyname = argnames.get(key, False) + if keyname != False: + if value == 'False': + clargs[keyname] = '-1' + else: clargs[keyname] = value if user == False: @@ -83,15 +85,18 @@ }) sys.exit(1) -chage_check = ["chage", "-l", user] -before = subprocess.check_output(chage_check) +def chage_check(user): + os.system('chage -l "%s" > tmp' % user) + return open('tmp', 'r').read() + +before = chage_check(user) arguments = list() for key, val in clargs.items(): arguments.append("--%s %s" % (key, val)) command = executable + ' ' + ' '.join(arguments) + ' ' + user -rc = subprocess.call(command, shell=True) +rc = os.system(command) if rc != 0: print json.dumps({ @@ -101,7 +106,7 @@ }) sys.exit(1) -changed = before != subprocess.check_output(chage_check) +changed = before != chage_check(user) print json.dumps({ "changed": changed,