![Ansible 连接两个变量](https://linux22.com/image/752835/Ansible%20%E8%BF%9E%E6%8E%A5%E4%B8%A4%E4%B8%AA%E5%8F%98%E9%87%8F.png)
我有两个变量定义文件如下
变量.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) }}"
连接“ +
”可与列表或被视为列表的字符串一起使用。