community.aws.wafv2_web_acl 模块 – 创建和删除 WAF Web ACL

注意

此模块是 community.aws 集合 (版本 9.0.0) 的一部分。

如果您正在使用 ansible 包,则可能已安装此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用: ansible-galaxy collection install community.aws。您需要其他要求才能使用此模块,有关详细信息,请参见 要求

要在 playbook 中使用它,请指定: community.aws.wafv2_web_acl

community.aws 1.5.0 中的新增功能

概要

要求

执行此模块的主机需要以下要求。

  • python >= 3.6

  • boto3 >= 1.28.0

  • botocore >= 1.31.0

参数

参数

注释

access_key

别名:aws_access_key_id、aws_access_key、ec2_access_key

字符串

AWS 访问密钥 ID。

有关访问令牌的更多信息,请参见 AWS 文档 https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys

还可以按优先级递减顺序使用 AWS_ACCESS_KEY_IDAWS_ACCESS_KEYEC2_ACCESS_KEY 环境变量。

aws_access_keyprofile 选项是互斥的。

为了与 AWS botocore SDK 保持一致,aws_access_key_id 别名是在 5.1.0 版本中添加的。

ec2_access_key 别名已弃用,将在 2024-12-01 之后的一个版本中移除。

EC2_ACCESS_KEY 环境变量的支持已弃用,将在 2024-12-01 之后的一个版本中移除。

aws_ca_bundle

路径

验证 SSL 证书时使用的 CA 证书包的位置。

还可以使用 AWS_CA_BUNDLE 环境变量。

aws_config

字典

用于修改 botocore 配置的字典。

可以在 AWS 文档中找到参数 https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore.config.Config

cloudwatch_metrics

布尔值

为 wafv2 web acl 启用 cloudwatch 指标。

选项

  • false

  • true ← (默认)

custom_response_bodies

字典

community.aws 3.1.0 中新增

自定义响应键和内容体的映射。在此处定义响应体,并通过提供

主体字典元素的键在规则中引用它们。

每个元素必须具有唯一的字典键,并且在字典中具有content_typecontent两个键。

debug_botocore_endpoint_logs

布尔值

使用 botocore.endpoint 记录器来解析在任务期间进行的唯一(而不是总计)"resource:action" API 调用,将集合输出到任务结果中的 resource_actions 键。使用 aws_resource_action 回调将输出到 playbook 期间进行的总列表。

还可以使用 ANSIBLE_DEBUG_BOTOCORE_LOGS 环境变量。

选项

  • false ← (默认)

  • true

default_action

字符串

wafv2 web acl 的默认操作。

选项

  • “阻止”

  • “允许”

描述

字符串

WAFv2 Web ACL 的描述。

endpoint_url

别名:ec2_url, aws_endpoint_url, s3_url

字符串

连接到AWS端点的URL,而不是默认的AWS端点。虽然这可以用于连接到其他兼容AWS的服务,但amazon.aws和community.aws集合只针对AWS进行了测试。

也可以使用AWS_URLEC2_URL环境变量,优先级依次递减。

ec2_urls3_url别名已弃用,将在2024-12-01之后的一个版本中移除。

EC2_URL环境变量的支持已弃用,将在2024-12-01之后的一个版本中移除。

metric_name

字符串

CloudWatch 指标的名称。

如果未指定且启用了cloudwatch_metrics,则将使用Web ACL本身的名称。

name

字符串 / 必需

Web ACL 的名称。

profile

别名:aws_profile

字符串

用于身份验证的命名AWS配置文件。

有关命名配置文件的更多信息,请参阅AWS文档 https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html

也可以使用AWS_PROFILE环境变量。

profile选项与aws_access_keyaws_secret_keysecurity_token选项互斥。

purge_rules

布尔值

设置为no时,保留现有的负载均衡器规则。将修改和添加,但不会删除。

选项

  • false

  • true ← (默认)

purge_tags

布尔值

如果purge_tags=truetags已设置,则将从资源中清除现有标签,以完全匹配tags参数定义的内容。

如果未设置tags参数,则即使purge_tags=True,也不会修改标签。

aws:开头的标签键由Amazon保留,不能修改。因此,在purge_tags参数中将忽略它们。有关更多信息,请参阅Amazon文档 https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html#tag-conventions

选项

  • false

  • true ← (默认)

region

别名:aws_region, ec2_region

字符串

要使用的AWS区域。

对于IAM、Route53和CloudFront等全局服务,将忽略region

也可以使用AWS_REGIONEC2_REGION环境变量。

有关更多信息,请参阅Amazon AWS文档 http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region

