通过循环 with_items 获得 ansible 组

get ansible groups by looping with_items

我正在尝试通过以下无效的过程获取清单主机组名称

- debug:
   msg: "{{ groups['{{ item }}'] }}"
  with_items: "{{ vm.stdout_lines }}"

这就是我真正想要做的 我将通过 shell 脚本

获取服务器列表
    - name: Getting the servers list
      shell: |
        sh getServers.sh
      register: vm

然后通过add_host

将它们添加到库存中
    - name: Creating Logical host_group for each server
      add_host:
       name: "{{ item }}"
       groups: ["{{item }}"]
      with_items: "{{ vm.stdout_lines }}"
      register: inv

在这里,我试图获取我在上述步骤中添加的唯一组,而不是所有组

- debug:
   msg: "{{ groups['{{ item }}'] }}"
  with_items: "{{ vm.stdout_lines }}"

错误是

{"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute '{{ item }}'

感谢对此的帮助!

修正语法

  msg: "{{ groups[item] }}"