Ansible:显示最后 X 输出行
Ansible: Show last X output lines
例如,有没有办法只输出 Ansible shell 输出的最后 5 行?
也许使用循环?
示例:
- name: Running Migrations
ansible.builtin.shell: /some-script-produces-lot-of-output.sh
register: ps
- debug: var=ps.stdout_lines
调试应该只输出最后 5 行。
您可以为此使用 Python's slicing notation:
- debug:
var: ps.stdout_lines[-5:]
将从列表末尾(因此为负值)到列表末尾输出 5 行。
鉴于任务
- command: printf "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10"
register: ps
- debug:
var: ps.stdout_lines[-5:]
这产生:
TASK [command] ************************************************************
changed: [localhost]
TASK [debug] **************************************************************
ok: [localhost] =>
ps.stdout_lines[-5:]:
- line6
- line7
- line8
- line9
- line10
例如,有没有办法只输出 Ansible shell 输出的最后 5 行?
也许使用循环?
示例:
- name: Running Migrations
ansible.builtin.shell: /some-script-produces-lot-of-output.sh
register: ps
- debug: var=ps.stdout_lines
调试应该只输出最后 5 行。
您可以为此使用 Python's slicing notation:
- debug:
var: ps.stdout_lines[-5:]
将从列表末尾(因此为负值)到列表末尾输出 5 行。
鉴于任务
- command: printf "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10"
register: ps
- debug:
var: ps.stdout_lines[-5:]
这产生:
TASK [command] ************************************************************
changed: [localhost]
TASK [debug] **************************************************************
ok: [localhost] =>
ps.stdout_lines[-5:]:
- line6
- line7
- line8
- line9
- line10