Search code examples
variablesansiblehostvars

How to show specific number of lines in Ansible debug task


How to show only the first five lines of the hostvars variable in Ansible?

- name: "Show Ansible hostvars var"
  debug:
    msg: "{{ hostvars }}" # How to stop after 5 showed lines?

Solution

  • One way could be to translate the YAML dictionary into a string, split it on line feed, apply a slice, then, join it back into a single string.

    For example:

    - debug:
        msg: "{{ (hostvars | to_nice_yaml | split('\n'))[:5] | join('\n') }}"