具有动态组名的Ansible嵌套循环

时间:2016-03-16 14:21:05

标签: ansible ansible-playbook

我的vars文件:

applications:
  - name: server_1
    deploy: True
    branch: master
    group: frontend
  - name: server_2
    deploy: True
    branch: master
    group: backend

我的任务是

- name: deploy boxer
  command: "curl http://example.com?hosts={{item.1}}"
  with_subelements:
    - applications
    - "{{ groups[group] }}" # This is not working.

inventory.yml

[frontend]
host1.example.com
host2.example.com

[backend]
host11.example.com
host12.example.com

我想遍历应用程序,并遍历组中的每个主机(一次只有一个主机)将其传递给curl命令。

我最后想要这个。

curl http://example.com?hosts=host1.example.com
curl http://example.com?hosts=host2.example.com
curl http://example.com?hosts=host11.example.com
curl http://example.com?hosts=host12.example.com

1 个答案:

答案 0 :(得分:0)

您的意思是使用with_nested吗?

 with_nested:
   - "{{ applications }}"
   - "{{ groups }}"
 when: item.0.group == item.1