【问题标题】:Using variable for tag name in Ansible AWS modules在 Ansible AWS 模块中使用变量作为标签名称
【发布时间】:2022-12-01 04:45:17
【问题描述】:

ec2_snapshot 模块允许我同时创建卷和标签的快照。在为标签使用固定名称时,这是直截了当的。但是如何从变量中设置标签名称本身呢?

示例任务:

- name: AWS EBS Disks Snapshot For Volumes
  ec2_snapshot:
  aws_access_key: "{{ aws_access_key_id }}"
  aws_secret_key: "{{ aws_secret_key_id }}"
  security_token: "{{ aws_security_token }}"
  volume_id: "{{ item.id }}"
  region:  "{{ aws_region }}"
  snapshot_tags: 
    Name: "{{ timestamp.stdout }}"
    "{{ tagname_variable }}": "{{ tagvalue_variable }}"
    type: "{{ item.type }}"
  description: "{{ timestamp.stdout }}_snapshot"
  with_items:
    - "{{ volumeinputs }}"

tagname_variable 按字面意思创建为标记名称,而不是变量的值。
我怎样才能使这项工作?

【问题讨论】:

    标签: amazon-web-services ansible


    【解决方案1】:

    您将需要动态创建字典的那部分,例如,使用 combine,因为 YAML 字典键通常不会由 Ansible 模板化。

    这可以在任务本身的 vars 部分中完成:

    - name: AWS EBS Disks Snapshot For Volumes
      ec2_snapshot:
      aws_access_key: "{{ aws_access_key_id }}"
      aws_secret_key: "{{ aws_secret_key_id }}"
      security_token: "{{ aws_security_token }}"
      volume_id: "{{ item.id }}"
      region:  "{{ aws_region }}"
      snapshot_tags: "{{ _snapshot_tags }}"
      description: "{{ timestamp.stdout }}_snapshot"
      loop: "{{ volumeinputs }}"
      vars:
        _snapshot_tags_static:
          Name: "{{ timestamp.stdout }}"
          type: "{{ item.type }}"
        _snapshot_tags: >-
          {{
            _snapshot_tags_static
            | combine({tagname_variable: tagvalue_variable})
          }}
    

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 2011-02-08
      • 2023-03-18
      • 2016-11-21
      • 1970-01-01
      • 2015-06-01
      • 2022-08-20
      • 2022-11-21
      • 2020-04-07
      相关资源
      最近更新 更多