For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/ansible-aws-db-security-group-open-to-large-scope.md.
A documentation index is available at /llms.txt.
Security group rules that use CIDR blocks containing 256 or more IP addresses broaden the attack surface and make unauthorized access or lateral movement easier.
For Ansible EC2 security groups (modules amazon.aws.ec2_group and ec2_group), ensure each rule’s cidr_ip is a CIDR with a prefix length greater than 24 (for example /25–/32) so the subnet contains fewer than 256 addresses. This rule flags any task where rules[].cidr_ip has a prefix length of 24 or less (for example, 10.0.0.0/24, 10.0.0.0/16, or 0.0.0.0/0). If broader access is required, prefer tighter subnetting, explicit host IPs, or security-group references instead of large CIDR ranges.
Secure Ansible example with a narrow CIDR (/32 single host):
- name:example ec2 group2ec2_group:name:example1description:an example EC2 groupvpc_id:12345region:eu-west-1aaws_secret_key:SECRETaws_access_key:ACCESSrules:- proto:tcpfrom_port:80to_port:80cidr_ip:10.1.1.1/32
Non-Compliant Code Examples
- name:create minimal aurora instance in default VPC and default subnet groupamazon.aws.rds_instance:engine:auroradb_instance_identifier:ansible-test-aurora-db-instanceinstance_type:db.t2.smallpassword:"{{ password }}"username:"{{ username }}"cluster_id:ansible-test-clusterdb_security_groups:["example"]- name:example ec2 groupec2_group:name:exampledescription:an example EC2 groupvpc_id:12345region:eu-west-1aaws_secret_key:SECRETaws_access_key:ACCESSrules:- proto:tcpfrom_port:80to_port:80cidr_ip:0.0.0.0/0- proto:tcpfrom_port:22to_port:22cidr_ip:10.0.0.0/8- proto:tcpfrom_port:443to_port:443group_id:amazon-elb/sg-87654321/amazon-elb-sg- proto:tcpfrom_port:3306to_port:3306group_id:123412341234/sg-87654321/exact-name-of-sg- proto:udpfrom_port:10050to_port:10050cidr_ip:10.0.0.0/8- proto:udpfrom_port:10051to_port:10051group_id:sg-12345678- proto:icmpfrom_port:8# icmp type, -1 = any typeto_port:-1# icmp subtype, -1 = any subtypecidr_ip:192.168.1.0/24- proto:all# the containing group name may be specified heregroup_name:example
1
2
rulesets:- Ansible / AWS # Rules to enforce / AWS.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.