Initial commit
[ta/infra-ansible.git] / roles / manage_linux_user / tasks / main.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 or delete linux user"
17   user:
18     name: "{{ item.name }}"
19     password: "{{ item.password | default('') }}"
20     remove: "{{ item.remove | default('yes') }}"
21     force: "{{ item.remove | default('yes') }}"
22     state: "{{ item.state | default('absent') }}"
23   with_items: "{{ linuxuser | default([]) }}"
24
25 - name: "remove the old public ssh key"
26   file:
27     path: /home/{{ item.name }}/.ssh/{{ item.name }}
28     state: absent
29   with_items: "{{ linuxuser | default([]) }}"
30
31 - name: "Create the user .ssh directory"
32   when: item.state == 'present'
33   file:
34     path: /home/{{ item.name }}/.ssh
35     state: directory
36     owner: "{{ item.name }}"
37     group: "{{ item.name }}"
38     mode: 0700
39   with_items: "{{ linuxuser | default([]) }}"
40
41 - name: "add a new public ssh key"
42   when: item.state == 'present' and item.public_key != ""
43   lineinfile:
44     path: /home/{{ item.name }}/.ssh/{{ item.name }}
45     create: yes
46     regexp: '.*'
47     state: "{{ item.state | default('absent') }}"
48     line: "{{ item.public_key | default('') }}"
49     group: "{{ item.name }}"
50     owner: "{{ item.name }}"
51     mode: 0400
52   with_items: "{{ linuxuser | default([]) }}"
53
54 - name: Locking and Unlocking the user
55   when: item.state == 'present' and item.password != ""
56   command: sudo passwd "{{ item.name }}" "{{ item.lock_state }}"
57   with_items: "{{ linuxuser | default([]) }}"