首页 > 解决方案 > 如何在字典列表中使用 set_fact?

问题描述

我有一个 ansible 脚本,它使用 ec2_instance_info 获取有关 EC2 实例的信息。它以字典列表的形式出现,如果其中不存在,我正在尝试为该列表中的每个项目添加一个字典值。如何让 set_fact 对每个字典执行操作?

我正在尝试完成的示例:

  - name: Insert value if nonexistent
    set_fact:
      "{{item}}": "{{[item]|combine(add_val)}}"
    when: item.public_ip_address is not defined
    with_items: "{{info}}"
    register: tmp_info

这导致:

... virtualization_type': 'hvm', 'cpu_options': {'core_count': 1, 'threads_per_core': 1}, 'capacity_reservation_specification': {'cap
acity_reservation_preference': 'open'}, 'hibernation_options': {'configured': False}, 'metadata_options': {'state': 'applied', 'http_tokens': 'optional', 'http_put_response_hop_limit': 1, 'http_endpoint': 'enabl
ed'}, 'enclave_options': {'enabled': False}}' is not valid. Variables must start with a letter or underscore character, and contain only letters, numbers and underscores."}

据我所知,它正在尝试获取 dict 的值并将其设置为名称。

有什么办法可以重写它以引用我要修改的有问题的字典,然后在同一个字典列表中修改它?

标签: amazon-web-servicesamazon-ec2ansibleyamljinja2

解决方案


推荐阅读