title: Ansible loop over the first task output array
body: <p>I want to loop over the first task output in second task. In the first task getting hostname and IP in the array.  In the second task loop over the array print the each item in the array in separate line.</p>
  
  <p>here the code I have so for.</p>
  
  <pre><code>- name: Store known hosts of 'all' the hosts in the inventory file
    hosts: localhost
    connection: local
  
    vars:
      ssh_known_hosts_command: "ssh-keyscan -T 10"
      ssh_known_hosts_file: "{{ lookup('env','HOME') + '/.ssh/known_hosts' }}"
      ssh_known_hosts: "{{ groups['all'] }}"
  
    tasks:
  
    - name: For each host, find the ip
      shell: 'echo -e "{{ item }}\n`dig +short {{ item }}`"'
      with_items: "{{ ssh_known_hosts }}"
      register: ssh_known_host_results
      ignore_errors: yes
  
    - name: print message
      debug:
        msg: "{{ item.stdout_lines[0] + ' test' }}"
      with_items: "{{ ssh_known_host_results.results }}"
  </code></pre>
  
  <p>in the second task how can I loop over ssh_known_host_results.results  array?</p>
  
  <p>thanks
  SR</p>
  
  <p>I am looking for something like this:</p>
  
  <pre><code>- name: print message
      debug:
        msg: "{{ item+ ' test' }}"
        with_items: "{{outer_item.stdout_lines "    
      with_items: "{{ ssh_known_host_results.results }}"
      loop_control:
      loop_var: outer_item
  </code></pre>
  
  <p>when I add to ignore localhost its not giving array item. how can it make return hostname and ip as two array elements?</p>
  
  <pre><code> - name: For each host, find the ip
      shell: 'echo -e "{{ item }}\n`dig +short {{ item }}`"'
      with_items: "{{ ssh_known_hosts }}"
      when: not item == 'localhost'
      register: ssh_known_host_results
      ignore_errors: yes
  
    - name: print message
      debug:
        msg: "{{ item + ' test' }}"
      with_items: "{{ ssh_known_host_results.results | map(attribute='stdout_lines') | list }}"
  </code></pre>