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

**URL:** <https://forum.ansible.com/t/how-to-store-and-pass-the-value-of-register-variable-in-loop-using-ansible/24342>\
**Category:** Ansible Project\
**Created:** [April 27, 2017, 5:48pm UTC](https://forum.ansible.com/t/how-to-store-and-pass-the-value-of-register-variable-in-loop-using-ansible/24342 "2017-04-27T17:48:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nishant\_bawane](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nishant\_bawane](https://forum.ansible.com/u/nishant_bawane)\
**Post date:** [April 27, 2017, 5:48pm UTC](https://forum.ansible.com/t/how-to-store-and-pass-the-value-of-register-variable-in-loop-using-ansible/24342/1 "2017-04-27T17:48:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 27, 2017, 6:43pm UTC](https://forum.ansible.com/t/how-to-store-and-pass-the-value-of-register-variable-in-loop-using-ansible/24342/2 "2017-04-27T18:43:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nishant\_bawane](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nishant\_bawane](https://forum.ansible.com/u/nishant_bawane)\
**Post date:** [April 27, 2017, 7:05pm UTC](https://forum.ansible.com/t/how-to-store-and-pass-the-value-of-register-variable-in-loop-using-ansible/24342/3 "2017-04-27T19:05:44Z")

</div>

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.
