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

1,026 views
Skip to first unread message

nishant bawane

unread,
Apr 27, 2017, 1:48:09 PM4/27/17
to Ansible Project
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 }}"

Dick Visser

unread,
Apr 27, 2017, 2:43:32 PM4/27/17
to ansible...@googlegroups.com


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?


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f7105a43-4c63-4bdd-88d7-f684ac5fed48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Dick Visser
Sr. System & Network Engineer
GÉANT

Want to join us? We're hiring: https://www.geant.org/jobs

nishant bawane

unread,
Apr 27, 2017, 3:05:44 PM4/27/17
to Ansible Project

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.
Reply all
Reply to author
Forward
0 new messages