Created a role for initial lockdown of recent VPS, and started role for basic server configuration

This commit is contained in:
Alex Tavarez
2025-09-05 00:43:14 -04:00
parent e427da26a6
commit 0cafb4968b
12 changed files with 296 additions and 0 deletions

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).

View File

@@ -0,0 +1,15 @@
#SPDX-License-Identifier: MIT-0
---
# defaults file for lockdown
files_mode: no
# create_groups:
# - group_name: "ftp"
create_users:
- username: "{{ hostvars['server'][0].username }}"
password: "{{ hostvars['server'][0].password }}"
# ssh_authorize: yes
# web_users:
# - caddy
# - www-data
ssh_pubkey_filename_pattern: '.*\.pub'
include_root_lock: yes

View File

@@ -0,0 +1,2 @@
PasswordAuthentication no
PermitEmptyPasswords no

View File

@@ -0,0 +1 @@
PermitRootLogin no

View File

@@ -0,0 +1,13 @@
# SPDX-License-Identifier: MIT-0
---
# handlers file for lockdown
- name: Restart SSH server
when: ansible_facts["user_id"] == "root"
ansible.builtin.service:
name: ssh
state: restarted
tags:
- default
- restart_ssh
register: restarted_ssh
listen: "restart ssh"

View File

@@ -0,0 +1,35 @@
#SPDX-License-Identifier: MIT-0
galaxy_info:
author: your name
description: your role description
company: your company (optional)
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.1
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.

View File

@@ -0,0 +1,12 @@
# SPDX-License-Identifier: MIT-0
---
# tasks file for lockdown
- name: Disable shell for root user
when: ansible_facts["user_id"] != "root"
become: true
ansible.builtin.user:
name: root
shell: /sbin/nologin
tags:
- deshell_root
register: root_shell_disabled

View File

@@ -0,0 +1,160 @@
# SPDX-License-Identifier: MIT-0
---
# tasks file for lockdown
# @NOTE: assumes one logged in to SSH server as root to begin with, hence no need for privilege escalation
- name: Create users
when: ansible_facts["user_id"] == "root"
block:
- name: Create sys-admin user
ansible.builtin.user:
name: "{{ create_users[0].username }}"
uid: 1000
password: "{{ create_users[0].password }}"
append: yes
groups:
- sudo
shell: /bin/bash
generate_ssh_key: yes
password_expire_min: 93
password_expire_max: 186
password_expire_warn: 45
comment: sysadmin
# ssh_key_passphrase: "{{ item.password }}"
state: present
tags:
- default
- administrative_user
register: created_admin
- name: Create new user
ansible.builtin.user:
name: "{{ item.username }}"
uid: 1000
password: "{{ item.password }}"
append: yes
groups:
- sudo
shell: /bin/bash
generate_ssh_key: yes
password_expire_min: 93
password_expire_max: 186
password_expire_warn: 45
comment: administrator
# ssh_key_passphrase: "{{ item.password }}"
state: present
loop: "{{ create_users[1:] }}"
tags:
- other_users
register: created_user
- name: Specify authorized SSH keys for users based on local public keys
when: not files_mode and ansible_facts["user_id"] == "root"
block:
- name: Acquire list of SSH public keys for sys-admin user
ansible.builtin.find:
paths: "{{ lookup('env', 'HOME') }}/.ssh"
patterns:
- '{{ ssh_pubkey_filename_pattern }}'
use_regex: yes
recurse: no
tags:
- default
- administrative_user
- admin_ssh
register: ssh_public_keys
- name: Register SSH public keys as sys-admin user's authorized keys
ansible.builtin.lineinfile:
path: "{{ created_admin.home }}/.ssh/authorized_keys"
line: "{{ lookup('ansible.builtin.file', item) }}"
owner: "{{ created_admin.name }}"
group: "{{ created_admin.name }}"
mode: "0600"
create: yes
insertafter: ~
state: present
tags:
- default
- administrative_user
- admin_ssh
loop: "{{ ssh_public_keys.files }}"
- name: Register SSH puplic keys as other users' authorized keys
ansible.builtin.copy:
src: "ssh/{{ item.name }}/authorized_keys"
dest: "{{ item.home }}/.ssh/authorized_keys"
force: yes
backup: yes
owner: "{{ item.name }}"
group: "{{ item.name }}"
mode: "0600"
tags:
- other_users
- others_ssh
loop: "{{ created_user }}"
register: authorized_ssh_pubkeys
- name: Specify authorized SSH keys for users
when: files_mode and ansible_facts["user_id"] == "root"
block:
- name: Specify authorized keys file for sys-admin user
ansible.builtin.copy:
src: ssh/authorized_keys
dest: "{{ created_admin.home }}/.ssh/authorized_keys"
force: yes
backup: yes
owner: "{{ created_admin.name }}"
group: "{{ created_admin.name }}"
mode: "0600"
tags:
- default
- administrative_user
- admin_ssh
register: authorized_admin_ssh_pubkeys
- name: Specify authorized keys file for other users
ansible.builtin.copy:
src: "ssh/{{ item.name }}/authorized_keys"
dest: "{{ item.home }}/.ssh/authorized_keys"
force: yes
backup: yes
owner: "{{ item.name }}"
group: "{{ item.name }}"
mode: "0600"
tags:
- other_users
- others_ssh
loop: "{{ created_user }}"
register: authorized_ssh_pubkeys
- name: Lock down root SSH access
when: ansible_facts["user_id"] == "root"
block:
- name: Constrain SSH authentication methods to using SSH key
ansible.builtin.copy:
src: sshd_config.d/auth.conf
dest: /etc/ssh/sshd_config.d/auth.conf
force: yes
backup: yes
mode: "0644"
tags:
- depass_root
register: constrained_auth
- name: Prohibit access to root via SSH
ansible.builtin.copy:
src: sshd_config.d/denyroot.conf
dest: /etc/ssh/sshd_config.d/denyroot.conf
force: yes
backup: yes
mode: "0644"
tags:
- prohib_root_ssh
register: prohibited_root_ssh_login
- name: Lock the root account
when: include_root_lock
ansible.builtin.user:
name: root
password_lock: yes
tags:
- delog_root
register: prohibited_root_login
tags:
- default
- deroot
notify: "restart ssh"
- name: Import disabling of shell root by sys-admin user
ansible.builtin.import_tasks:
file: ./deshell.yml

View File

@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
localhost

View File

@@ -0,0 +1,6 @@
#SPDX-License-Identifier: MIT-0
---
- hosts: localhost
remote_user: root
roles:
- lockdown