Initial commit
[ta/infra-ansible.git] / roles / bootstrap-host / tasks / prepare_ssh_keys.yml
1 ---
2 # Copyright 2015, Rackspace US, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 - name: Ensure root has a .ssh directory
17   file:
18     path: /root/.ssh
19     state: directory
20     owner: root
21     group: root
22     mode: 0700
23   tags:
24     - ssh-key-dir
25
26 - name: Check for existing ssh private key file
27   stat:
28     path: /root/.ssh/id_rsa
29   register: ssh_key_private
30   tags:
31     - ssh-key-check
32
33 - name: Check for existing ssh public key file
34   stat:
35     path: /root/.ssh/id_rsa.pub
36   register: ssh_key_public
37   tags:
38     - ssh-key-check
39
40 - name: Remove an existing private/public ssh keys if one is missing
41   file:
42     path: "/root/.ssh/{{ item }}"
43     state: absent
44   when: not ssh_key_public.stat.exists or not ssh_key_private.stat.exists
45   with_items:
46     - 'id_rsa'
47     - 'id_rsa.pub'
48   tags:
49     - ssh-key-clean
50
51 - name: Create ssh key pair for root
52   user:
53     name: root
54     generate_ssh_key: yes
55     ssh_key_bits: 2048
56     ssh_key_file: /root/.ssh/id_rsa
57   tags:
58     - ssh-key-generate
59
60 - name: Fetch the generated public ssh key
61   fetch:
62     src: "/root/.ssh/id_rsa.pub"
63     dest: "/tmp/id_rsa.pub"
64     flat: yes
65   when: inventory_hostname == groups['all'][0]
66   tags:
67     - ssh-key-authorized
68
69 - name: Ensure root's new public ssh key is in authorized_keys
70   authorized_key:
71     user: root
72     key: "{{ lookup('file','/tmp/id_rsa.pub') }}"
73     manage_dir: no
74   tags:
75     - ssh-key-authorized