ansible两个变量替换任务中的项目

问题描述 投票:-1回答:1

我想实现如下任务,{{snapshot id}}在变量中注册。 (在3个或更多快照ID的寄存器快照ID中)

instance id是另一个变量,其值从库存中读取

我想在任务中传递像下面这样的varailes

snapshot_id [0] ---> groups ['webA']

snapthot_id [1] ----> groups ['webb']

 ec2_vol:
        snapshot: "{{ snapshot_id }}"
        instance: "{{ hostvars[item]['instance_id'] }}"
        region: "{{ aws_region }}"
        device_name: /dev/sda1
     register: volume_id
     with_items:
         - {{ snapshot_id[0], groups['webA'] }}
         - {{ snapshot_id[1], groups['webB']    }}
         - {{ }}
         - {{ }}

实现这一目标的正确语法是什么。

ansible ansible-2.x
1个回答
-1
投票

使用with_together你可以得到这样的东西:

 ec2_vol:
        snapshot: "{{ item.0}}"
        instance: "{{ hostvars[item.1]['instance_id'] }}"
        region: "{{ aws_region }}"
        device_name: /dev/sda1
     register: volume_id
     with_together:
         - {{ snapshot_id }}
         - {{ [ groups['webA'], groups['webB'] ] }}

我不太确定你在哪里与group vars,但这应该满足你的需要。

© www.soinside.com 2019 - 2024. All rights reserved.