aboutsummaryrefslogtreecommitdiffstats
path: root/roles/users/tasks/main.yml
blob: db38667863909443220a24535d3ce7931f20a995 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
---
- import_tasks: root.yml
  tags:
  - users
  - root

- name: Create users
  user:
    name: '{{ item }}'
    state: present
    comment: '{{ userdata[item].name }}'
    shell: "{{ ('shell' in userdata[item])|ternary(userdata[item].shell,'/bin/bash') }}"
    uid: '{{ userdata[item].uid }}'
    password: "{{ ('password' in userdata[item])|ternary(userdata[item].password,omit) }}"
    group: users
    groups: "{{ (users[item] == 'sudo')|ternary([sudo_group],[]) }}"
  with_items: '{{ users|dictsort()|selectattr(1)|map(attribute=0)|list }}'
  tags:
  - users

- name: Create .ssh directories
  file:
    path: '~{{ item }}/.ssh'
    state: directory
    owner: '{{ item }}'
    group: users
    mode: 0700
  with_items: '{{ users|dictsort()|selectattr(1)|map(attribute=0)|list }}'
  when: "'authorized_keys' in userdata[item]"
  tags:
  - users

- name: Create authorized_keys
  template:
    dest: '~{{ item }}/.ssh/authorized_keys'
    src: authorized_keys.j2
    owner: '{{ item }}'
    group: users
    mode: 0600
  with_items: '{{ users|dictsort()|selectattr(1)|map(attribute=0)|list }}'
  when: "'authorized_keys' in userdata[item]"
  tags:
  - users

- name: Include user tasks
  include_tasks:
    file: '{{ user }}.yml'
    apply:
      tags:
      - users
  with_items: '{{ users|dictsort()|selectattr(1)|map(attribute=0)|list }}'
  loop_control:
    loop_var: user
  when: "'tasks' in userdata[user] and userdata[user].tasks"
  tags:
  - users

- name: Remove users
  user:
    name: '{{ item }}'
    state: absent
    remove: yes
  with_items: '{{ userdata|dictsort()|map(attribute=0)|difference(users|dictsort()|selectattr(1)|map(attribute=0))|list }}'
  tags:
  - users

# vim: set ts=2 sw=2 et: