Skip to content

Commit

Permalink
Support Python < 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
craigw committed Oct 17, 2016
1 parent a8f47f2 commit 35f065b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
27 changes: 16 additions & 11 deletions library/chage.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import sys
import glob
import shlex
import subprocess
import os
import json

args_file = sys.argv[1]
Expand All @@ -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:
Expand All @@ -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({
Expand All @@ -101,7 +106,7 @@
})
sys.exit(1)

changed = before != subprocess.check_output(chage_check)
changed = before != chage_check(user)

print json.dumps({
"changed": changed,
Expand Down

0 comments on commit 35f065b

Please sign in to comment.