Skip to content

Instantly share code, notes, and snippets.

@nicolas17
Last active September 6, 2016 02:29
Show Gist options
  • Save nicolas17/29b8904c5f6d25896c596548857f487e to your computer and use it in GitHub Desktop.
Save nicolas17/29b8904c5f6d25896c596548857f487e to your computer and use it in GitHub Desktop.
Ansible ssh keys
---
- hosts: default
remote_user: root
vars:
users:
- foo
- bar
- baz
- quux
keys: {}
tasks:
- name: gather SSH keys
set_fact:
keys: "{{ {item: lookup('fileglob','keydir/'+item+'@key*.pub', wantlist=True) | sort} | combine(keys) }}"
with_items: "{{users}}"
- name: check that SSH keys exist
assert:
that: "{{keys[item] | length != 0}}"
msg: "user '{{item}}' doesn't exist or has no keys"
with_items: "{{users}}"
- name: setup SSH keys for root
template: src=ssh_keys.j2 dest=/root/.ssh/authorized_keys
{% for user in users: %}
# {{user}}
{% for keyfile in keys[user] %}
{{ lookup('file', keyfile) }}
{% endfor %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment