summaryrefslogtreecommitdiff
path: root/tasks
diff options
context:
space:
mode:
authorheqnx <root@heqnx.com>2025-10-02 13:08:58 +0300
committerheqnx <root@heqnx.com>2025-10-02 13:08:58 +0300
commitc00c75d310e2afea3b521e8a4b90e800ab4f9ff2 (patch)
tree7875697bfeadfba2276e2e1d2066eca551bf3e17 /tasks
parentac5767248e7b20b376127ac8e3cfdd1a7aaf4193 (diff)
downloadansible-cockpit-c00c75d310e2afea3b521e8a4b90e800ab4f9ff2.tar.gz
ansible-cockpit-c00c75d310e2afea3b521e8a4b90e800ab4f9ff2.zip
initial commitHEADmain
Diffstat (limited to 'tasks')
-rw-r--r--tasks/cockpit_configure.yaml105
-rw-r--r--tasks/harden.yaml151
-rw-r--r--tasks/preflight.yaml16
3 files changed, 272 insertions, 0 deletions
diff --git a/tasks/cockpit_configure.yaml b/tasks/cockpit_configure.yaml
new file mode 100644
index 0000000..dc93a3d
--- /dev/null
+++ b/tasks/cockpit_configure.yaml
@@ -0,0 +1,105 @@
+- name: remove /etc/nginx/sites-enabled directory
+ file:
+ path: /etc/nginx/sites-enabled
+ state: absent
+
+- name: remove /etc/nginx/sites-available directory
+ file:
+ path: /etc/nginx/sites-available
+ state: absent
+
+- name: remove /var/www/html directory
+ file:
+ path: /var/www/html
+ state: absent
+ ignore_errors: true
+
+- name: ensure /var/www/html directory exists
+ file:
+ path: /var/www/html
+ state: directory
+ mode: '0755'
+ owner: www-data
+ group: www-data
+
+- name: set directory permissions to 755
+ ansible.builtin.file:
+ path: /var/www/html/
+ recurse: yes
+ state: directory
+ mode: '0755'
+ owner: www-data
+ group: www-data
+
+- name: ensure /etc/cockpit directory exists
+ file:
+ path: /etc/cockpit
+ state: directory
+ mode: '0755'
+ owner: root
+ group: root
+
+- name: create /etc/systemd/system/cockpit.socket.d directory
+ file:
+ path: /etc/systemd/system/cockpit.socket.d
+ state: directory
+ mode: '0755'
+ owner: root
+ group: root
+
+- name: ensure /etc/NetworkManager/conf.d/ directory exists
+ file:
+ path: /etc/NetworkManager/conf.d/
+ state: directory
+ mode: '0755'
+ owner: root
+ group: root
+
+- name: set up systemd cockpit socket override.conf
+ copy:
+ src: files/cockpit.socket.override.conf
+ dest: /etc/systemd/system/cockpit.socket.d/override.conf
+ mode: '0644'
+
+- name: generate /etc/cockpit/cockpit.conf
+ template:
+ src: templates/cockpit.conf.j2
+ dest: /etc/cockpit/cockpit.conf
+ owner: root
+ group: root
+ mode: '0644'
+
+- name: generate /etc/nginx/nginx.conf from template
+ template:
+ src: templates/nginx.conf.j2
+ dest: /etc/nginx/nginx.conf
+ owner: root
+ group: root
+ mode: '0644'
+
+- name: fix packagekit offline with dummy network interface
+ copy:
+ dest: /etc/NetworkManager/conf.d/10-globally-managed-devices.conf
+ content: |
+ [keyfile]
+ unmanaged-devices=none
+
+- name: create dummy network intereface
+ command: nmcli con add type dummy con-name fake ifname fake0 ip4 1.2.3.4/24 gw4 1.2.3.1
+
+- name: reload systemd daemon
+ command: systemctl daemon-reload
+
+- name: restart cockpit service
+ systemd:
+ name: cockpit.service
+ state: restarted
+ enabled: true
+ when: ansible_service_mgr == 'systemd'
+
+- name: restart nginx service
+ systemd:
+ name: nginx
+ state: restarted
+ enabled: true
+ when: ansible_service_mgr == 'systemd'
diff --git a/tasks/harden.yaml b/tasks/harden.yaml
new file mode 100644
index 0000000..2cdb15b
--- /dev/null
+++ b/tasks/harden.yaml
@@ -0,0 +1,151 @@
+- name: update apt packages
+ apt:
+ update_cache: true
+
+- name: install apt packages
+ apt:
+ name: "{{ apt_packages }}"
+ state: present
+ update_cache: true
+ environment:
+ DEBIAN_FRONTEND: noninteractive
+
+- name: clear /etc/issue and /etc/motd
+ copy:
+ content: ""
+ dest: "{{ item }}"
+ loop:
+ - /etc/issue
+ - /etc/motd
+
+- name: check if /etc/update-motd.d directory exists
+ stat:
+ path: /etc/update-motd.d
+ register: motd_dir
+
+- name: find files in /etc/update-motd.d
+ find:
+ paths: /etc/update-motd.d
+ file_type: file
+ register: motd_files
+ when: motd_dir.stat.exists
+
+- name: remove execute permissions from all files in /etc/update-motd.d
+ file:
+ path: "{{ item.path }}"
+ mode: u-x,g-x,o-x
+ loop: "{{ motd_files.files }}"
+ when: motd_dir.stat.exists
+
+- name: enforce root-only cron/at
+ file:
+ path: "{{ item }}"
+ state: touch
+ owner: root
+ group: root
+ mode: '0600'
+ loop:
+ - /etc/cron.allow
+ - /etc/at.allow
+
+- name: remove deny files for cron and at
+ file:
+ path: "{{ item }}"
+ state: absent
+ loop:
+ - /etc/cron.deny
+ - /etc/at.deny
+
+- name: backup sshd_config
+ copy:
+ src: /etc/ssh/sshd_config
+ dest: "/etc/ssh/sshd_config.bak_{{ ansible_date_time.iso8601_basic }}"
+ remote_src: true
+
+- name: harden sshd_config
+ copy:
+ dest: /etc/ssh/sshd_config
+ content: |
+ Port 22
+ Banner /etc/issue
+ UsePAM yes
+ Protocol 2
+ Subsystem sftp /usr/lib/openssh/sftp-server
+ LogLevel verbose
+ PrintMotd no
+ #AcceptEnv LANG LC_*
+ MaxSessions 5
+ StrictModes yes
+ Compression no
+ MaxAuthTries 3
+ IgnoreRhosts yes
+ PrintLastLog yes
+ AddressFamily inet
+ X11Forwarding no
+ PermitRootLogin yes
+ AllowTcpForwarding yes
+ ClientAliveInterval 1200
+ AllowAgentForwarding no
+ PermitEmptyPasswords no
+ ClientAliveCountMax 0
+ GSSAPIAuthentication no
+ KerberosAuthentication no
+ IgnoreUserKnownHosts yes
+ PermitUserEnvironment no
+ ChallengeResponseAuthentication no
+ MACs hmac-sha2-512,hmac-sha2-256
+ Ciphers aes128-ctr,aes192-ctr,aes256-ctr
+
+- name: regenerate SSH host keys
+ shell: |
+ rm -f /etc/ssh/ssh_host_*key*
+ ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
+ ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ""
+ args:
+ creates: /etc/ssh/ssh_host_ed25519_key
+
+- name: restart ssh
+ systemd:
+ name: ssh
+ state: restarted
+ enabled: true
+ when: ansible_service_mgr == 'systemd'
+
+- name: enable unattended-upgrades
+ shell: dpkg-reconfigure --priority=low unattended-upgrades
+ args:
+ creates: /etc/apt/apt.conf.d/50unattended-upgrades
+
+- name: restart unattended-upgrades
+ systemd:
+ name: unattended-upgrades
+ state: restarted
+ enabled: true
+ when: ansible_service_mgr == 'systemd'
+
+- name: disable ipv6 in grub
+ lineinfile:
+ path: /etc/default/grub
+ regexp: '^GRUB_CMDLINE_LINUX='
+ line: 'GRUB_CMDLINE_LINUX="ipv6.disable=1"'
+
+- name: update grub
+ command: update-grub
+
+- name: allow ssh port and enable ufw
+ ufw:
+ rule: allow
+ port: 22
+ proto: tcp
+
+- name: enable ufw
+ ufw:
+ state: enabled
+ policy: deny
+
+- name: restart ufw
+ systemd:
+ name: ufw
+ state: restarted
+ enabled: true
+ when: ansible_service_mgr == 'systemd'
diff --git a/tasks/preflight.yaml b/tasks/preflight.yaml
new file mode 100644
index 0000000..3358d46
--- /dev/null
+++ b/tasks/preflight.yaml
@@ -0,0 +1,16 @@
+- name: ensure script is run as root
+ assert:
+ that:
+ - ansible_effective_user_id == 0
+ fail_msg: "this playbook must be run as root"
+
+- name: check if system is debian-based
+ command: dpkg -l
+ register: dpkg_check
+ changed_when: false
+ failed_when: false
+
+- name: fail if not debian-based
+ fail:
+ msg: "distribution not Debian-based"
+ when: dpkg_check.rc != 0