-
Notifications
You must be signed in to change notification settings - Fork 2
/
keypair.yaml
73 lines (58 loc) · 2.11 KB
/
keypair.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
# Create an SSH keypair
# Requires that local key files
- hosts: localhost
gather_facts: True
vars_files:
- settings.yaml
vars:
keypair_path: "{{ ansible_env.HOME }}/.ssh"
tasks:
# Set up the SSH keypair
# Either creates a new keypair in OpenStack and saves the keys locally
# or, if a local key already exists, check that it matches
- name: Test for local private key
stat:
path: "{{ keypair_path }}/id_rsa"
register: private_key_file
- name: Test for local public key
stat:
path: "{{ keypair_path }}/id_rsa.pub"
register: public_key_file
- name: Create shared ssh keypair
openstack.cloud.keypair:
name: "{{ os.keypair_name }}"
state: present
register: keypair
- name: Check that we can safely write the private key
assert:
that: not private_key_file.stat.exists
fail_msg: Created a new keypair, but local private key file already exists - fix or delete before proceeding
quiet: true
when: keypair.key.private_key
- name: Write private key
copy:
dest: "{{ keypair_path }}/id_rsa"
content: "{{ keypair.key.private_key | trim }}"
mode: 0600
when: keypair.key.private_key and not private_key_file.stat.exists
- name: Write public key
copy:
dest: "{{ keypair_path }}/id_rsa.pub"
content: "{{ keypair.key.public_key | trim }}"
mode: 0644
when: keypair.key.public_key and not public_key_file.stat.exists
- name: Test for local private key
stat:
path: "{{ keypair_path }}/id_rsa"
register: private_key_file
- name: Check that private key file exists
assert:
that: private_key_file.stat.exists
fail_msg: No local private key file - fix or delete before proceeding
quiet: true
- name: Check public key content
assert:
that: "{{ lookup('file', keypair_path + '/id_rsa.pub', rstrip=true) == (keypair.key.public_key | trim) }}"
fail_msg: Local public key file has incorrect content - fix or delete before proceeding
quiet: true