Search code examples
ansibleansible-inventory

get ansible groups by looping with_items


I'm trying to get the inventory host group names by below process which is not working

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

this is what actually I'm trying to do I will get a list of servers by a shell script

    - name: Getting the servers list
      shell: |
        sh getServers.sh
      register: vm

Then adding them into inventory by add_host

    - name: Creating Logical host_group for each server
      add_host:
       name: "{{ item }}"
       groups: ["{{item }}"]
      with_items: "{{ vm.stdout_lines }}"
      register: inv

Here I'm trying to get the only groups I've added in above step instead of all groups

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

Error is

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

Appreciate help on this!


Solution

  • Fix the syntax

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