Amazon ec2 当有条件时应用于具有Ansible的嵌套列表,而不仅仅是顶级列表

Amazon ec2 当有条件时应用于具有Ansible的嵌套列表,而不仅仅是顶级列表,amazon-ec2,scripting,ansible,Amazon Ec2,Scripting,Ansible,所以我有一个Ansible+AWS EC2的致命问题 如果磁盘类型为“io1”,则应指定“iops”,否则不应指定。 如果我想使磁盘类型可配置,我必须在何时应用,但它不起作用: - name: Provision AWS Instances ec2: instance_type: "{{ ec2_instance_type }}" image: "{{ ec2_ami_id }}" ... volumes: - device_name: "{{ e

所以我有一个Ansible+AWS EC2的致命问题

如果磁盘类型为“io1”,则应指定“iops”,否则不应指定。 如果我想使磁盘类型可配置,我必须在何时应用,但它不起作用:

- name: Provision AWS Instances
  ec2:
    instance_type: "{{ ec2_instance_type }}"
    image: "{{ ec2_ami_id }}"
    ...
    volumes:
      - device_name: "{{ ec2_root_disk_name }}"
        volume_type: "{{ ec2_root_disk_type }}"
        volume_size: "{{ ec2_root_disk_size }}"
        delete_on_termination: "{{ ec2_root_disk_delete_on_termination }}"
        iops: "{{ ec2_root_disk_iops }}"
        when: ec2_root_disk_type == 'io1'
      - device_name: "{{ ec2_root_disk_name }}"
        volume_type: "{{ ec2_root_disk_type }}"
        volume_size: "{{ ec2_root_disk_size }}"
        delete_on_termination: "{{ ec2_root_disk_delete_on_termination }}"
        when: ec2_root_disk_type != 'io1'
但是,不处理此嵌套的“when”条件,并添加两次驱动器:

"volumes": [
    {
        "delete_on_termination": "true", 
        "device_name": "/dev/sda1", 
        "iops": "100", 
        "volume_size": "30", 
        "volume_type": "gp2", 
        "when": "ec2_root_disk_type == 'io1'"
    }, 
    {
        "delete_on_termination": "true", 
        "device_name": "/dev/sda1", 
        "volume_size": "30", 
        "volume_type": "gp2", 
        "when": "ec2_root_disk_type != 'io1'"
    }, 
]
我可以在整个命令中添加'when',但我可能有多达5个驱动器,所以这意味着复制它超过32次

是否可以在嵌套列表上设置Ansible进程“when”

是否可以将此列表组合到一个变量中,请在此命令中引用它?Ansible的文档不涉及非字符串、结构化变量。也许我可以应用收集过滤


这里建议采取什么行动?我想每个人在尝试创建可配置类型的卷时都会遇到这个问题。

您可以使用内联条件,例如:

- device_name: "{{ ec2_root_disk_name }}"
  volume_type: "{{ ec2_root_disk_type }}"
  volume_size: "{{ ec2_root_disk_size }}"
  delete_on_termination: "{{ ec2_root_disk_delete_on_termination }}"
  iops: "{{ ec2_root_disk_iops if ec2_root_disk_type == 'io1' }}"

在资源清册中指定卷,而不是在播放中指定卷

例如,
group\u vars/with\u iops.yml
文件:

volumes:
  - device_name: "{{ ec2_root_disk_name }}"
    volume_type: "{{ ec2_root_disk_type }}"
    volume_size: "{{ ec2_root_disk_size }}"
    delete_on_termination: "{{ ec2_root_disk_delete_on_termination }}"
    iops: "{{ ec2_root_disk_iops }}"
volumes:
  - device_name: "{{ ec2_root_disk_name }}"
    volume_type: "{{ ec2_root_disk_type }}"
    volume_size: "{{ ec2_root_disk_size }}"
    delete_on_termination: "{{ ec2_root_disk_delete_on_termination }}"
group\u vars/without\u iops.yml
文件:

volumes:
  - device_name: "{{ ec2_root_disk_name }}"
    volume_type: "{{ ec2_root_disk_type }}"
    volume_size: "{{ ec2_root_disk_size }}"
    delete_on_termination: "{{ ec2_root_disk_delete_on_termination }}"
    iops: "{{ ec2_root_disk_iops }}"
volumes:
  - device_name: "{{ ec2_root_disk_name }}"
    volume_type: "{{ ec2_root_disk_type }}"
    volume_size: "{{ ec2_root_disk_size }}"
    delete_on_termination: "{{ ec2_root_disk_delete_on_termination }}"
该剧:

- name: Provision AWS Instances
  ec2:
    instance_type: "{{ ec2_instance_type }}"
    image: "{{ ec2_ami_id }}"
    ...
    volumes: "{{ volumes }}"

实际上,我已经在playbook的
library/
中添加了一个空的自定义python模块,并在
action\u plugins/
中添加了一个相应的动作插件。在这个插件中,我通过生成所需的卷列表来处理输入参数,并使用改进的参数调用
ec2

这很好,但是我可以让整个设备(列表项)成为可选的吗?我想要可选的驱动器。我一定会检查这个。如果我执行
ec2\u root\u disk\u iops,如果ec2\u root\u disk\u type==“io1”else”“
,则结果JSON中仍然存在属性,AWS调用仍然失败。如果我执行了
else 0
,仍然会失败。如果类型为!='AWS根本不需要此属性这看起来很有趣,但现在我有两个配置文件。我无法从yml获取变量(可以吗?),因此我基本上需要进行两次配置。是的,您有两个配置文件:一个用于需要iops的主机组,另一个用于不需要iops的主机组。是的,这些组中的主机可以访问这些yaml文件中的变量。我想我可以在一个.yml文件中列出所有磁盘如何引用jinja2中的给定卷?例如,我能做{{volumes[1].volume\u size}}等吗?仅仅因为你能做某件事并不是一个好主意。有些主机需要iops,有些不需要iops,对吗?不,我想通过group_vars使其完全可配置