【问题标题】:Ansible jinja filter out elements with attribute not equal to something OR attribute missingAnsible jinja 过滤掉属性不等于某物或缺少属性的元素
【发布时间】:2019-01-07 20:32:13
【问题描述】:

我有一些这样的任务:

- ec2_asg_facts:
    region: us-west-2
    tags:
      Name: my-asg
  register: asg
- set_fact:
    current_lc: "{{ asg.results | map(attribute='launch_configuration_name') | first }}"
    instances: "{{ asg.results | map(attribute='instances') | flatten | list }}"
- set_fact:
    instances_to_rotate: "{{ instances | rejectattr('launch_configuration_name', 'equalto', current_lc) | list }}"

问题在于,如果实例的启动配置被删除(当我将 ASG 的启动配置更新为新配置但尚未终止旧实例时,通常会出现这种情况)@987654322缺少@ 属性,因此第三个任务失败并显示消息'dict object' has no attribute 'launch_configuration_name'。如何过滤 instances 列表中 launch_configuration_name 属性不等于 current_lc 或缺少 launch_configuration_name 属性的项目?

【问题讨论】:

    标签: amazon-ec2 ansible jinja2


    【解决方案1】:

    答案是分两次过滤;先过滤掉属性未定义的item,再过滤掉属性不等于current_lc的item。

    - set_fact:
        instances_to_rotate: "{{ instances | rejectattr('launch_configuration_name', 'undefined') | rejectattr('launch_configuration_name', 'equalto', current_lc) | list }}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 2012-07-05
      相关资源
      最近更新 更多