3

Im registering with Ansible output from network checking in such way:

- name: Test kube networking
  shell: kubectl exec -n iperf -it {{ item }} /test.sh 
  loop: "{{ pods.stdout_lines }}"
  register: echo

- debug: msg={{ item.stdout_lines }}
  loop: "{{ echo.results }}"

and now how to go trough all of entries? I want to have all "stdout_lines" from every result as item. Is such thing posible? Or maybe some "nested" loop one over results and second trough all stdout_lines from every result?

user3069488
  • 169
  • 2
  • 4
  • 19
  • Right now it isn't clear to me what output you have, or what you want.. Sure would help if you included the what your current output of the `Test kube networking` looks like, and what you actually want it to look like. – Zoredache Feb 05 '19 at 23:32

1 Answers1

6

Whenever I run into situations like this for ansible I stop and look for a better approach.

For example there is a 'script' module. If you write a nice script that outputs some json you can then import the output from that command as a ansible fact with the 'from_json' filter. This way you do the hard work in your favorite language and get your facts perfectly arranged so they are easy to loop through.

But to attempt to answer your question.. I am guessing you want to have all the stdout_lines from the kubectl command combined into a single long list. This should do it:

- name: Test kube networking
  shell: kubectl exec -n iperf -it {{ item }} /test.sh 
  loop: "{{ pods.stdout_lines }}"
  register: echo

- set_fact:
    stdout_lines: []

- set_fact:
    stdout_lines: "{{ stdout_lines + item.stdout_lines }}"
  with_items: "{{ echo.results }}"

- debug:
    msg: "This is a stdout line: {{ item }}"
  with_items: "{{ stdout_lines }}"
Nate Moseman
  • 106
  • 2
  • The first part of your answer is definitively good advise! I know we care about answering the questions asked around here and you did that I assume, but I just want to emphasize how valuable your advice is in my opinion. – Thorian93 Jul 22 '21 at 18:46