我在让 Ansible 从 with_items 块中动态选择库存组时遇到问题。我正在使用ansible 2。
我尝试了很多变体,但是当我在名称的一部分中使用变量时,我找不到收集组的方法。我想知道 ansible 或 jinja 中是否有类似 eval 的东西,这将允许我访问它,或者我可以通过其他方式动态访问 groups 变量中的元素
- hosts: localhost
gather_facts: false
vars:
cw_env: "pprod"
group_name: "{{ 'groups.tag_environment_' + cw_env }}"
tasks:
- name: Test Group variable with items.
debug: msg="{{ item }}"
with_items: "{{ 'groups.tag_environment_' + cw_env }}" # Gives STRING groups.tag_environment_pprod
with_items: "groups.tag_environment_{{cw_env }}" # Gives STRING groups.tag_environment_pprod
with_items: "groups.tag_environment_pprod" # WORKS - gives inventory group members
with_items: "{{ groups.tag_environment_pprod }}" # WORKS - gives inventory group members