ec2_region别名已弃用,将在2024-12-01之后的一个版本中移除。

EC2_REGION环境变量的支持已弃用,将在2024-12-01之后的一个版本中移除。

rules

列表 / 元素=字典

用于标识要允许、阻止或计数的Web请求的规则语句。

有关托管规则的列表,请参阅 https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-list.html

action

字典

规则是被阻止、允许还是计数。

name

字符串

WAFv2规则的名称

priority

整数

规则优先级

statement

字典

规则配置。

visibility_config

字典

单个WAFv2规则的可见性。

sampled_requests

布尔值

是否存储Web请求样本,true或false。

选项

  • false ← (默认)

  • true

scope

字符串 / 必需

Web ACL 的地理范围。

选项

  • “CLOUDFRONT”

  • “REGIONAL”

secret_key

别名:aws_secret_access_key, aws_secret_key, ec2_secret_key

字符串

AWS 密钥访问密钥。

有关访问令牌的更多信息,请参见 AWS 文档 https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys

也可以使用AWS_SECRET_ACCESS_KEYAWS_SECRET_KEYEC2_SECRET_KEY环境变量,优先级依次递减。

secret_keyprofile选项互斥。

为了与AWS botocore SDK保持一致,在5.1.0版本中添加了aws_secret_access_key别名。

ec2_secret_key别名已弃用,将在2024-12-01之后的一个版本中移除。

EC2_SECRET_KEY环境变量的支持已弃用,将在2024-12-01之后的一个版本中移除。

session_token

别名:aws_session_token, security_token, aws_security_token, access_token

字符串

与临时凭据一起使用的AWS STS会话令牌。

有关访问令牌的更多信息,请参见 AWS 文档 https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys

也可以使用AWS_SESSION_TOKENAWS_SECURITY_TOKENEC2_SECURITY_TOKEN环境变量,优先级依次递减。

security_tokenprofile选项互斥。

在3.2.0版本中添加了aws_session_tokensession_token别名,在6.0.0版本中将参数名称从security_token重命名为session_token

security_tokenaws_security_tokenaccess_token别名已弃用,将在2024-12-01之后的一个版本中移除。

EC2_SECRET_KEYAWS_SECURITY_TOKEN环境变量的支持已弃用,将在2024-12-01之后的一个版本中移除。

state

字符串 / 必需

规则是否存在。

选项

  • “present”

  • “absent”

tags

别名:resource_tags

字典

表示要应用于资源的标签的字典。

如果未设置tags参数,则不会修改标签。

validate_certs

布尔值

设置为false时,将不会验证与AWS API通信的SSL证书。

强烈建议不要设置validate_certs=false,可以考虑改用aws_ca_bundle

选项

  • false

  • true ← (默认)

备注

注意

  • 在4.0.0版本中添加了对purge_tags参数的支持。

  • 注意:对于模块,环境变量和配置文件是从Ansible的“主机”上下文读取的,而不是“控制器”上下文。因此,可能需要将文件显式复制到“主机”。对于查找和连接插件,环境变量和配置文件是从Ansible的“控制器”上下文读取的,而不是“主机”上下文。

  • Ansible使用的AWS SDK (boto3)也可能从其在Ansible“主机”上下文中的配置文件(通常为~/.aws/credentials)读取凭据和其他设置的默认值,例如区域。有关更多信息,请参阅 https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html

示例

- name: Create test web acl
  community.aws.wafv2_web_acl:
    name: test05
    description: hallo eins
    scope: REGIONAL
    default_action: Allow
    sampled_requests: false
    cloudwatch_metrics: true
    metric_name: test05-acl-metric
    rules:
      - name: zwei
        priority: 0
        action:
          block: {}
        visibility_config:
          sampled_requests_enabled: true
          cloud_watch_metrics_enabled: true
          metric_name: ddos
        statement:
          xss_match_statement:
            field_to_match:
              body: {}
            text_transformations:
              - type: NONE
                priority: 0
      - name: admin_protect
        priority: 1
        override_action:
          none: {}
        visibility_config:
          sampled_requests_enabled: true
          cloud_watch_metrics_enabled: true
          metric_name: fsd
        statement:
          managed_rule_group_statement:
            vendor_name: AWS
            name: AWSManagedRulesAdminProtectionRuleSet

      # AWS Managed Bad Input Rule Set
      # but allow PROPFIND_METHOD used e.g. by webdav
      - name: bad_input_protect_whitelist_webdav
        priority: 2
        override_action:
          none: {}
        visibility_config:
          sampled_requests_enabled: true
          cloud_watch_metrics_enabled: true
          metric_name: bad_input_protect
        statement:
          managed_rule_group_statement:
            vendor_name: AWS
            name: AWSManagedRulesKnownBadInputsRuleSet
            excluded_rules:
              - name: PROPFIND_METHOD

      # Rate Limit example. 1500 req/5min
      # counted for two domains via or_statement. login.mydomain.tld and api.mydomain.tld
      - name: rate_limit_example
        priority: 3
        action:
          block: {}
        visibility_config:
          sampled_requests_enabled: true
          cloud_watch_metrics_enabled: true
          metric_name: mydomain-ratelimit
        statement:
          rate_based_statement:
            limit: 1500
            aggregate_key_type: IP
            scope_down_statement:
              or_statement:
                statements:
                  - byte_match_statement:
                      search_string: login.mydomain.tld
                      positional_constraint: CONTAINS
                      field_to_match:
                        single_header:
                          name: host
                      text_transformations:
                        - type: LOWERCASE
                          priority: 0
                  - byte_match_dtatement:
                      search_string: api.mydomain.tld
                      positional_constraint: CONTAINS
                      field_to_match:
                        single_header:
                          name: host
                      text_transformations:
                        - type: LOWERCASE
                          priority: 0
    purge_rules: true
    tags:
      A: B
      C: D
    state: present

