我如何在Ansible中循环使用它

时间:2016-10-12 00:46:36

标签: ansible ansible-2.x

我在defaults / main.yml

中有这个
aws_efs_mount:
- "10.22.x.1": "us-east-1c.fs-123jha23.efs.us-east-1.amazonaws.com:/"
- "10.22.x.2": "us-east-1c.fs-97c3hw43.efs.us-east-1.amazonaws.com:/"
- "10.22.x.3": "us-east-1c.fs-76etgc22.efs.us-east-1.amazonaws.com:/"

如何迭代它?所以我可以这样做

- name: Show key/value pair
  debug: msg="Key is {{ ??? }} and value is {{ ??? }}"
  with_???: "{{ aws_efs_mount }}"

如果我使用with_items并使用item.key,我得到" item没有名为key"的属性,如果我使用with_dict,我得到" with_dict需要一个dict"

1 个答案:

答案 0 :(得分:1)

当你指定一个dicts数组并且它不是一个集合时,为什么不让dict更详细?

aws_efs_mount:
- { ip: "10.22.x.1", mount: "us-east-1c.fs-123jha23.efs.us-east-1.amazonaws.com:/"}
- { ip: "10.22.x.2", mount: "us-east-1c.fs-97c3hw43.efs.us-east-1.amazonaws.com:/"}
- { ip: "10.22.x.3", mount: "us-east-1c.fs-76etgc22.efs.us-east-1.amazonaws.com:/" }


- name: Show key/value pair
  debug: msg="Key is {{ item.ip }} and value is {{ item.mount }}"
  with_items: "{{ aws_efs_mount }}"