From 4bab7edf283d1357816cbbf7a6b94e0280f1078f Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Thu, 22 Oct 2026 23:03:44 +0300 Subject: chore: setup ansible --- roles/base/defaults/main.yml | 9 +++ roles/base/handlers/main.yml | 5 ++ roles/base/tasks/main.yml | 152 +++++++++++++++++++++++++++++++++++ roles/base/templates/repositories.j2 | 4 + roles/base/templates/resolv.conf.j2 | 4 + roles/base/templates/sshd_config.j2 | 123 ++++++++++++++++++++++++++++ 6 files changed, 297 insertions(+) create mode 100644 roles/base/defaults/main.yml create mode 100644 roles/base/handlers/main.yml create mode 100644 roles/base/tasks/main.yml create mode 100644 roles/base/templates/repositories.j2 create mode 100644 roles/base/templates/resolv.conf.j2 create mode 100644 roles/base/templates/sshd_config.j2 (limited to 'roles') diff --git a/roles/base/defaults/main.yml b/roles/base/defaults/main.yml new file mode 100644 index 0000000..2f581d8 --- /dev/null +++ b/roles/base/defaults/main.yml @@ -0,0 +1,9 @@ +--- +repositories: + - "https://dl-cdn.alpinelinux.org/alpine/latest-stable/main" + +nameservers: + - "9.9.9.9" + - "9.9.9.10" + - "1.1.1.1" + - "1.0.0.1" diff --git a/roles/base/handlers/main.yml b/roles/base/handlers/main.yml new file mode 100644 index 0000000..6998953 --- /dev/null +++ b/roles/base/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: Restart sshd + ansible.builtin.service: + name: sshd + state: restarted diff --git a/roles/base/tasks/main.yml b/roles/base/tasks/main.yml new file mode 100644 index 0000000..2fe9a88 --- /dev/null +++ b/roles/base/tasks/main.yml @@ -0,0 +1,152 @@ +--- +- block: + - name: Configure resolv.conf + ansible.builtin.template: + src: resolv.conf.j2 + dest: /etc/resolv.conf + owner: root + group: root + mode: '0444' + + - name: Configure Alpine package repositories + ansible.builtin.template: + src: repositories.j2 + dest: /etc/apk/repositories + owner: root + group: root + mode: '0444' + register: apk_repos + + - name: Update apk repository cache + community.general.apk: + update_cache: true + when: apk_repos.changed + + - name: Install baseline native packages via apk + community.general.apk: + name: + - ca-certificates + - curl + - wget + - git + - htop + - acl + - doas + - mosh + - zram-init + state: present + + - name: Clean local Alpine package cache + ansible.builtin.command: + cmd: apk cache clean + changed_when: false + + - name: Create un-privileged user for management + ansible.builtin.user: + name: "{{ username }}" + shell: /bin/ash + groups: wheel + create_home: true + + - name: Configure doas without password for wheel group + ansible.builtin.copy: + content: "permit nopass :wheel\n" + dest: /etc/doas.d/wheel.conf + owner: root + group: root + mode: '0440' + + - name: Deploy sshd_config + ansible.builtin.template: + src: sshd_config.j2 + dest: /etc/ssh/sshd_config + owner: root + group: root + mode: '0400' + validate: /usr/sbin/sshd -t -f %s + notify: Restart sshd + + - name: Configure OpenRC sysinit services + ansible.builtin.service: + name: "{{ item }}" + enabled: true + runlevel: sysinit + loop: + - devfs + - dmesg + - hwdrivers + - mdev + + - name: Configure OpenRC boot services + ansible.builtin.service: + name: "{{ item }}" + enabled: true + runlevel: boot + loop: + - bootmisc + - hostname + - hwclock + - modules + - networking + - seedrng + + - name: Configure OpenRC default services and start them + ansible.builtin.service: + name: "{{ item }}" + enabled: true + state: started + loop: + - crond + - sshd + - zram-init + + - name: Configure OpenRC shutdown services + ansible.builtin.service: + name: "{{ item }}" + enabled: true + runlevel: shutdown + loop: + - killprocs + - mount-ro + - savecache + + - name: Ensure TCP BBR kernel module loads on boot + ansible.builtin.lineinfile: + path: /etc/modules + line: tcp_bbr + create: true + mode: '0644' + register: bbr_module + + - name: Load TCP BBR module immediately if not loaded + ansible.builtin.command: + cmd: modprobe tcp_bbr + # Ensures it's active immediately + when: bbr_module.changed or ansible_facts["kernel"] is defined + + - name: Enable BBR network congestion control + ansible.builtin.sysctl: + name: "{{ item.name }}" + value: "{{ item.value }}" + sysctl_set: true + state: present + reload: true + loop: + # Congestion & Queues + - { name: 'net.core.default_qdisc', value: 'fq' } + - { name: 'net.ipv4.tcp_congestion_control', value: 'bbr' } + - { name: 'net.core.somaxconn', value: '1024' } + - { name: 'net.ipv4.tcp_max_syn_backlog', value: '2048' } + # TCP Performance & Fast Open + - { name: 'net.ipv4.tcp_tw_reuse', value: '1' } + - { name: 'net.ipv4.tcp_fastopen', value: '3' } + - { name: 'net.ipv4.tcp_fin_timeout', value: '15' } + # Security Hardening + - { name: 'net.ipv4.tcp_syncookies', value: '1' } + - { name: 'net.ipv4.conf.all.rp_filter', value: '1' } + - { name: 'net.ipv4.conf.default.rp_filter', value: '1' } + - { name: 'net.ipv4.conf.all.accept_source_route', value: '0' } + - { name: 'net.ipv4.icmp_echo_ignore_broadcasts', value: '1' } + + tags: + - base diff --git a/roles/base/templates/repositories.j2 b/roles/base/templates/repositories.j2 new file mode 100644 index 0000000..096e267 --- /dev/null +++ b/roles/base/templates/repositories.j2 @@ -0,0 +1,4 @@ +# {{ ansible_managed }} +{% for repo in repositories %} +{{ repo }} +{% endfor %} diff --git a/roles/base/templates/resolv.conf.j2 b/roles/base/templates/resolv.conf.j2 new file mode 100644 index 0000000..b3de7ba --- /dev/null +++ b/roles/base/templates/resolv.conf.j2 @@ -0,0 +1,4 @@ +# {{ ansible_managed }} +{% for ns in nameservers %} +nameserver {{ ns }} +{% endfor %} diff --git a/roles/base/templates/sshd_config.j2 b/roles/base/templates/sshd_config.j2 new file mode 100644 index 0000000..dc5029d --- /dev/null +++ b/roles/base/templates/sshd_config.j2 @@ -0,0 +1,123 @@ +# {{ ansible_managed }} + +# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +# The strategy used for options in the default sshd_config shipped with +# OpenSSH is to specify options with their default value where +# possible, but leave them commented. Uncommented options override the +# default value. + +# Include configuration snippets before processing this file to allow the +# snippets to override directives set in this file. +Include /etc/ssh/sshd_config.d/*.conf + +#Port 22 +#AddressFamily any +#ListenAddress 0.0.0.0 +#ListenAddress :: + +#HostKey /etc/ssh/ssh_host_rsa_key +#HostKey /etc/ssh/ssh_host_ecdsa_key +#HostKey /etc/ssh/ssh_host_ed25519_key +#HostKey /etc/ssh/ssh_host_mldsa44_ed25519_key + +# Ciphers and keying +#RekeyLimit default none + +# Logging +#SyslogFacility AUTH +#LogLevel INFO + +# Authentication: + +#LoginGraceTime 2m +PermitRootLogin no +#StrictModes yes +#MaxAuthTries 6 +#MaxSessions 10 + +#PubkeyAuthentication yes + +# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2 +# but this is overridden so installations will only check .ssh/authorized_keys +AuthorizedKeysFile .ssh/authorized_keys + +#AuthorizedPrincipalsFile none + +#AuthorizedKeysCommand none +#AuthorizedKeysCommandUser nobody + +# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts +#HostbasedAuthentication no +# Change to yes if you don't trust ~/.ssh/known_hosts for +# HostbasedAuthentication +#IgnoreUserKnownHosts no +# Don't read the user's ~/.rhosts and ~/.shosts files +#IgnoreRhosts yes + +# To disable tunneled clear text passwords, change to "no" here! +PasswordAuthentication no +#PermitEmptyPasswords no + +# Change to "no" to disable keyboard-interactive authentication. Depending on +# the system's configuration, this may involve passwords, challenge-response, +# one-time passwords or some combination of these and other methods. +KbdInteractiveAuthentication no + +# Kerberos options +#KerberosAuthentication no +#KerberosOrLocalPasswd yes +#KerberosTicketCleanup yes +#KerberosGetAFSToken no + +# GSSAPI options +#GSSAPIAuthentication no +#GSSAPICleanupCredentials yes + +# Set this to 'yes' to enable PAM authentication, account processing, +# and session processing. If this is enabled, PAM authentication will +# be allowed through the KbdInteractiveAuthentication and +# PasswordAuthentication. Depending on your PAM configuration, +# PAM authentication via KbdInteractiveAuthentication may bypass +# the setting of "PermitRootLogin prohibit-password". +# If you just want the PAM account and session checks to run without +# PAM authentication, then enable this but set PasswordAuthentication +# and KbdInteractiveAuthentication to 'no'. +#UsePAM no + +#AllowAgentForwarding yes +# Feel free to re-enable these if your use case requires them. +AllowTcpForwarding yes +GatewayPorts yes +X11Forwarding no +#X11DisplayOffset 10 +#X11UseLocalhost yes +#PermitTTY yes +#PrintMotd yes +#PrintLastLog yes +#TCPKeepAlive yes +#PermitUserEnvironment no +#Compression delayed +#ClientAliveInterval 0 +#ClientAliveCountMax 3 +#UseDNS no +#PidFile /run/sshd.pid +#MaxStartups 10:30:100 +#PermitTunnel no +#ChrootDirectory none +#VersionAddendum none + +# no default banner path +#Banner none + +# override default of no subsystems +Subsystem sftp internal-sftp + +# Example of overriding settings on a per-user basis +#Match User anoncvs +# X11Forwarding no +# AllowTcpForwarding no +# PermitTTY no +# ForceCommand cvs server + +Include /etc/ssh/sshd_config.d/andr/config -- cgit v1.3.1