- name: Create IP filtering web ACL
  community.aws.wafv2_web_acl:
    name: ip-filtering-traffic
    description: ACL that filters web traffic based on rate limits and whitelists some IPs
    scope: REGIONAL
    default_action: Allow
    sampled_requests: true
    cloudwatch_metrics: true
    metric_name: ip-filtering-traffic
    rules:
      - name: whitelist-own-IPs
        priority: 0
        action:
          allow: {}
        statement:
          ip_set_reference_statement:
            arn: 'arn:aws:wafv2:us-east-1:123456789012:regional/ipset/own-public-ips/1c4bdfc4-0f77-3b23-5222-123123123'
        visibility_config:
          sampled_requests_enabled: true
          cloud_watch_metrics_enabled: true
          metric_name: waf-acl-rule-whitelist-own-IPs
      - name: rate-limit-per-IP
        priority: 1
        action:
          block:
            custom_response:
              response_code: 429
              custom_response_body_key: too_many_requests
        statement:
          rate_based_statement:
            limit: 5000
            aggregate_key_type: IP
        visibility_config:
          sampled_requests_enabled: true
          cloud_watch_metrics_enabled: true
          metric_name: waf-acl-rule-rate-limit-per-IP
        purge_rules: true
    custom_response_bodies:
      too_many_requests:
        content_type: APPLICATION_JSON
        content: '{ message: "Your request has been blocked due to too many HTTP requests coming from your IP" }'
    region: us-east-1
    state: present

返回值

常见的返回值已在 此处 记录,以下是此模块特有的字段

描述

arn

字符串

Web ACL ARN

返回:只要Web ACL存在,就会始终返回。

示例:"arn:aws:wafv2:eu-central-1:123456789012:regional/webacl/test05/318c1ab9-fa74-4b3b-a974-f92e25106f61"

capacity

整数

Web ACL 的当前容量

返回:只要Web ACL存在,就会始终返回。

示例:140

custom_response_bodies

字典

要在规则中使用的自定义响应正文配置

返回:只要Web ACL存在,就会始终返回。

示例:{"too_many_requests": {"content": "{ message: \"Your request has been blocked due to too many HTTP requests coming from your IP\" }", "content_type": "APPLICATION_JSON"}}

default_action

字典

ACL 的默认操作

返回:只要Web ACL存在,就会始终返回。

示例:{"allow": {}}

描述

字符串

Web ACL 的描述

返回:只要Web ACL存在,就会始终返回。

示例:"Some web acl description"

name

字符串

Web ACL 名称

返回:只要Web ACL存在,就会始终返回。

示例:"test02"

rules

列表 / 元素=字符串

Web ACL 的当前规则

返回:只要Web ACL存在,就会始终返回。

示例:[{"name": "admin_protect", "override_action": {"none": {}}, "priority": 1, "statement": {"managed_rule_group_statement": {"name": "AWSManagedRulesAdminProtectionRuleSet", "vendor_name": "AWS"}}, "visibility_config": {"cloud_watch_metrics_enabled": true, "metric_name": "admin_protect", "sampled_requests_enabled": true}}]

visibility_config

字典

Web ACL 的可见性配置

返回:只要Web ACL存在,就会始终返回。

示例: {"cloud_watch_metrics_enabled": true, "metric_name": "blub", "sampled_requests_enabled": false}

作者

  • Markus Bergholz (@markuman)