From be7b37ee2eb9b96fa08669ba383ee63d5174bf87 Mon Sep 17 00:00:00 2001 From: Mykhailo Nikiforov Date: Sun, 14 Jan 2024 19:04:48 +0200 Subject: [PATCH] feat(role): init --- .gitea/workflows/checks-test.yml | 58 +++++++++++++++ .yamllint | 7 ++ Makefile | 15 ++++ README.md | 38 ++++++++++ defaults/main.yml | 3 + handlers/main.yml | 2 + meta/main.yml | 17 +++++ molecule/default/converge.yml | 119 +++++++++++++++++++++++++++++++ molecule/default/molecule.yml | 19 +++++ tasks/group.yml | 6 ++ tasks/main.yml | 11 +++ tasks/user.yml | 30 ++++++++ tests/inventory | 2 + tests/test.yml | 5 ++ vars/main.yml | 2 + 15 files changed, 334 insertions(+) create mode 100644 .gitea/workflows/checks-test.yml create mode 100644 .yamllint create mode 100644 Makefile create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 molecule/default/converge.yml create mode 100644 molecule/default/molecule.yml create mode 100644 tasks/group.yml create mode 100644 tasks/main.yml create mode 100644 tasks/user.yml create mode 100644 tests/inventory create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/.gitea/workflows/checks-test.yml b/.gitea/workflows/checks-test.yml new file mode 100644 index 0000000..fc3cd39 --- /dev/null +++ b/.gitea/workflows/checks-test.yml @@ -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 }} + diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..15a72c1 --- /dev/null +++ b/.yamllint @@ -0,0 +1,7 @@ +--- +extends: default + +rules: + line-length: + max: 120 + level: warning diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4594b86 --- /dev/null +++ b/Makefile @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/README.md @@ -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). diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..fb29139 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,3 @@ +--- +users: {} +users_groups: {} diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..46e2ea7 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for users diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..250f3fa --- /dev/null +++ b/meta/main.yml @@ -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: [] diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 0000000..80e226c --- /dev/null +++ b/molecule/default/converge.yml @@ -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' diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..d291e5b --- /dev/null +++ b/molecule/default/molecule.yml @@ -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} diff --git a/tasks/group.yml b/tasks/group.yml new file mode 100644 index 0000000..7f1ba02 --- /dev/null +++ b/tasks/group.yml @@ -0,0 +1,6 @@ +--- +- name: "Create group `{{ group }}`" + become: true + group: + name: "{{ group }}" + gid: "{{ users_groups[group].get('gid', None) }}" diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..9313ec3 --- /dev/null +++ b/tasks/main.yml @@ -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 diff --git a/tasks/user.yml b/tasks/user.yml new file mode 100644 index 0000000..a9dc6f2 --- /dev/null +++ b/tasks/user.yml @@ -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]' diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..7925515 --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - users diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..4070860 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for users