This paper examines Infrastructure as Code (IaC) with Ansible to automate and enhance security hardening in Linux environments. As IT infrastructures grow more complex, manual security configurations become error-prone, inefficient, and inconsistent. IaC addresses these issues by allowing organizations to define and deploy infrastructure configurations as code, ensuring a consistent security baseline. Focusing on key settings for Firewalld, SELinux, and SSH, the study demonstrates how Ansible enforces these configurations in a scalable, repeatable, and resilient manner. Results show that using Ansible for security hardening reduces deployment times, minimizes manual errors, and ensures uniform security standards across diverse systems. This research offers a practical foundation for organizations seeking to improve their cybersecurity posture, emphasizing IaC's transformative potential in achieving secure, efficient, and adaptable infrastructure management.