feat(role): init
Some checks failed
checks-test / Lint (push) Failing after 34s
checks-test / molecule (rockylinux9) (push) Failing after 2m53s
checks-test / molecule (ubuntu2204) (push) Failing after 2m45s
checks-test / molecule (debian11) (push) Successful in 3m50s

This commit is contained in:
2024-01-14 19:04:48 +02:00
commit be7b37ee2e
15 changed files with 334 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
---
name: checks-test
on:
pull_request:
push:
branches:
- main
schedule:
- cron: "30 4 * * 4"
defaults:
run:
working-directory: 'palkx.users'
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout the codebase
uses: actions/checkout@v4
with:
path: 'palkx.users'
- name: Setup Python 3
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Intall test dependencies
run: pip3 install yamllint
- name: Lint code
run: 'yamllint .'
molecule:
name: molecule
runs-on: ubuntu-latest
strategy:
matrix:
distro:
- rockylinux9
- ubuntu2204
- debian11
steps:
- name: Checkout the codebase
uses: actions/checkout@v4
with:
path: 'palkx.users'
- name: Setup Python 3
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Intall test dependencies
run: pip3 install ansible molecule molecule-plugins[docker] docker
- name: Run Molecule tests
run: molecule test
env:
PY_COLORS: '1'
ANSIBLE_FORCE_COLOR: '1'
MOLECULE_DISTRO: ${{ matrix.distro }}

7
.yamllint Normal file
View File

@@ -0,0 +1,7 @@
---
extends: default
rules:
line-length:
max: 120
level: warning

15
Makefile Normal file
View File

@@ -0,0 +1,15 @@
MOLECULE_DISTRO ?= ubuntu2204
.DEFAULT_TARGET := test
.PHONY = converge
converge:
@PY_COLORS=1 ANSIBLE_FORCE_COLOR=1 MOLECULE_DISTRO=$(MOLECULE_DISTRO) molecule converge
.PHONY = test
test:
@PY_COLORS=1 ANSIBLE_FORCE_COLOR=1 MOLECULE_DISTRO=$(MOLECULE_DISTRO) molecule test
.PHONY = destroy
destroy:
@PY_COLORS=1 ANSIBLE_FORCE_COLOR=1 MOLECULE_DISTRO=$(MOLECULE_DISTRO) molecule destroy

38
README.md Normal file
View File

@@ -0,0 +1,38 @@
Role Name
=========
A brief description of the role goes here.
Requirements
------------
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
Role Variables
--------------
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
Dependencies
------------
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
- hosts: servers
roles:
- { role: username.rolename, x: 42 }
License
-------
BSD
Author Information
------------------
An optional section for the role authors to include contact information, or a website (HTML is not allowed).

3
defaults/main.yml Normal file
View File

@@ -0,0 +1,3 @@
---
users: {}
users_groups: {}

2
handlers/main.yml Normal file
View File

@@ -0,0 +1,2 @@
---
# handlers file for users

17
meta/main.yml Normal file
View File

@@ -0,0 +1,17 @@
---
galaxy_info:
author: palkx
description: provision users and groups
license: license (BSD, MIT)
min_ansible_version: 2.1
platforms:
- name: Fedora
versions:
- all
- name: Debian
versions:
- all
- name: Ubuntu
versions:
- all
galaxy_tags: []

View File

