2012-11-27 17:18:46 +01:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-07-28 23:02:16 +02:00
|
|
|
"""
|
|
|
|
Ansible module to add authorized_keys for ssh logins.
|
2012-05-30 22:41:38 +02:00
|
|
|
(c) 2012, Brad Olson <brado@movedbylight.com>
|
|
|
|
|
|
|
|
This file is part of Ansible
|
|
|
|
|
|
|
|
Ansible is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Ansible is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2012-09-28 21:55:49 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: authorized_key
|
2012-10-01 09:18:54 +02:00
|
|
|
short_description: Adds or removes an SSH authorized key
|
2012-09-28 21:55:49 +02:00
|
|
|
description:
|
2012-10-01 09:18:54 +02:00
|
|
|
- Adds or removes an SSH authorized key for a user from a remote host.
|
2012-09-28 21:55:49 +02:00
|
|
|
version_added: "0.5"
|
|
|
|
options:
|
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- Name of the user who should have access to the remote host
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
key:
|
|
|
|
description:
|
|
|
|
- the SSH public key, as a string
|
|
|
|
required: true
|
|
|
|
default: null
|
2013-03-26 16:12:09 +01:00
|
|
|
path:
|
|
|
|
description:
|
|
|
|
- Alternate path to the authorized_keys file
|
|
|
|
required: false
|
2013-04-19 04:43:14 +02:00
|
|
|
default: "(homedir)+/.ssh/authorized_keys"
|
2013-04-05 20:51:08 +02:00
|
|
|
version_added: "1.2"
|
2013-03-26 16:12:09 +01:00
|
|
|
manage_dir:
|
|
|
|
description:
|
2013-04-19 04:16:28 +02:00
|
|
|
- Whether this module should manage the directory of the authorized_keys file
|
2013-03-26 16:12:09 +01:00
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "yes"
|
2013-04-05 20:51:08 +02:00
|
|
|
version_added: "1.2"
|
2012-09-28 21:55:49 +02:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- whether the given key should or should not be in the file
|
|
|
|
required: false
|
|
|
|
choices: [ "present", "absent" ]
|
|
|
|
default: "present"
|
2013-04-19 04:43:14 +02:00
|
|
|
description:
|
|
|
|
- "adds or removes authorized keys for particular user accounts"
|
2012-09-28 21:55:49 +02:00
|
|
|
author: Brad Olson
|
|
|
|
'''
|
|
|
|
|
2013-04-05 21:33:21 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Example using key data from a local file on the management machine
|
2013-04-19 04:36:06 +02:00
|
|
|
authorized_key: user=charlie key="{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"
|
2013-04-05 21:33:21 +02:00
|
|
|
|
|
|
|
# Using alternate directory locations:
|
2013-04-19 04:36:06 +02:00
|
|
|
authorized_key: user=charlie key="{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}" sshdir='/etc/ssh/authorized_keys/charlie' manage_dir=no
|
2013-04-05 21:33:21 +02:00
|
|
|
'''
|
|
|
|
|
2012-07-28 23:02:16 +02:00
|
|
|
# Makes sure the public key line is present or absent in the user's .ssh/authorized_keys.
|
|
|
|
#
|
|
|
|
# Arguments
|
|
|
|
# =========
|
|
|
|
# user = username
|
|
|
|
# key = line to add to authorized_keys for user
|
2013-03-26 16:12:09 +01:00
|
|
|
# path = path to the user's authorized_keys file (default: ~/.ssh/authorized_keys)
|
|
|
|
# manage_dir = whether to create, and control ownership of the directory (default: true)
|
2012-07-28 23:02:16 +02:00
|
|
|
# state = absent|present (default: present)
|
|
|
|
#
|
|
|
|
# see example in examples/playbooks
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import pwd
|
|
|
|
import os.path
|
2012-10-20 07:54:08 +02:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2012-05-30 22:41:38 +02:00
|
|
|
|
2013-03-26 16:12:09 +01:00
|
|
|
def keyfile(module, user, write=False, path=None, manage_dir=True):
|
2012-07-28 23:02:16 +02:00
|
|
|
"""
|
2012-08-07 02:07:02 +02:00
|
|
|
Calculate name of authorized keys file, optionally creating the
|
2012-05-30 22:41:38 +02:00
|
|
|
directories and file, properly setting permissions.
|
|
|
|
|
|
|
|
:param str user: name of user in passwd file
|
2012-07-02 21:16:57 +02:00
|
|
|
:param bool write: if True, write changes to authorized_keys file (creating directories if needed)
|
2013-03-26 16:12:09 +01:00
|
|
|
:param str path: if not None, use provided path rather than default of '~user/.ssh/authorized_keys'
|
|
|
|
:param bool manage_dir: if True, create and set ownership of the parent dir of the authorized_keys file
|
2012-05-30 22:41:38 +02:00
|
|
|
:return: full path string to authorized_keys for user
|
|
|
|
"""
|
|
|
|
|
2012-11-08 01:43:09 +01:00
|
|
|
try:
|
|
|
|
user_entry = pwd.getpwnam(user)
|
|
|
|
except KeyError, e:
|
|
|
|
module.fail_json(msg="Failed to lookup user %s: %s" % (user, str(e)))
|
2013-03-26 16:12:09 +01:00
|
|
|
if path is None:
|
|
|
|
homedir = user_entry.pw_dir
|
|
|
|
sshdir = os.path.join(homedir, ".ssh")
|
|
|
|
keysfile = os.path.join(sshdir, "authorized_keys")
|
|
|
|
else:
|
|
|
|
sshdir = os.path.dirname(path)
|
|
|
|
keysfile = path
|
2012-07-28 23:02:16 +02:00
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
if not write:
|
2012-07-28 23:02:16 +02:00
|
|
|
return keysfile
|
|
|
|
|
2012-05-30 22:41:38 +02:00
|
|
|
uid = user_entry.pw_uid
|
|
|
|
gid = user_entry.pw_gid
|
2012-07-28 23:02:16 +02:00
|
|
|
|
2013-03-26 16:12:09 +01:00
|
|
|
if manage_dir in BOOLEANS_TRUE:
|
|
|
|
if not os.path.exists(sshdir):
|
|
|
|
os.mkdir(sshdir, 0700)
|
|
|
|
if module.selinux_enabled():
|
|
|
|
module.set_default_selinux_context(sshdir, False)
|
|
|
|
os.chown(sshdir, uid, gid)
|
|
|
|
os.chmod(sshdir, 0700)
|
2012-07-28 23:02:16 +02:00
|
|
|
|
2013-04-12 20:46:41 +02:00
|
|
|
if not os.path.exists(keysfile):
|
|
|
|
basedir = os.path.dirname(keysfile)
|
|
|
|
if not os.path.exists(basedir):
|
|
|
|
os.makedirs(basedir)
|
2012-07-02 19:57:38 +02:00
|
|
|
try:
|
2012-07-02 21:16:57 +02:00
|
|
|
f = open(keysfile, "w") #touches file so we can set ownership and perms
|
2012-07-02 19:57:38 +02:00
|
|
|
finally:
|
|
|
|
f.close()
|
2012-12-14 01:29:40 +01:00
|
|
|
if module.selinux_enabled():
|
|
|
|
module.set_default_selinux_context(keysfile, False)
|
2012-07-28 23:02:16 +02:00
|
|
|
|
2012-05-30 22:41:38 +02:00
|
|
|
os.chown(keysfile, uid, gid)
|
|
|
|
os.chmod(keysfile, 0600)
|
|
|
|
return keysfile
|
|
|
|
|
2012-07-28 23:02:16 +02:00
|
|
|
def readkeys(filename):
|
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
if not os.path.isfile(filename):
|
2012-07-28 23:02:16 +02:00
|
|
|
return []
|
|
|
|
f = open(filename)
|
|
|
|
keys = [line.rstrip() for line in f.readlines()]
|
|
|
|
f.close()
|
2012-05-30 22:41:38 +02:00
|
|
|
return keys
|
|
|
|
|
2012-10-20 07:54:08 +02:00
|
|
|
def writekeys(module, filename, keys):
|
2012-07-28 23:02:16 +02:00
|
|
|
|
2012-10-25 05:50:11 +02:00
|
|
|
fd, tmp_path = tempfile.mkstemp('', 'tmp', os.path.dirname(filename))
|
2012-10-20 07:54:08 +02:00
|
|
|
f = open(tmp_path,"w")
|
|
|
|
try:
|
|
|
|
f.writelines( (key + "\n" for key in keys) )
|
|
|
|
except IOError, e:
|
|
|
|
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
|
2012-07-28 23:02:16 +02:00
|
|
|
f.close()
|
2013-04-24 03:52:23 +02:00
|
|
|
module.atomic_move(tmp_path, filename)
|
2012-07-28 23:02:16 +02:00
|
|
|
|
|
|
|
def enforce_state(module, params):
|
|
|
|
"""
|
|
|
|
Add or remove key.
|
2012-05-30 22:41:38 +02:00
|
|
|
"""
|
|
|
|
|
2013-03-26 16:12:09 +01:00
|
|
|
user = params["user"]
|
|
|
|
key = params["key"]
|
|
|
|
path = params.get("path", None)
|
|
|
|
manage_dir = params.get("manage_dir", True)
|
|
|
|
state = params.get("state", "present")
|
2012-05-30 22:41:38 +02:00
|
|
|
|
2013-02-03 01:17:18 +01:00
|
|
|
key = key.split('\n')
|
2013-01-13 03:38:53 +01:00
|
|
|
|
2012-07-28 23:02:16 +02:00
|
|
|
# check current state -- just get the filename, don't create file
|
2013-03-26 16:12:09 +01:00
|
|
|
write = False
|
|
|
|
params["keyfile"] = keyfile(module, user, write, path, manage_dir)
|
2012-07-28 23:02:16 +02:00
|
|
|
keys = readkeys(params["keyfile"])
|
2013-02-03 01:07:10 +01:00
|
|
|
|
|
|
|
# Check our new keys, if any of them exist we'll continue.
|
2013-02-03 01:17:18 +01:00
|
|
|
for new_key in key:
|
2013-02-03 01:07:10 +01:00
|
|
|
present = new_key in keys
|
|
|
|
# handle idempotent state=present
|
|
|
|
if state=="present":
|
|
|
|
if present:
|
|
|
|
continue
|
|
|
|
keys.append(new_key)
|
2013-03-26 16:12:09 +01:00
|
|
|
write = True
|
|
|
|
writekeys(module, keyfile(module, user, write, path, manage_dir), keys)
|
2013-02-03 01:07:10 +01:00
|
|
|
params['changed'] = True
|
|
|
|
|
|
|
|
elif state=="absent":
|
|
|
|
if not present:
|
|
|
|
continue
|
|
|
|
keys.remove(new_key)
|
2013-03-26 16:12:09 +01:00
|
|
|
write = True
|
|
|
|
writekeys(module, keyfile(module, user, write, path, manage_dir), keys)
|
2013-02-03 01:07:10 +01:00
|
|
|
params['changed'] = True
|
|
|
|
|
2012-07-28 23:02:16 +02:00
|
|
|
return params
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2013-03-26 16:12:09 +01:00
|
|
|
user = dict(required=True, type='str'),
|
|
|
|
key = dict(required=True, type='str'),
|
|
|
|
path = dict(required=False, type='str'),
|
2013-04-05 20:51:08 +02:00
|
|
|
manage_dir = dict(required=False, type='bool'),
|
2013-03-26 16:12:09 +01:00
|
|
|
state = dict(default='present', choices=['absent','present'])
|
2012-07-28 23:02:16 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
results = enforce_state(module, module.params)
|
|
|
|
module.exit_json(**results)
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
2012-08-07 02:07:02 +02:00
|
|
|
main()
|