-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateSSHkey.yaml
54 lines (44 loc) · 1.46 KB
/
createSSHkey.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
---
- name: Playbook to create SSH key pair
hosts: localhost
gather_facts: false
vars_prompt:
- name: "sshKeyFolder"
prompt: "Define the SSH key pair storage folder:"
default: "~/.ssh"
private: no
- name: "hostName"
prompt: "Define the Host Name:"
default: "example.local"
private: no
- name: "hostIP"
prompt: "Define the Host IP:"
default: "0.0.0.0"
private: no
- name: "sshKeyPassword"
prompt: "Define the password for SSH Private Key:"
unsafe: true
default: ""
private: yes # Set private to yes to hide input (default is no)
vars:
sshCipher: "ed25519"
tasks:
- name: Check if SSH key already exists
stat:
path: "{{ sshKeyFolder }}/{{ hostIP }}.{{ hostName }}.key"
register: ssh_key_stat
- name: Fail if SSH key already exists
fail:
msg: "SSH key {{ sshKeyFolder }}/{{ hostIP }}.{{ hostName }}.key already exists!"
when: ssh_key_stat.stat.exists
- name: Generate SSH key pair
community.crypto.openssh_keypair:
path: "{{ sshKeyFolder }}/{{ hostIP }}.{{ hostName }}.key"
type: "{{ sshCipher }}"
passphrase: "{{ sshKeyPassword }}"
comment: "{{ hostIP }}.{{ hostName }}"
register: ssh_key
- name: Display generated SSH key information
debug:
msg: "Generated SSH key with fingerprint: {{ ssh_key.fingerprint }}"
when: ssh_key is defined