Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Feature add more options #16

Merged
merged 11 commits into from
Jun 10, 2024
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ ansible-galaxy install claranet.users
## :gear: Role variables

### Users

Users supported attributes are `name`, `append`, `shell`, `createhome`, `home`, `password`, `uid`, `group`, `groups`, `update_password`, `state`, `remove` and `comment`.
[More informations](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/user_module.html)

Variable | Default value | Description
---------|---------------|----------------------------------------------------------------------------
users | **{}** | Create groups, users and enable bashrc, ssh/config, vimrc and profile files
Expand Down
23 changes: 21 additions & 2 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
hosts: all
become: true
become_user: root
roles:
- role: claranet.users
tasks:
- name: Include role claranet.users
ansible.builtin.include_role:
name: claranet.users
vars:
users:
root:
Expand Down Expand Up @@ -38,3 +40,20 @@
group: adm
groups:
- daemon

- name: Create tbd user
ansible.builtin.include_role:
name: claranet.users
vars:
users:
tbd:
tags:
- molecule-idempotence-notest

- name: Remove tbd user
ansible.builtin.include_role:
name: claranet.users
vars:
users:
tbd:
state: absent
13 changes: 13 additions & 0 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,16 @@ def test_installed_packages(host):
else:
assert host.package("vim").is_installed
assert host.package("e2fsprogs").is_installed


def test_tbd_user(host):
user = host.user("tbd")
assert not user.exists


def test_tbd_home(host):
user_name = "tbd"
file_name = f"/home/{user_name}"
file = host.file(file_name)
assert file.exists
assert file.is_directory
2 changes: 2 additions & 0 deletions tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
ansible.builtin.package:
name: "{{ _users_packages }}"
state: present
tags:
- molecule-idempotence-notest
8 changes: 6 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
- ansible.builtin.include_tasks: install.yml

# create users and groups
- ansible.builtin.include_tasks: create_groups.yml
- ansible.builtin.include_tasks: manage_groups.yml
loop: "{{ users | dict2items }}"
loop_control:
label: "{{ item.key }}"
- ansible.builtin.include_tasks: create_users.yml
- ansible.builtin.include_tasks: manage_users.yml
loop: "{{ users | dict2items }}"
loop_control:
label: "{{ item.key }}"
Expand All @@ -43,15 +43,19 @@
loop: "{{ users | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.value.state|default("present") != "absent"
- ansible.builtin.include_tasks: configure_ssh.yml
loop: "{{ users | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.value.state|default("present") != "absent"
- ansible.builtin.include_tasks: configure_vim.yml
loop: "{{ users | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.value.state|default("present") != "absent"
- ansible.builtin.include_tasks: configure_profile.yml
loop: "{{ users | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.value.state|default("present") != "absent"
3 changes: 2 additions & 1 deletion tasks/create_groups.yml → tasks/manage_groups.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
- name: "create_groups | create group {{ item.value.group | default('') }}"
- name: "manage_groups | create group {{ item.value.group | default('') }}"
ansible.builtin.group:
name: "{{ item.value.group }}"
gid: "{{ item.value.gid | default(omit) }}"
state: "{{ item.value.state | default('present') }}"
when: item.value.group is defined
7 changes: 5 additions & 2 deletions tasks/create_users.yml → tasks/manage_users.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
- name: "create_users | include hardening.yml"
- name: "manage_users | include hardening.yml"
ansible.builtin.include_tasks: hardening.yml
vars:
_users_status: "unset"

- name: "create_users | create user {{ item.key }}"
- name: "manage_users | {{ 'create' if (item.value.state | default('present')) != 'absent' else 'remove' }} user {{ item.key }}"
ansible.builtin.user:
name: "{{ item.key }}"
append: "{{ item.value.append | default(true if item.value.groups | default([]) | length > 0 else omit) }}"
Expand All @@ -16,3 +16,6 @@
group: "{{ item.value.group | default(omit) }}"
groups: "{{ item.value.groups | default(omit) }}"
update_password: "{{ item.value.update_password | default(omit) }}"
state: "{{ item.value.state | default('present') }}"
remove: "{{ item.value.remove | default(false) }}"
comment: "{{ item.value.comment | default(omit) }}"
Loading