Debian Codegraph @

ansible_collections/amazon/aws/tests/integration/targets/setup_sshkey/tasks/main.yml

raw
1 ---
2 # (c) 2014, James Laska <[email protected]>
3
4 # This file is part of Ansible
5 #
6 # Ansible is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Ansible is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
18
19 - name: Create a temp dir
20 ansible.builtin.tempfile:
21 state: directory
22 register: sshkey_dir
23 tags:
24 - prepare
25
26 - name: Ensure script is available
27 ansible.builtin.copy:
28 src: ec2-fingerprint.py
29 dest: "{{ sshkey_dir.path }}/ec2-fingerprint.py"
30 mode: "0700"
31 tags:
32 - prepare
33
34 - name: Set location of SSH keys
35 ansible.builtin.set_fact:
36 sshkey: "{{ sshkey_dir.path }}/key_one"
37 another_sshkey: "{{ sshkey_dir.path }}/key_two"
38 sshkey_pub: "{{ sshkey_dir.path }}/key_one.pub"
39 another_sshkey_pub: "{{ sshkey_dir.path }}/key_two.pub"
40
41 # Because some older OSes don't like the Elliptic Curve keys we'll stick to 4096 bit RSA for now
42 - name: Generate sshkey (4096 bit RSA)
43 ansible.builtin.command: ssh-keygen -t rsa -b 4096 -P '' -f '{{ sshkey }}'
44 args:
45 creates: '{{ sshkey }}'
46 tags:
47 - prepare
48
49 # AWS uses a custom MD5 checksum for its RSA keys (rather than the modern default of a base64 encoded sha256 hash)
50 - name: Record fingerprint
51 ansible.builtin.command: "{{ sshkey_dir.path }}/ec2-fingerprint.py {{ sshkey_pub }}"
52 changed_when: false
53 register: fingerprint
54 tags:
55 - prepare
56
57 # With OpenSSH 9.5 the default changed to ED25519, to avoid confusing issues when running locally vs
58 # in CI, we set the default
59 - name: Generate another_sshkey (ED25519)
60 ansible.builtin.command: ssh-keygen -t ed25519 -P '' -f {{ another_sshkey }}
61 args:
62 creates: '{{ another_sshkey }}'
63 tags:
64 - prepare
65
66 # When adding support for ED25519 Amazon stuck to the newer default of base64 encoded sha256 hashes, this means we can
67 # ask ssh-keygen for the fingerprint, but we'll explicitly ask for the sha256 version just in case the default changes
68 - name: Record another fingerprint
69 ansible.builtin.command: "ssh-keygen -l -E sha256 -f {{ another_sshkey_pub }}"
70 changed_when: false
71 register: another_fingerprint
72 tags:
73 - prepare
74
75 - name: Set facts for future roles
76 ansible.builtin.set_fact:
77 # Public SSH keys (OpenSSH format)
78 # - 4096 bit RSA (broad support)
79 key_material: "{{ lookup('file', sshkey_pub) }}"
80 # - ED25519 (modern, not supported by some older services/OSes)
81 another_key_material: "{{ lookup('file', another_sshkey_pub) }}"
82 # AWS Format fingerprints
83 # - RSA: MD5sum based
84 fingerprint: "{{ fingerprint.stdout }}"
85 # - ED25519: AWS pad the base64 output (technically correct), ssh-keygen truncates the output
86 another_fingerprint: "{{ another_fingerprint.stdout[11:54] }}="
87 tags:
88 - prepare