Ansible 连接两个变量

Ansible 连接两个变量

我有两个变量定义文件如下

变量.yml

aws_common_tags:
    Project: "{{ project_name }}"
    Application: "{{ application_name }}"
        Region: "{{ aws_region }}"
        Ansible: "Yes"

实例.yml

ec2_instance_tags:
    environment_name: dev

现在任务.yml我正在尝试创建带有所有标签的 aws ec2 实例,但它不起作用。

ec2:
    key_name: "{{ keypair_name }}"
    instance_type: "{{ ec2_instance_type }}"
    instance_tags: "{{ aws_common_tags + ec2_instance_tags }}"

我们收到以下错误:

"tags": {
        "Ansible": "Yes",
        "Application": "webserver",
        "Project": "document_upload",
        "Region": "us-west-2"
    },

fatal: [localhost]: FAILED! => {
    "msg": "Unexpected templating type error occurred on ({{ aws_common_tags + ec2_instance_tags }}): unsupported operand type(s) for +: 'dict' and 'dict'" }

谢谢

答案1

这是可能的结合字典。例如

ec2:
    key_name: "{{ keypair_name }}"
    instance_type: "{{ ec2_instance_type }}"
    instance_tags: "{{ aws_common_tags|combine(ec2_instance_tags) }}"

连接“ +”可与列表或被视为列表的字符串一起使用。

相关内容