Support for authorized keys
[ta/infra-ansible.git] / roles / bootstrap-host / tasks / create_sudo_user.yml
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15
16 - name: Create Sudo user and ssh key pair for it.
17   user:
18     name: "{{ sudo_user }}"
19     password: "{{ sudo_user_password }}"
20     generate_ssh_key: yes
21     ssh_key_bits: 2048
22     ssh_key_file: .ssh/id_rsa
23   tags:
24     - ssh-key-generate
25
26 - name: Add to sudoer list
27   copy:
28     content: "{{ sudo_user }} ALL=(ALL) NOPASSWD:ALL"
29     dest: "/etc/sudoers.d/{{ sudo_user }}"
30     mode: 0440
31
32 - name: Fetch the generated public ssh key
33   fetch:
34     src: "/home/{{ sudo_user }}/.ssh/id_rsa.pub"
35     dest: "/tmp/id_rsa.pub"
36     flat: yes
37   when: inventory_hostname == groups['all'][0]
38   tags:
39     - ssh-key-authorized
40
41 - name: Ensure sudo user's new public ssh key is in authorized_keys
42   authorized_key:
43     user: "{{ sudo_user }}"
44     key: "{{ lookup('file','/tmp/id_rsa.pub') }}"
45     manage_dir: no
46     exclusive: yes
47   tags:
48     - ssh-key-authorized
49
50 - name: Populate authorized keys from config to sudo user
51   authorized_key:
52     user: "{{ sudo_user }}"
53     key: "{{ sudo_user_authorized_keys | join('\n') }}"
54     manage_dir: no
55   tags:
56     - configured-authorized-keys
57
58 - name: Ensure there is a private key /etc/userconfig/id_rsa in virtual env. Provide read permissions to all users
59   file:
60     path: "/etc/userconfig/id_rsa"
61     mode: 0644
62   when: facter_virtual == "kvm"
63
64 - name: Ensure root has a .ssh directory
65   file:
66     path: /root/.ssh
67     state: directory
68     owner: root
69     group: root
70     mode: 0700
71   when: facter_virtual == "kvm"
72
73 - name: Copy /etc/userconfig/id_rsa /root/.ssh/id_rsa
74   copy:
75     src: /etc/userconfig/id_rsa
76     dest: /root/.ssh/id_rsa
77     owner: root
78     group: root
79     mode: 0400
80   when: facter_virtual == "kvm"
81
82 - name: Default http config listens on port 80, comment it.
83   lineinfile:
84     path: "/etc/httpd/conf/httpd.conf"
85     line: "Listen 80"
86     state: "absent"