How to store and pass the value of register variable in loop using ansible

I am using the loop in ansible by “with_lines”, in which i am fetching the ip address of all the servers, on which i need to run the command. I need to fetch theid_rsa.pub files details from the server and store them into the file on my localhost. I need to append the output of all the id_rsa.pub keys, so that i will have the list of all those files on single place.

Currently I am able to fetch the value of id_rsa.pub file but i am not able to display the same using “debug”.
If i use r.results[0].stdout, then it gives the id_rsa.pub value for the first ip, but i need this to be in loop. I am stucked up in that point.
I tried below this, but it do not works, i am not sure how to use “item” with debug mode

  1. How can the debug thing can be in the loop
  2. How can i append the result of the debug using the loop in the below mentioned file.

Any quick help will be really appreciated, thanks in advance.

  • hosts: localhost
    tasks:

  • name: “fetch id_rsa.pub files data”
    shell: ssh dp794d@{{ item }} “cat /home/dp794d/.ssh/id_rsa.pub”
    register: r
    with_lines: cat “/home/capio/ansible/pmossWipm/day1/logs/testIP.txt”

  • debug: msg=“r.results[{{item}}].stdout”
    with_lines: cat “/home/capio/ansible/pmossWipm/day1/logs/testIP.txt”

  • name: “Append the output of username to LLC_LIST File”
    lineinfile:
    dest: /home/capio/file
    line: “{{ r.results[{{item}}].stdout }}”

Instead of iterating over a lines in a file and SSH’ing into them manually you could also use a dynamic inventory and then leverage the more robust ansible modules to get the file’s content?

i have my ansible engine on consider server A, from which i am running these playbooks on hoset server -B. I want these steps to be performed on the other servers which are listed ( ip address) under the text file. That why i can not use the host file inventory.
Thats the reason i need to find out the solution using the ssh command, it has alomst done, seems i am missing somewhere from syntax point of view.