3

Is there a way to output only the last 5 lines of an Ansible shell output, for example?
Maybe using loops?

Example:

  - name: Running Migrations
    ansible.builtin.shell: /some-script-produces-lot-of-output.sh
    register: ps
  - debug: var=ps.stdout_lines

Debug should only output the last 5 lines.

β.εηοιτ.βε
  • 24,064
  • 11
  • 54
  • 67

1 Answers1

3

You can use Python's slicing notation for this:

- debug: 
    var: ps.stdout_lines[-5:]

Will output the 5 lines from the end of the list (hence the negative value) up until the end of the list.


Given the tasks

- command: printf "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10"
  register: ps

- debug: 
    var: ps.stdout_lines[-5:]

This yields:

TASK [command] ************************************************************
changed: [localhost]

TASK [debug] **************************************************************
ok: [localhost] => 
  ps.stdout_lines[-5:]:
  - line6
  - line7
  - line8
  - line9
  - line10
β.εηοιτ.βε
  • 24,064
  • 11
  • 54
  • 67