通过循环with_items获得ansible组

时间:2020-06-23 07:19:49

标签: ansible ansible-inventory

我正在尝试通过不起作用的以下过程获取清单主机组名称

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

这实际上是我想要做的 我将通过外壳脚本获得服务器列表

    - 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 }}'

对此有所帮助!

1 个答案:

答案 0 :(得分:1)

修复语法

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