Building an Ansible Automated Compliance Enforcement Framework for AWS Linux Servers
Introduction
Managing the security configuration of Linux servers is an important responsibility in cloud and infrastructure engineering.
When you manage multiple servers, checking their configurations manually can become repetitive, time-consuming and difficult to standardize.
You might need to inspect SSH authentication settings, verify firewall rules, review privileged accounts, check sensitive file permissions, inspect running services, monitor pending security updates, validate logging and review network security parameters.
Doing these checks manually across multiple servers introduces inconsistencies and makes it difficult to maintain reliable compliance evidence.
I wanted to approach this problem differently.
Instead of repeatedly connecting to individual servers and running commands manually, I decided to build an Ansible Automated Compliance Enforcement framework that could inspect Linux security configurations across multiple AWS EC2 instances, identify compliance gaps, and support controlled remediation.
This project was an opportunity to apply practical skills in Ansible, AWS, Linux administration, infrastructure automation and security hardening.
In this article, I will walk through the architecture, implementation approach, security checks, troubleshooting process, actual audit findings and lessons I learned while building the project.
This is a practical engineering project, not a theoretical demonstration.
1. Project Overview
Project: Ansible Automated Compliance Enforcement
Author: Jamiu Olatunji Bakre
Category: DevOps / Platform Engineering / Infrastructure Automation / Linux Security
The objective was to build a reusable automation framework that could:
- Connect to multiple Ubuntu servers using SSH.
- Inspect Linux security configurations.
- Identify security misconfigurations.
- Evaluate compliance against defined security expectations.
- Separate security auditing from remediation.
- Apply approved configuration changes.
- Re-run audits to validate remediation.
- Support structured compliance reporting.
Infrastructure used
The environment consisted of:
| Component | Description |
|---|---|
| Control node | Ubuntu running on WSL |
| Managed servers | Two Ubuntu AWS EC2 instances |
| Automation framework | Ansible |
| Connection method | SSH |
| Remote user | ubuntu |
| Authentication | SSH private key |
| Version control | Git |
| Operating environment | Linux |
Screenshot 1 — Project environment
Capture your WSL Ubuntu terminal showing your project directory, Ansible version and working environment.
01-project-environment.png
2. Understanding the Architecture
I designed the project around an agentless Ansible architecture.
The control node runs Ansible and communicates with the managed servers over SSH.
There is no requirement to install a separate Ansible agent on each managed server.
The control node handles orchestration, while the managed nodes execute the required tasks.
Architecture diagram
flowchart TB
A["Ansible Control Node<br/>Ubuntu on WSL"]
B["Ansible Inventory"]
C["Compliance Playbook"]
D["SSH Authentication"]
E["Compliance Audit Role"]
F["Compliance Remediation Role"]
G["AWS EC2<br/>compliance-server-01"]
H["AWS EC2<br/>compliance-server-02"]
I["Security Findings"]
J["Post-Remediation Audit"]
A --> B
B --> C
C --> D
D --> G
D --> H
C --> E
C --> F
E --> G
E --> H
G --> I
H --> I
F --> G
F --> H
F --> J
J --> I
How the architecture works
1. Ansible control node
This is the machine from which I execute Ansible commands and playbooks.
My control node runs Ubuntu under Windows Subsystem for Linux (WSL).
2. Inventory
The inventory tells Ansible which servers to manage and how to connect to them.
3. SSH authentication
Ansible establishes remote connections using SSH private-key authentication.
4. Managed nodes
These are the two Ubuntu EC2 instances receiving the audit and remediation tasks.
5. Compliance playbook
The central playbook coordinates the execution process.
6. Audit and remediation roles
The audit role checks the security configuration, while the remediation role applies selected changes.
7. Post-remediation verification
After remediation, the audit is executed again to determine whether the intended configuration has been achieved.
The architecture allows the same automation to be executed against multiple servers without manually connecting to each one.
Screenshot 2 — Architecture and AWS infrastructure
Capture the AWS EC2 Instances page showing the two managed instances. Hide public IP addresses, account identifiers and other sensitive information.
Suggested filename: 02-aws-ec2-infrastructure.png
3. Preparing the Ansible Environment
I used Ubuntu on WSL as my Ansible control node.
The project was organized inside my Linux home directory.
mkdir -p ~/ansible-automated-compliance-enforcement
cd ~/ansible-automated-compliance-enforcement
I verified the Ansible installation:
ansible --version
Ansible provides the automation engine used to execute tasks against the managed infrastructure.
SSH authentication
The managed servers were accessed using an SSH private key.
An important troubleshooting issue occurred during the initial connection setup.
The inventory referenced a private-key path that did not match the actual location of the key.
The investigation identified the intended key location:
/home/cloud/.ssh/Your-key.pem
I restricted access to the private key:
chmod 400 ~/.ssh/your-key-pair.pem
This ensures that the private key is not readable or writable by other users.
The private key itself is never included in the project repository.
Screenshot 3 — Ansible installation
Capture the output of ansible --version.
Suggested filename: 03-ansible-version.png
4. Configuring the Ansible Inventory
Ansible inventory defines the managed infrastructure.
I grouped the two EC2 servers under:
[compliance_servers]
compliance-server-01 ansible_host=YOUR_SERVER_01_IP
compliance-server-02 ansible_host=YOUR_SERVER_02_IP
[compliance_servers:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/YOUR_USER/.ssh/Your-key.pem
The IP addresses above are placeholders. Use your current server addresses when configuring your own environment.
The group compliance_servers allows me to execute the same playbook against both managed nodes.
For example:
ansible compliance_servers -m ping
Both servers returned successful Ansible connectivity results during the project.
Why inventory groups matter
Without inventory groups, managing multiple servers can become difficult as infrastructure grows.
By grouping related servers, I can target them together, assign shared variables and reuse the same automation.
Screenshot 4 — Inventory configuration
Capture the inventory file showing the two host aliases and the inventory group. Blur IP addresses and hide the private-key path if it contains personal information.
Suggested filename: 04-ansible-inventory.png
5. Understanding Ansible Connectivity
One of the first important tests was confirming that the control node could communicate with both EC2 instances.
I executed:
ansible compliance_servers -m ping
Both managed servers returned successful results.
This was an important milestone.
The Ansible ping module is different from the traditional Linux ICMP ping command.
Instead of simply testing network reachability, the Ansible ping module checks whether Ansible can establish its configured connection and execute a small module successfully.
This helped establish that:
- SSH authentication was working.
- The inventory was correctly configured.
- The managed servers were reachable through Ansible.
- Remote module execution was functioning.
6. Structuring the Project with Reusable Ansible Roles
I wanted the project to be maintainable rather than putting every security check into one large playbook.
I therefore separated the automation into two major responsibilities:
- Compliance auditing.
- Compliance remediation.
The audit role inspects the systems.
The remediation role applies selected configuration changes.
The central playbook coordinates both.
The recovered project structure includes the following components:
ansible-automated-compliance-enforcement/
│
├── inventory/
│ └── group_vars/
│ └── all/
│ └── vars.yml
│
├── playbooks/
│ └── compliance.yml
│
├── roles/
│ ├── compliance_checks/
│ │ ├── defaults/
│ │ │ └── main.yml
│ │ └── tasks/
│ │ ├── main.yml
│ │ ├── firewall.yml
│ │ └── network.yml
│ │
│ └── compliance_remediation/
│ └── tasks/
│ ├── main.yml
│ ├── ssh.yml
│ ├── firewall.yml
│ └── network.yml
│
├── README.md
└── .gitignore
This is a partial, evidence-backed representation of the implementation. The complete repository may contain additional task files and configuration components.
Why use roles?
Ansible roles provide a way to organize related tasks, variables, templates and handlers.
For example, instead of placing all SSH, firewall and network tasks in one file, I can organize them by responsibility.
This improves:
- Maintainability.
- Reusability.
- Readability.
- Troubleshooting.
- Future expansion.
Screenshot 6 — Project structure
Capture the output of tree or find showing your actual project structure.
Suggested filename: 06-project-structure.png
7. The Eight Linux Compliance Categories
The framework was organized around eight security categories.
Each category focuses on a different aspect of Linux server security.
7.1 SSH Security
SSH is one of the most important remote administration services on a Linux server.
An insecure SSH configuration can expose systems to unnecessary authentication risks.
The audit examined effective SSH configuration, including:
- Root login restrictions.
- Password authentication.
- Public-key authentication.
- Maximum authentication attempts.
During the initial inspection, the following values were observed:
MaxAuthTries 6
PermitRootLogin without-password
PubkeyAuthentication yes
PasswordAuthentication no
The intended stricter baseline was:
PermitRootLogin no
PasswordAuthentication no
MaxAuthTries 4
For remediation, I prepared an Ansible-managed configuration file:
/etc/ssh/sshd_config.d/00-ansible-compliance.conf
An important part of safe SSH remediation is validating the configuration before reloading the daemon.
The validation command is:
sshd -t
If validation succeeds, the configuration syntax is accepted by the SSH daemon.
This helps prevent an invalid configuration from breaking remote administrative access.
The project also used configuration backups and controlled service handling.
The subsequent audit verified the intended effective SSH settings on both managed servers.
Engineering lesson: Security hardening should not compromise the ability to administer the infrastructure.
Capture the actual SSH audit output showing the effective configuration and PASS results.
7.2 Firewall Security
The second category focused on UFW, the Uncomplicated Firewall.
The audit inspected:
- Whether UFW was installed.
- Whether the firewall was active.
- Default incoming policy.
- SSH application profile.
Commands used during the investigation included:
ufw version
ufw status verbose
ufw app info OpenSSH
One important discovery was that having UFW installed did not mean that the firewall was enabled.
The initial audit identified inactive firewall status.
Before enabling a firewall remotely, I verified that SSH access was permitted.
The remediation sequence included allowing the OpenSSH application profile, setting default policies and enabling UFW.
Subsequent audit results confirmed that UFW was active and the default incoming policy was deny.
This was one of the key remediation milestones in the project.
7.3 User and Account Security
User account security is important because unauthorized privileged access can compromise an entire server.
The project included checks involving:
- Sudoers configuration validity.
- Sudo group membership.
- Admin group membership.
- Accounts with empty password fields.
- Unauthorized sudo users.
One of the validation commands was:
visudo -c
The historical output included:
/etc/sudoers: parsed OK
/etc/sudoers.d/90-cloud-init-users: parsed OK
The recorded audit results showed no empty-password accounts and no unauthorized sudo users under the project's defined checks.
This helped establish that the inspected account configuration satisfied the implemented baseline.
Capture the actual user audit results, including sudoers validation and account findings. Avoid exposing unnecessary usernames.
7.4 File and Directory Permissions
Linux permissions determine who can read, write or execute files.
Incorrect permissions on sensitive system files can expose information or allow unauthorized modification.
The audit included important account-related files such as:
| File | Expected permission |
|---|---|
/etc/passwd |
0644 |
/etc/shadow |
0600 |
The historical audit recorded both files as passing their permission checks.
Understanding file ownership and permission modes is essential when automating Linux security.
For example:
stat -c '%a %U %G %n' /etc/passwd /etc/shadow
This command displays the permissions, owner, group and filename.
Capture the permission inspection output for the actual files checked by your project.
7.5 Legacy and Unnecessary Services
Unnecessary services increase the attack surface of a server.
The project inspected installed packages, service units and listening ports according to its configured checks.
It is important to distinguish between:
- A package being installed.
- A service being enabled.
- A service currently running.
- A process listening on a network port.
These are different conditions.
The historical audit reported no prohibited packages, service units or listening ports under the project's configured policy.
I consider this an important part of infrastructure security because unnecessary services should be reviewed rather than automatically removed without understanding their dependencies.
Capture the actual service compliance output and findings.
7.6 System Updates and Patch Compliance
Keeping Linux systems updated is an important security responsibility.
The project inspected pending package updates and security updates.
The historical findings were:
| Finding | Per server |
|---|---|
| Total pending updates | 14 |
| Security updates | 11 |
This was a compliance finding rather than a successful patching result.
I deliberately distinguish between identifying pending updates and installing them.
Blindly upgrading production infrastructure can introduce compatibility problems, service interruptions or reboot requirements.
A controlled patching process should consider:
- Maintenance windows.
- Package dependencies.
- Application compatibility.
- Backup and recovery.
- Reboot requirements.
- Post-update validation.
The audit therefore provides useful operational visibility without automatically treating every pending update as an instruction to upgrade immediately.
Capture the actual Ansible package audit output showing the number of pending updates and security updates.
7.7 Logging and Audit Configuration
Logs are essential for understanding what happens on a Linux server.
They support:
- Troubleshooting.
- Security investigations.
- Authentication monitoring.
- Service failure analysis.
- Operational accountability.
The project inspected logging-related configuration, including rsyslog and systemd-journald.
The historical audit recorded both facilities as passing their defined checks.
The configuration also included an inspection of ForwardToSyslog.
This category helps ensure that important operational logging facilities are available and configured according to the project's expectations.
Capture the actual logging audit results, including the services and configuration values inspected.
7.8 Network Security
Network security was one of the more interesting parts of this project.
I wanted to inspect Linux kernel network parameters rather than relying only on firewall rules.
The audit included:
- IPv4 forwarding.
- IPv4 ICMP redirect acceptance.
- IPv4 ICMP redirect sending.
- IPv6 forwarding.
- IPv6 ICMP redirect acceptance.
- Relevant default and interface-specific settings.
I used the following command to inspect the network parameters:
ansible compliance_servers -b -m shell -a \
"sysctl net.ipv4.ip_forward \
net.ipv4.conf.all.accept_redirects \
net.ipv4.conf.default.accept_redirects \
net.ipv4.conf.all.send_redirects \
net.ipv4.conf.default.send_redirects \
net.ipv6.conf.all.forwarding \
net.ipv6.conf.default.forwarding \
net.ipv6.conf.all.accept_redirects \
net.ipv6.conf.default.accept_redirects"
Discovering network interfaces
Another important step was discovering the interfaces on the managed servers.
I executed:
ansible compliance_servers -m setup \
-a "filter=ansible_interfaces"
The discovered interfaces included:
lo
ens5
This matters because checking only global network parameters may not reveal every interface-specific configuration.
Runtime versus persistent configuration
Linux sysctl parameters can be configured at runtime or persisted across reboots.
For example:
sysctl net.ipv4.ip_forward
displays the current runtime value.
Persistent configuration can be stored in files under:
/etc/sysctl.d/
The project included a persistent security configuration file:
/etc/sysctl.d/99-ansible-network-security.conf
This distinction is important because a setting applied only at runtime may disappear after a reboot.
The later audit verified the persistent network baseline and reported passing network checks.
Capture the actual sysctl inspection output, discovered interfaces and network compliance results.
8. Separating Compliance Auditing from Remediation
One of the most important architectural decisions was keeping the audit and remediation responsibilities separate.
I organized the project around two roles:
roles/
├── compliance_checks/
│
└── compliance_remediation/
Audit mode
Audit mode inspects the server configuration and identifies compliance findings.
It should not intentionally modify the managed systems.
Remediation mode
Remediation mode allows the automation to apply selected security configuration changes.
The intended workflow is:
Gather facts
|
v
Validate execution mode
|
v
Run compliance audit
|
v
Check for remediation mode
|
v
Apply approved remediation
|
v
Run compliance audit again
|
v
Review final findings
The project supports two execution modes:
audit
and:
remediate
The default execution mode was configured as audit.
This is useful because an ordinary audit should not unexpectedly modify infrastructure.
Why this design matters
Imagine running a compliance check against ten production servers.
You want to know which systems are misconfigured before making changes.
Separating the two responsibilities allows you to inspect first, review findings and then decide whether remediation is appropriate.
It also makes troubleshooting easier because you can distinguish between problems discovered during inspection and changes introduced during remediation.
Screenshot 15 — Audit and remediation architecture
[SCREENSHOT PLACEHOLDER 15: PLAYBOOK AND ROLE STRUCTURE]
Capture the central compliance playbook and the two role directories in your project.
Suggested filename: 15-audit-remediation-architecture.png
9. Safe Remediation Engineering
Applying security configurations remotely requires caution.
A configuration change that looks harmless can interrupt administrative access or break an application.
For SSH remediation, the implementation included a configuration validation step:
sshd -t
For firewall remediation, SSH access was allowed before enabling UFW.
For network remediation, persistent configuration was considered rather than relying only on temporary runtime changes.
These are important engineering principles:
- Back up configurations before modifying sensitive files.
- Validate configuration syntax before applying service changes.
- Use handlers where appropriate.
- Avoid unnecessary service restarts.
- Apply changes conditionally.
- Re-run the audit after remediation.
- Consider rollback and recovery procedures.
A particularly important Ansible concept here is idempotency.
Idempotency means that if I execute the same automation again, Ansible should recognize that the desired configuration is already present and avoid unnecessary changes.
This is important for repeatable infrastructure management.
I also learned that changed=0 does not necessarily mean every security requirement has passed. A server can remain unchanged while the audit identifies outstanding compliance gaps.
10. Troubleshooting and Engineering Lessons
Building this project involved more than writing task files.
Several issues required investigation.
Challenge 1: Incorrect SSH private-key path
Problem: Ansible could not connect to the managed servers because the configured private-key path was incorrect.
Investigation: I compared the inventory configuration with the actual key location and tested SSH connectivity.
Resolution: I corrected the key path and restricted its file permissions.
Lesson: Always verify the actual SSH key path and test manual SSH before troubleshooting Ansible itself.
Challenge 2: Firewall installed but inactive
Problem: The initial audit showed that UFW was installed but inactive.
Investigation: I checked firewall status and verified the OpenSSH application profile.
Resolution: I implemented remediation that allowed SSH before enabling the firewall.
Lesson: Remote firewall changes must be designed around administrator access and recovery.
Challenge 3: Effective SSH configuration
Problem: The effective SSH configuration did not initially match the intended security baseline.
Investigation: I inspected effective SSH settings rather than assuming that the main configuration file represented the active configuration.
Resolution: I prepared an Ansible-managed SSH configuration and validated the effective values after remediation.
Lesson: Configuration files and effective runtime configuration are not always the same.
Challenge 4: Network interface discovery
Problem: Global network settings alone were not sufficient for a complete network security assessment.
Investigation: I gathered Ansible facts to identify available network interfaces.
Resolution: I incorporated interface-aware inspection and persistent sysctl checks.
Lesson: Security auditing needs to consider the actual configuration of the system, including individual interfaces.
Challenge 5: Outstanding package updates
Problem: The audit identified pending security updates.
Investigation: I inspected package update information and distinguished security updates from other updates.
Resolution: The framework reported outstanding patches rather than automatically installing everything.
Lesson: Compliance automation should support operational decisions, not blindly make potentially disruptive changes.
11. Executing and Validating the Framework
I used several Ansible commands throughout the implementation.
Inspect inventory
ansible-inventory --graph
This displays the inventory hierarchy and managed host groups.
Test connectivity
ansible compliance_servers -m ping
This confirms Ansible connectivity and remote module execution.
Validate playbook syntax
ansible-playbook playbooks/compliance.yml --syntax-check
This checks the playbook for syntax and structural issues.
Execute the compliance audit
ansible-playbook playbooks/compliance.yml
Explicitly select audit mode
ansible-playbook playbooks/compliance.yml \
-e compliance_mode=audit
Execute remediation mode
ansible-playbook playbooks/compliance.yml \
-e compliance_mode=remediate
Remediation should only be executed against systems where you have reviewed the intended changes and have an appropriate recovery plan.
Historical audit execution
A later recorded audit execution produced the following results on both managed servers:
| Ansible result | Per server |
|---|---|
| OK | 52 |
| Changed | 0 |
| Failed | 0 |
| Skipped | 70 |
| Unreachable | 0 |
An earlier execution reported a different recap, including 65 skipped tasks. These were separate runs and should not be combined.
Understanding the results
-
ok: A task completed without requiring a change. -
changed: A task modified the system. -
failed: A task encountered an error. -
skipped: A task was not executed because of a condition or other execution rule. -
unreachable: Ansible could not establish the required connection to the host.
The absence of failed tasks means the automation completed without reported task failures.
It does not automatically mean that every compliance check passed.
That is why I also examined the actual compliance findings.
Screenshot 16 — Playbook execution
Capture the actual final audit execution showing both hosts, their task summaries and the recap.
12. What the Project Revealed
The historical audit results showed that seven of the eight categories passed their defined checks, while the system update category continued to identify pending packages.
| Category | Recorded result |
|---|---|
| SSH security | PASS |
| Firewall security | PASS |
| User security | PASS |
| File permissions | PASS |
| Legacy services | PASS |
| System updates | FINDINGS |
| Logging | PASS |
| Network security | PASS |
These results represent the project's recorded audit findings, not an independent security certification.
The update findings remained important:
- 14 total pending updates per server.
- 11 security updates per server.
The framework successfully identified these outstanding items instead of hiding them behind a successful Ansible execution summary.
For me, this was one of the most important outcomes of the project.
A useful compliance automation system must identify problems accurately, not simply report that its playbook executed successfully.
13. Reporting and Future Improvements
Structured compliance reporting is an important direction for this project.
A JSON report could make the audit findings easier to consume in external systems.
For example, a future report could contain:
- Hostname.
- Audit timestamp.
- Compliance category.
- Individual check.
- Expected configuration.
- Actual configuration.
- Pass or fail status.
- Remediation status.
A simplified example of the intended reporting structure might look like this:
{
"hostname": "compliance-server-01",
"compliance": {
"ssh": {
"status": "PASS"
},
"firewall": {
"status": "PASS"
},
"system_updates": {
"status": "FAIL",
"pending_updates": 14,
"security_updates": 11
}
}
}
This is an illustrative future report format, not a claim that this exact JSON file was generated by the current implementation.
Other improvements I would like to introduce include:
- Ansible Vault for managing sensitive variables.
- Ansible-lint and YAML linting.
- Molecule testing for reusable roles.
- CI/CD validation through GitHub Actions.
- Historical compliance reports.
- Centralized reporting and dashboards.
- Scheduled compliance audits.
- More comprehensive rollback and recovery procedures.
- Integration with AWS Systems Manager and other security tooling.
These improvements would help evolve the project into a more comprehensive infrastructure compliance automation solution.
14. Key Lessons Learned
This project reinforced several important engineering principles.
1. Automation starts with understanding the infrastructure.
Before writing automation, I needed to understand SSH connectivity, Linux configuration, firewall behaviour and network interfaces.
2. Ansible roles improve maintainability.
Separating audit and remediation made the project easier to understand and extend.
3. Security remediation must be controlled.
A security improvement should not accidentally lock administrators out of their own servers.
4. Effective configuration matters.
Inspecting the effective SSH configuration was more useful than simply reading a configuration file.
5. Runtime and persistent settings are different.
This became especially important during network security auditing.
6. A successful playbook is not the same as a compliant server.
The actual security findings matter more than the execution recap alone.
7. Verification is part of automation.
Applying a configuration change is only one part of the process. Re-running the audit establishes whether the intended result was achieved.
15. Conclusion
Building this Ansible Automated Compliance Enforcement framework gave me practical experience in applying infrastructure automation to Linux security.
I worked through SSH connectivity problems, configured an Ansible inventory, organized reusable roles, inspected eight security categories and implemented controlled remediation for important security configurations.
More importantly, I learned to distinguish between auditing, identifying compliance gaps, applying changes and verifying the resulting configuration.
The project is still open to improvement, particularly around structured reporting, automated testing and broader integration with cloud security tooling.
However, it has provided a practical foundation for understanding how Ansible can help standardize Linux security checks across multiple cloud servers.
My next goal is to continue improving the framework, strengthen its reporting capabilities and make it easier for other engineers to reproduce.
The main lesson: infrastructure automation should not just execute commands. It should provide consistency, visibility, controlled changes and verifiable results.
Project Information
Project: Ansible Automated Compliance Enforcement
Author: Jamiu Olatunji Bakre
Focus areas:
- Ansible automation
- Linux administration
- AWS EC2
- SSH security
- UFW firewall management
- Configuration management
- Security auditing
- Infrastructure remediation
- Network security
- YAML Project repository: [Insert your public GitHub repository URL]
Technical portfolio: https://jahmeeu-cloud.github.io/portfolio-project/
Developer profile: https://dev.to/jamiu_cloud
If you are working on infrastructure automation, Linux security or Ansible, I would be interested in hearing about your approach and the challenges you have encountered.
Thank you for reading!








Top comments (0)