Initial commit
[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 root'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   tags:
47     - ssh-key-authorized
48
49 - name: Ensure there is a private key /etc/userconfig/id_rsa in virtual env. Provide read permissions to all users
50   file:
51     path: "/etc/userconfig/id_rsa"
52     mode: 0644
53   when: facter_virtual == "kvm"
54
55 - name: Ensure root has a .ssh directory
56   file:
57     path: /root/.ssh
58     state: directory
59     owner: root
60     group: root
61     mode: 0700
62   when: facter_virtual == "kvm"
63
64 - name: Copy /etc/userconfig/id_rsa /root/.ssh/id_rsa
65   copy:
66     src: /etc/userconfig/id_rsa
67     dest: /root/.ssh/id_rsa
68     owner: root
69     group: root
70     mode: 0400
71   when: facter_virtual == "kvm"
72
73 - name: Default http config listens on port 80, comment it.
74   lineinfile:
75     path: "/etc/httpd/conf/httpd.conf"
76     line: "Listen 80"
77     state: "absent"