ansible with_items 会解释

ansible with_items will interpret

我在让 Ansible 从 with_items 块中动态 select 库存组时遇到问题。我正在使用 ansible 2。

我尝试了很多变体,但是当我在名称的一部分中使用变量时,我找不到收集组的方法。我想知道 ansible 或 jinja 中是否有类似 eval 的东西,这将允许我访问它,或者我可以通过其他方式动态地访问组变量中的元素

- 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

Jinja/Ansible 中没有 eval。但是您应该能够以

的身份访问该组
groups["tag_environment_%s"|replace("%s", cw_env)]