Prerequisites
Install Ansible
Install Ansible on your control machine (the computer you’ll run the playbooks from).
See the official Ansible installation guide for more options.
Create SSH Keys
Create SSH public/private key pairs for secure authentication:
Press Enter to accept the default location, and optionally set a passphrase for additional security.
Configure Variables
Edit the variables file to match your environment:Change all variables according to your needs, including:
- Username and password
- SSH port
- Firewall rules
- Any other customizations
Enable SSH Root Access (Temporarily)
Before running the playbooks, temporarily enable SSH root access on your target server:Find and modify:Then restart SSH:
Configure Static IP (Recommended)
It’s recommended to configure a static IP address on your system before running the playbooks.
Running the Playbooks
Requirements Playbook
Run the requirements playbook first, using the root password you specified during server installation:This playbook sets up the initial requirements before the main hardening process.
Main Playbook
Run the main playbook with the new user’s password you specified in thevariables.yml file:
Subsequent Runs
If you need to run the playbooks multiple times, use the SSH key and the new SSH port:Replace
SSH_PORT with your configured SSH port and /PATH/TO/SSH/KEY with the path to your SSH private key.What Gets Configured
The Ansible playbooks automate the following security configurations:SSH Hardening
- Disable root login
- Configure SSH keys
- Set secure SSH options
- Change default SSH port
Firewall Configuration
- Install and configure UFW
- Set up default deny policies
- Allow specified services
User Management
- Create limited user accounts
- Configure sudo access
- Set up proper groups
System Hardening
- Install security updates
- Configure fail2ban
- Set up automatic updates
- Additional hardening measures
Post-Playbook Verification
Troubleshooting
If you encounter issues:- Check Ansible output - The playbook provides detailed output about each task
- Verify variables - Ensure all variables in
variables.ymlare correct - Test connectivity - Make sure you can reach the target server
- Review logs - Check both Ansible logs and system logs on the target server
For detailed manual configuration steps, refer to the other sections of this guide. The Ansible playbooks are a convenient automation layer on top of the manual processes.