Skip to content

Commit 110fb43

Browse files
authored
Supporting private keys in user_ansible. (#842)
* Supporting private keys in user_ansible. * Improved defaults and docs. * User sub roles need all variables. * Hiding private key and allowing hashing of known_hosts.
1 parent 03ccb0a commit 110fb43

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

roles/user_ansible/defaults/main.yml

+7
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ user_ansible:
1414
groups: ""
1515
# List of SSH pub keys to authorize. These must be provided as strings (content of the pub key).
1616
ssh_keys: []
17+
# List of SSH private keys to add to server. These must be provided as strings (content of the private key).
18+
# Be sure to store securely using SOPS or similar.
19+
ssh_private_keys: []
20+
# List of hostnames to add to known_hosts.
21+
known_hosts: []
22+
# Whether or not to hash any provided hosts for known_hosts.
23+
known_hosts_hash: true

roles/user_ansible/tasks/main.yml

+33
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,36 @@
4444
with_items: "{{ user_ansible.ssh_keys }}"
4545
loop_control:
4646
loop_var: key
47+
48+
- name: Copy SSH private key to target.
49+
ansible.builtin.copy:
50+
dest: "/home/{{ user_ansible.username }}/.ssh/{% if key_id == 0 %}id_rsa{% else %}id_rsa_{{ key_id }}{% endif %}"
51+
content: "{{ key }}"
52+
owner: "{{ user_ansible.username }}"
53+
group: "{{ user_ansible.username }}"
54+
mode: '0600'
55+
with_items: "{{ user_ansible.ssh_private_keys }}"
56+
no_log: true
57+
loop_control:
58+
loop_var: key
59+
index_var: key_id
60+
61+
- name: Ensure known_hosts file exists.
62+
ansible.builtin.file:
63+
path: "/home/{{ user_ansible.username }}/.ssh/known_hosts"
64+
state: touch
65+
owner: "{{ user_ansible.username }}"
66+
group: "{{ user_ansible.username }}"
67+
mode: '0600'
68+
when: user_ansible.known_hosts | length
69+
70+
- name: Add public keys to known_hosts.
71+
ansible.builtin.known_hosts:
72+
hash_host: "{{ user_ansible.known_hosts_hash }}"
73+
path: "/home/{{ user_ansible.username }}/.ssh/known_hosts"
74+
name: "{{ host }}"
75+
key: "{{ lookup('pipe', 'ssh-keyscan -t rsa ' + host) }}"
76+
state: present
77+
with_items: "{{ user_ansible.known_hosts }}"
78+
loop_control:
79+
loop_var: host

roles/user_deploy/defaults/main.yml

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ user_deploy:
1414
# List of additional groups to add the user to.
1515
groups: []
1616
ssh_keys: []
17+
# If you add more than one key here, make sure the original private key of your deploy server is the first.
18+
ssh_private_keys: []
19+
known_hosts: []
20+
known_hosts_hash: true

roles/user_provision/defaults/main.yml

+4
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ user_provision:
1212
sudoer: true
1313
groups: []
1414
ssh_keys: []
15+
# If you add more than one key here, make sure the original private key of your infra controller server is the first.
16+
ssh_private_keys: []
17+
known_hosts: []
18+
known_hosts_hash: true

0 commit comments

Comments
 (0)