大网数据广告活动
服务器运维问答

Ansible multiline results and loop

· 52CE 我爱测

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_li…

问题原文

原帖链接 · user3069488 · CC BY-SA 4.0

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?

已采纳解答

原帖链接 · Nate Moseman · CC BY-SA 4.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-02-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。