@@ -0,0 +1,119 @@
---
- name: Converge
hosts: all
become: true
pre_tasks:
- name: Update apt cache.
package:
update_cache: true
cache_valid_time: 600
when: ansible_os_family == 'Debian'
- name: Ensure build dependencies are installed (RedHat).
package:
name:
- openssh-server
- openssh-clients
state: present
when: ansible_os_family == 'RedHat'
- name: Ensure build dependencies are installed (Fedora).
package:
name: procps
state: present
when: ansible_distribution == 'Fedora'
- name: Ensure build dependencies are installed (Debian).
package:
name:
- openssh-server
- openssh-client
state: present
when: ansible_os_family == 'Debian'
- name: Ensure auth.log file is present.
copy:
dest: /var/log/auth.log
content: ""
force: false
mode: 0644
when: ansible_distribution == 'Debian'
roles:
- role: palkx.users
vars:
users_groups:
admin:
gid: 1000
admin1:
gid: 1001
admin2:
gid: 1002
admin3:
gid: 1003
admin4:
gid: 1004
remote-users:
gid: 856
users:
admin:
shell: /bin/sh
uid: 1000
groups:
- admin
- remote-users
passwords:
# Generated with openssl passwd -6 -salt xyz testpass
default: $6$xyz$nz7SVil2FgVuZ4wjm/1PO31S1QyGBUPVClD55.anfY2pEjs9fUXceRVGsghlUh2I9Jsc2awuh94KOXmNJTcv.0
authorized_keys:
default:
key: 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIAxFJolWPmbFVMMyD9kxQb353ngyUsEebgkK9AcnjOI cardno:13_460_390'
admin1:
shell: /bin/sh
uid: 1001
groups:
- admin1
- remote-users
passwords:
# Generated with openssl passwd -6 -salt xyz testpass
default: $6$xyz$nz7SVil2FgVuZ4wjm/1PO31S1QyGBUPVClD55.anfY2pEjs9fUXceRVGsghlUh2I9Jsc2awuh94KOXmNJTcv.0
authorized_keys:
default:
key: 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIAxFJolWPmbFVMMyD9kxQb353ngyUsEebgkK9AcnjOI cardno:13_460_390'
admin2:
shell: /bin/sh
uid: 1002
groups:
- admin2
- remote-users
passwords:
# Generated with openssl passwd -6 -salt xyz testpass
default: $6$xyz$nz7SVil2FgVuZ4wjm/1PO31S1QyGBUPVClD55.anfY2pEjs9fUXceRVGsghlUh2I9Jsc2awuh94KOXmNJTcv.0
authorized_keys:
default:
key: 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIAxFJolWPmbFVMMyD9kxQb353ngyUsEebgkK9AcnjOI cardno:13_460_390'
admin3:
shell: /bin/sh
uid: 1003
groups:
- admin3
- remote-users
passwords:
# Generated with openssl passwd -6 -salt xyz testpass
default: $6$xyz$nz7SVil2FgVuZ4wjm/1PO31S1QyGBUPVClD55.anfY2pEjs9fUXceRVGsghlUh2I9Jsc2awuh94KOXmNJTcv.0
authorized_keys:
default:
key: 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIAxFJolWPmbFVMMyD9kxQb353ngyUsEebgkK9AcnjOI cardno:13_460_390'
admin4:
shell: /bin/bash
uid: 1004
groups:
- admin4
- remote-users
passwords:
# Generated with openssl passwd -6 -salt xyz testpass
default: $6$xyz$nz7SVil2FgVuZ4wjm/1PO31S1QyGBUPVClD55.anfY2pEjs9fUXceRVGsghlUh2I9Jsc2awuh94KOXmNJTcv.0
authorized_keys:
default:
key: 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIAxFJolWPmbFVMMyD9kxQb353ngyUsEebgkK9AcnjOI cardno:13_460_390'

View File

@@ -0,0 +1,19 @@
---
role_name_check: 1
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
cgroupns_mode: host
privileged: true
pre_build_image: true
provisioner:
name: ansible
playbooks:
converge: ${MOLECULE_PLAYBOOK:-converge.yml}

6
tasks/group.yml Normal file
View File

@@ -0,0 +1,6 @@
---
- name: "Create group `{{ group }}`"
become: true
group:
name: "{{ group }}"
gid: "{{ users_groups[group].get('gid', None) }}"

11
tasks/main.yml Normal file
View File

@@ -0,0 +1,11 @@
---
- include_tasks: group.yml
with_items: "{{ users_groups.keys() }}"
loop_control:
loop_var: group
- include_tasks: user.yml
when: ansible_default_ipv4.address in users[username]['passwords'] or users[username]['passwords'].get('default')
with_items: "{{ users.keys() }}"
loop_control:
loop_var: username

30
tasks/user.yml Normal file
View File

@@ -0,0 +1,30 @@
---
- name: "create user `{{ username }}`"
ansible.builtin.user:
name: "{{ username }}"
append: true
groups: "{{ users[username].get('groups', '') }}"
shell: "{{ users[username].get('shell', '/bin/bash') }}"
uid: "{{ users[username].get('uid', None) }}"
- name: "set specific password for user `{{ username }}`"
ansible.builtin.user:
name: "{{ username }}"
password: "{{ users[username]['passwords'].get(ansible_default_ipv4.address) }}"
when: "ansible_default_ipv4.address in users[username]['passwords'] and
users[username]['passwords'].get(ansible_default_ipv4.address) != 'default'"
- name: "set default password for user `{{ username }}`"
ansible.builtin.user:
name: "{{ username }}"
password: "{{ users[username]['passwords'].get('default') }}"
when: "ansible_default_ipv4.address not in users[username]['passwords'] or
users[username]['passwords'].get(ansible_default_ipv4.address) == 'default'"
- name: "setup ssh key for user `{{ username }}`"
ansible.builtin.authorized_key:
user: "{{ username }}"
state: "{{ users[username]['authorized_keys'][item].get('state', 'present') }}"
key: "{{ users[username]['authorized_keys'][item].get('key') }}"
with_items: "{{ users[username]['authorized_keys'].keys() }}"
when: '"authorized_keys" in users[username]'

2
tests/inventory Normal file
View File

@@ -0,0 +1,2 @@
localhost

5
tests/test.yml Normal file
View File

@@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- users

2
vars/main.yml Normal file
View File

@@ -0,0 +1,2 @@
---
# vars file for users