Skip to content

Commit 2dd4570

Browse files
committed
Added to git
0 parents commit 2dd4570

File tree

13 files changed

+229
-0
lines changed

13 files changed

+229
-0
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Role Name
2+
=========
3+
4+
Multi-user management with focus on a project to which everybody has access via sudo.
5+
6+
Example case:
7+
- There are multiple organizations managing their pages
8+
- We deploy a docker project that contains multiple websites
9+
- Each user can manage the docker project via `sudo ./make.sh ... something ...` instead of having access to global sudo
10+
11+
12+
Role Variables
13+
--------------
14+
15+
```yamlex
16+
technical_entrypoint: "/project/make.sh"
17+
enable_technical_entrypoint: true
18+
19+
technical_account: "tech.admin"
20+
technical_account_id: 1800
21+
technical_group: "technical"
22+
technical_group_id: 1161
23+
24+
users:
25+
accounts:
26+
- login: iwa.somebody
27+
section: "ZSP" # account description / organization name / etc.
28+
password: 'some-password-hash-generated-by-mkpasswd'
29+
global_sudo: no
30+
gid: 1161
31+
uid: 2050
32+
disabled: no
33+
```
34+
35+
Example Playbook
36+
----------------
37+
38+
```yamlex
39+
- hosts: servers
40+
roles:
41+
- { role: username.rolename, x: 42 }
42+
vars:
43+
# ...
44+
```
45+
46+
Adding a new user account
47+
-------------------------
48+
49+
1. Use the tool `./mkpasswd.sh` to generate a password
50+
2. Create an entry in the users.accounts variable (there are examples already)
51+
- Paste the password into password section of your new account with single quotes
52+
- Please do not enable `global_sudo` option unless you really have a reason for that
53+
- Please fill in `section` field with the organization name
54+
- Please use only a-z, numbers and dot characters for the user name, else it may not work
55+
3. Run deployment
56+
57+
Blocking access for the user account
58+
------------------------------------
59+
60+
1. Edit users.accounts variable
61+
2. For specified user account please set `disabled: yes`
62+
- NOTICE: Deleting whole user section from file will not have an effect, as the deployment will ignore that user and will not change it
63+
so the user account deletion is not possible, only blocking is possible
64+
3. Run deployment
65+
66+
License
67+
-------
68+
69+
MIT
70+
71+
Author Information
72+
------------------
73+
74+
Krzysztof Wesołowski, anarchosyndicalist, backend-devops programmer, grassroot advocate
75+
76+
Made especially for:
77+
https://iwa-ait.org
78+
https://zsp.net.pl

defaults/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
# defaults file for users

handlers/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
# handlers file for users

meta/main.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
galaxy_info:
2+
author: Krzysztof Wesołowski
3+
description: Sets the user accounts basing on the easy to understand yaml configuration
4+
company: IWA-AIT.org / ZSP (non-profit organization)
5+
license: MIT
6+
min_ansible_version: 1.2
7+
8+
galaxy_tags:
9+
- users
10+
- access
11+
- sudo
12+
- multiple users
13+
14+
dependencies: []

tasks/groups.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
- name: Create a group
2+
become: yes
3+
group:
4+
name: "{{ technical_group|default('technical') }}"
5+
state: present
6+
gid: "{{ technical_group_id|default('1161') }}"
7+
tags: user
8+
9+
- name: "Create a technical account"
10+
become: yes
11+
tags: user
12+
user:
13+
name: "{{ technical_account|default('tech.admin') }}"
14+
comment: "Technical account (deployment)"
15+
generate_ssh_key: yes
16+
ssh_key_bits: 2048
17+
ssh_key_file: .ssh/id_rsa
18+
shell: /bin/bash
19+
password: "{{ technical_password }}"
20+
update_password: on_create
21+
createhome: yes
22+
uid: "{{ technical_account_id|default('1800') }}"
23+
groups: ["{{ technical_group|default('technical') }}", 'sudo']
24+
25+
- name: Enable all members of technical group to execute given project file as root
26+
become: yes
27+
lineinfile:
28+
dest: /etc/sudoers
29+
line: "%{{ technical_group|default('technical') }} ALL=(root) NOPASSWD: {{ technical_entrypoint|default('/project/make.sh') }}"
30+
tags: user
31+
when: enable_technical_entrypoint == True

tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- include: preliminaries.yml
2+
- include: groups.yml
3+
- include: users.yml

tasks/preliminaries.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- name: Install required packages
2+
become: yes
3+
apt:
4+
name: "{{ item }}"
5+
state: present
6+
with_items:
7+
- sudo
8+
tags: user

tasks/user.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
- name: "Create user {{ user.login }}"
3+
become: yes
4+
tags: user
5+
user:
6+
name: "{{ user.login }}"
7+
comment: "{{ user.section }}"
8+
generate_ssh_key: yes
9+
ssh_key_bits: 2048
10+
ssh_key_file: .ssh/id_rsa
11+
shell: /bin/zsh
12+
password: "{{ user.password }}"
13+
update_password: on_create
14+
createhome: yes
15+
uid: "{{ user.uid }}"
16+
groups: "{{ technical_group|default('technical') }}"
17+
18+
- name: Disable account if it was disabled in users.yml
19+
become: yes
20+
shell: "chage -E 2000-05-01 {{ user.login }}"
21+
when: user.disabled == "yes" or user.disabled == "1" or user.disabled == True or user.disabled == "true" or user.disabled == 1
22+
23+
- name: Enable account if it was not disabled in users.yml
24+
become: yes
25+
shell: "chage -E 2200-05-01 {{ user.login }}"
26+
when: user.disabled == "no" or user.disabled == "0" or user.disabled == False or user.disabled == "false" or user.disabled == 0
27+
28+
- name: Allow user to use sudo globally (if allowed)
29+
become: yes
30+
user:
31+
name: '{{ user.login }}'
32+
groups: sudo
33+
append: yes
34+
when: user.global_sudo == "yes" or user.global_sudo == "1" or user.global_sudo == True or user.global_sudo == "true" or user.global_sudo == 1
35+
36+
- name: Disallow user to use sudo (if disallowed)
37+
become: yes
38+
shell: "gpasswd -d {{ user.login }} sudo || true"
39+
when: user.global_sudo == "no" or user.global_sudo == "0" or user.global_sudo == False or user.global_sudo == "false" or user.global_sudo == 0
40+
41+
- name: Check if the user has customized zshrc
42+
stat: path="/home/{{ user.login }}/.zshrc"
43+
register: zshrc_file
44+
45+
- name: Creating a zshrc from template (if the user does not have already modified it)
46+
become: yes
47+
become_user: "{{ user.login }}"
48+
template:
49+
src: zshrc
50+
dest: "/home/{{ user.login }}/.zshrc"
51+
when: zshrc_file.stat.exists == False
52+
53+
- name: Clone oh-my-zsh
54+
become: yes
55+
become_user: "{{ user.login }}"
56+
git:
57+
repo: https://github.com/robbyrussell/oh-my-zsh
58+
dest: "/home/{{ user.login }}/.oh-my-zsh"
59+

tasks/users.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
#
3+
# Iterate over all users and create/update each account one-by-one
4+
#
5+
6+
- include: user.yml user={{ item }}
7+
with_items: "{{ users.accounts }}"

templates/zshrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ZSH=$HOME/.oh-my-zsh
2+
ZSH_THEME="half-life"
3+
DISABLE_AUTO_UPDATE="true"
4+
plugins=(git symfony python vagrant ubuntu rsync composer systemd)
5+
source $ZSH/oh-my-zsh.sh
6+
export PATH=$HOME/bin:/usr/local/bin:$PATH

0 commit comments

Comments
 (0)