arXiv:2608.24962cs.SEAug 2026

Manuscript Offprint

arXiv:2608.24962

Evaluating and Preventing Security Smells in AI-Generated Ansible Code

Pandu Ranga Reddy Konala, Vimal Kumar, David Bainbridge, Junaid Haseeb

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Pandu Ranga Reddy Konala, Vimal Kumar, David Bainbridge, and Junaid Haseeb

School of Computing and Mathematical Sciences,

University of Waikato,

Hamilton, New Zealand 3240

{pkonala, vkumar, davidb, jhaseeb}@waikato.ac.nz

Abstract. AI coding assistants generate Infrastructure as Code, yet no work has examined whether this code meets security requirements. This matters because security smells in infrastructure code propagate to deployed systems, producing infrastructure that is insecure and untrustworthy. We evaluate 16 AI models generating Ansible roles for Apache Tomcat v10 and MongoDB v7, analysing 278 ansible roles against CIS benchmarks. Without security guidance, all 16 AI models produced code containing security smells, resulting in vulnerable infrastructure that fails compliance verification and underperforms code written by human developers. We introduce an approach integrating Ansible best practices and CIS benchmarks into prompts through an extended CO-STAR framework, enabling security smell prevention during synthesis rather than detection after deployment. When this approach is applied, 4 out of 16 models generate compliant code, with the leading model achieving 95%–100% CIS compliance, a fourfold improvement over humans at 23%–43%, with overall code quality improving by 19%–49%. The remaining 12 models fail not because they cannot generate code but because they cannot follow instructions with multiple constraints. For capable models, the approach requires no retraining and can be adopted through system prompts.

1 Introduction

Infrastructure as Code (IaC) automates system configuration through specifications that replace manual provisioning with version-controlled deployments [17]. Configuration management tools including Ansible, Terraform, and Puppet translate these specifications into deployed infrastructure. This automation introduces security risks, however. Security smells in infrastructure code, including hard-coded credentials, excessive permissions, and missing access controls, propagate directly to deployed systems, with the resulting infrastructure insecure and untrustworthy. Such infrastructure exposes organisations to data breaches, service disruptions, and regulatory penalties. In regulated industries, production deploy-

P. R. R. Konala et al.

ments must satisfy compliance frameworks such as CIS Benchmarks 1^{1} and DISA STIGs. 2^{2} Code containing security smells fails compliance verification and cannot receive deployment authorisation.

AI coding assistants compound these security risks. Tools such as Cursor, 3^{3} Bolt 4^{4} and others generate IaC from natural language descriptions using foundational AI models such as OpenAI's GPT-4o and Anthropic's Claude Opus 4 as their knowledge bases. Studies report that 45%–62% of AI-generated code contains vulnerabilities [2,8]. It is also reported that IaC misconfigurations account for 68% of cloud security incidents [3]. Infrastructure misconfigurations can pose a greater security threat than application vulnerabilities because they become attack vectors immediately upon deployment.

Existing approaches to IaC security rely on detection rather than prevention, with static analysis tools identifying security smells after code enters repositories [20]. This detection paradigm is inadequate for AI-generated code because each refinement cycle incurs time and computational costs while requiring security expertise for remediation. More fundamentally, detection cannot prevent security smells from entering codebases. Research on IaC compliance remains limited to frameworks without validation [5,10], while studies on AI code security focus on application code rather than infrastructure [12,22]. No known work has examined the security properties of AI-generated infrastructure code or whether security requirements can be embedded into the code generation process.

We investigate two questions: (RQ1) What is the default security posture of AI-generated infrastructure code and does it provide a viable foundation for achieving regulatory compliance? (RQ2) Can security-compliance requirements be embedded into the generation process itself, eliminating the need for iterative post-generation remediation? We evaluate 16 foundational AI models generating Ansible code for Apache Tomcat and MongoDB deployments, assessing 278 roles against CIS benchmarks through both pre-deployment code quality analysis and post-deployment compliance verification. Our contributions are:

– We evaluated 16 foundational AI models generating Ansible code, quantifying default security behaviour and standards awareness across open-source and closed-source implementations.

– We extended the CO-STAR prompting framework to integrate Ansible best practices 5^{5} and CIS benchmarks as generation constraints, enabling security smell prevention during synthesis for capable models without AI model retraining.

1^{1} Center for Internet Security, “CIS Critical Security Controls, Version 8,” 2024, https://www.cisecurity.org/controls/cis-controls-list/.

2^{2} Defense Information Systems Agency, “Security Technical Implementation Guide (STIG),” 2025, https://public.cyber.mil/stigs/downloads/.

3^{3} Cursor AI Inc., “Cursor,” 2024, https://cursor.sh.

4^{4} StackBlitz Labs, “Bolt: AI-Powered Full-Stack Web Development in the Browser,” 2025, https://github.com/stackblitz-labs/bolt.diy.

5^{5} Ansible Project, “Ansible Best Practices – Ansible Documentation,” 2019, https://docs.ansible.com/ansible/2.8/user_guide/playbooks_best_practices.html.