根据AWS标签分配ansible变量

2024-03-13

我正在尝试找出一种根据 AWS 中的标签在 Ansible 中分配变量的方法。我正在尝试ec2_remote_tags但它返回的信息比我需要的多得多。似乎应该有一种更简单的方法来做到这一点,但我只是没有想到。

例如,如果我有一个名为function这创造了tag_function_api使用动态库存进行分组,我想分配一个变量function到值api。关于有效方法来做到这一点有什么想法吗?


我已经成功地制作了带有值列表的标签字典:

- hosts: localhost
  tasks:
    - ec2_remote_facts:
        region: eu-west-1
      register: ec2_facts

    # get all possible tag names
    - set_fact: tags="{{ item.keys() }}"
      with_items: "{{ ec2_facts.instances | map(attribute='tags') | list }}"
      register: tmp_tags

    # get flattened list of tags (for some reason lookup() returns string, so we use with_)
    - assert: that=true
      with_flattened: "{{ tmp_tags.results | map(attribute='ansible_facts.tags') | list }}"
      register: tmp_tags

    # get unique tag names
    - set_fact: tags="{{ tmp_tags.results | map(attribute='item') | list | unique }}"

    - set_fact: my_tags="{{ {} }}"

    # get all possible values for a given tag
    - set_fact:
        my_tags: "{{ my_tags | combine( {''+item: ec2_facts.instances | map(attribute='tags.'+item) | select('defined') | list | unique}) }}"
      with_items: "{{ tags }}"

    - debug: var=my_tags
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据AWS标签分配ansible变量 的相关文章

随机推荐