From cd55d3b6ab44d34d39f2830654e8a9f47cd9c301 Mon Sep 17 00:00:00 2001 From: Piffey Parlance Date: Sat, 2 Feb 2013 18:07:10 -0600 Subject: [PATCH 1/2] * Added support for multi-line key files. --- authorized_key | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/authorized_key b/authorized_key index f65d8bfca65..51515444fa4 100644 --- a/authorized_key +++ b/authorized_key @@ -143,28 +143,32 @@ def enforce_state(module, params): key = params["key"] state = params.get("state", "present") + # If multiple keys split on the newline if '\n' in key: - module.fail_json(msg="key= can only contain a single key") + new_keys = key.split('\n') # check current state -- just get the filename, don't create file params["keyfile"] = keyfile(module, user, write=False) keys = readkeys(params["keyfile"]) - present = key in keys - - # handle idempotent state=present - if state=="present": - if present: - module.exit_json(changed=False) - keys.append(key) - writekeys(module, keyfile(module, user,write=True), keys) - - elif state=="absent": - if not present: - module.exit_json(changed=False) - keys.remove(key) - writekeys(module, keyfile(module, user,write=True), keys) - - params['changed'] = True + + # Check our new keys, if any of them exist we'll continue. + for new_key in new_keys: + present = new_key in keys + # handle idempotent state=present + if state=="present": + if present: + continue + keys.append(new_key) + writekeys(module, keyfile(module, user,write=True), keys) + params['changed'] = True + + elif state=="absent": + if not present: + continue + keys.remove(new_key) + writekeys(module, keyfile(module, user,write=True), keys) + params['changed'] = True + return params def main(): From 0f09e8ba91f5f38a43c41608098c0bea1f7f2362 Mon Sep 17 00:00:00 2001 From: Piffey Parlance Date: Sat, 2 Feb 2013 18:17:18 -0600 Subject: [PATCH 2/2] * Minor removal of some dumb I did. --- authorized_key | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/authorized_key b/authorized_key index 51515444fa4..f936e20c81d 100644 --- a/authorized_key +++ b/authorized_key @@ -143,16 +143,14 @@ def enforce_state(module, params): key = params["key"] state = params.get("state", "present") - # If multiple keys split on the newline - if '\n' in key: - new_keys = key.split('\n') + key = key.split('\n') # check current state -- just get the filename, don't create file params["keyfile"] = keyfile(module, user, write=False) keys = readkeys(params["keyfile"]) # Check our new keys, if any of them exist we'll continue. - for new_key in new_keys: + for new_key in key: present = new_key in keys # handle idempotent state=present if state=="present":