Ansible 模块 amazon.aws 和 aws_service_ip_ranges 查找插件
Ansible module amazon.aws and the aws_service_ip_ranges lookup plugin
在测试云端 WAF 实例时,我需要 Cloudfront 为我们的实例使用的 IP 列表或 IP 范围,以便我们可以将它们列入防火墙的白名单。看起来 Ansible 模块集合 amazon.aws
和查找插件 aws_service_ip_ranges
可以做到这一点,但是我非常坚持在 yaml 中获得正确的语法。
我已经阅读了 docs here, here, and here,但我仍然不太明白如何成功地查找所使用的 IP。我需要一个具体的例子来说明如何编写 yaml 来获取这些 IP。使用文档中给出的示例并没有奏效,同样,我不清楚这些示例的语法。
这是我现在所拥有的 returns vars:
行的语法错误。
---
- hosts: <hostname>
remote_user: ansible
collections: amazon.aws
tasks:
- name: Get IP Range
plugin: aws_service_ip_ranges
vars:
cloudfront_ranges: "{{ lookup('aws_service_ip_ranges', region 'us-east-1', service 'CLOUDFRONT', wantlist True) }}"
- name: "use list return option and iterate as a loop"
debug: msg="{% for cidr in cloudfront_ranges %}{{ cidr }} {% endfor %}"
答案是更简单,少做剧本。我将 collections
和 plugins
项合二为一。将 vars
移动到与 collections
.
相同的选项卡级别
---
- hosts: <hostname>
collections:
- amazon.aws.aws_service_ip_ranges
remote_user: ansible
vars:
cloudfront_ranges: "{{ lookup('aws_service_ip_ranges', service='CLOUDFRONT', wantlist=True) }}"
tasks:
- name: use list return option and iterate as a loop
debug: msg="{% for cidr in cloudfront_ranges %}{{ cidr }} {% endfor %}"
在测试云端 WAF 实例时,我需要 Cloudfront 为我们的实例使用的 IP 列表或 IP 范围,以便我们可以将它们列入防火墙的白名单。看起来 Ansible 模块集合 amazon.aws
和查找插件 aws_service_ip_ranges
可以做到这一点,但是我非常坚持在 yaml 中获得正确的语法。
我已经阅读了 docs here, here, and here,但我仍然不太明白如何成功地查找所使用的 IP。我需要一个具体的例子来说明如何编写 yaml 来获取这些 IP。使用文档中给出的示例并没有奏效,同样,我不清楚这些示例的语法。
这是我现在所拥有的 returns vars:
行的语法错误。
---
- hosts: <hostname>
remote_user: ansible
collections: amazon.aws
tasks:
- name: Get IP Range
plugin: aws_service_ip_ranges
vars:
cloudfront_ranges: "{{ lookup('aws_service_ip_ranges', region 'us-east-1', service 'CLOUDFRONT', wantlist True) }}"
- name: "use list return option and iterate as a loop"
debug: msg="{% for cidr in cloudfront_ranges %}{{ cidr }} {% endfor %}"
答案是更简单,少做剧本。我将 collections
和 plugins
项合二为一。将 vars
移动到与 collections
.
---
- hosts: <hostname>
collections:
- amazon.aws.aws_service_ip_ranges
remote_user: ansible
vars:
cloudfront_ranges: "{{ lookup('aws_service_ip_ranges', service='CLOUDFRONT', wantlist=True) }}"
tasks:
- name: use list return option and iterate as a loop
debug: msg="{% for cidr in cloudfront_ranges %}{{ cidr }} {% endfor %}"