Ansible 带有_项和hostvars的变量

Ansible 带有_项和hostvars的变量,ansible,ansible-2.x,ansible-facts,Ansible,Ansible 2.x,Ansible Facts,我需要传递带有_项的动态组名信息,以便访问从其他主机运行的特定事实。我无法硬编码组名 我尝试通过几种不同的方式设置作为“GroupName”传递的泛型变量。包括 与_项:“{{groups['{{{GROUPNAME}}]}}” 致命:[localhost]:失败!=>{“msg”:“'dict object'没有属性{{GROUPNAME}'”}获取组中主机的列表并循环它们 vars: my_group: GROUPNAME tasks: - set_fact:

我需要传递带有_项的动态组名信息,以便访问从其他主机运行的特定事实。我无法硬编码组名

我尝试通过几种不同的方式设置作为“GroupName”传递的泛型变量。包括

与_项:“{{groups['{{{GROUPNAME}}]}}”


致命:[localhost]:失败!=>{“msg”:“'dict object'没有属性{{GROUPNAME}'”}

获取组中主机的列表并循环它们

  vars:
    my_group: GROUPNAME
  tasks:
    - set_fact:
        my_hosts: "{{ groups|
                      dict2items|
                      selectattr('key', 'match', my_group)|
                      map(attribute='value')|
                      list|
                      flatten }}"
    - debug:
        msg: "{{ hostvars[item]['ansible_check_mode'] }}"
      loop: "{{ my_hosts }}"
(未测试)

  vars:
    my_group: GROUPNAME
  tasks:
    - set_fact:
        my_hosts: "{{ groups|
                      dict2items|
                      selectattr('key', 'match', my_group)|
                      map(attribute='value')|
                      list|
                      flatten }}"
    - debug:
        msg: "{{ hostvars[item]['ansible_check_mode'] }}"
      loop: "{{ my_hosts }}"