community.general.to_prettytable filter – Format a list of dictionaries as an ASCII table

Note

This filter plugin is part of the community.general collection (version 10.7.4).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.general. You need further requirements to be able to use this filter plugin, see Requirements for details.

To use it in a playbook, specify: community.general.to_prettytable.

New in community.general 10.7.0

Synopsis

  • This filter takes a list of dictionaries and formats it as an ASCII table using the prettytable Python library.

Requirements

The below requirements are needed on the local controller node that executes this filter.

  • prettytable

Input

This describes the input of the filter, the value before | community.general.to_prettytable.

Parameter

Comments

Input

list / elements=dictionary / required

A list of dictionaries to format.

Keyword parameters

This describes keyword parameters of the filter. These are the values key1=value1, key2=value2 and so on in the following example: input | community.general.to_prettytable(key1=value1, key2=value2, ...)

Parameter

Comments

column_alignments

dictionary

Dictionary where keys are column names and values are alignment settings. Valid alignment values are left, center, right, l, c, or r.

For example, {'name': 'left', 'id': 'right'} will align the name column to the left and the id column to the right.

column_order

list / elements=string

List of column names to specify the order of columns in the table.

header_names

list / elements=string

List of custom header names to use instead of dictionary keys.

Examples

---
- name: Set a list of users
  ansible.builtin.set_fact:
    users:
      - name: Alice
        age: 25
        role: admin
      - name: Bob
        age: 30
        role: user

- name: Display a list of users as a table
  ansible.builtin.debug:
    msg: >-
      {{
        users | community.general.to_prettytable
      }}

- name: Display a table with custom column ordering
  ansible.builtin.debug:
    msg: >-
      {{
        users | community.general.to_prettytable(
          column_order=['role', 'name', 'age']
        )
      }}

- name: Display a table with selective column output (only show name and role fields)
  ansible.builtin.debug:
    msg: >-
      {{
        users | community.general.to_prettytable(
          column_order=['name', 'role']
        )
      }}

- name: Display a table with custom headers
  ansible.builtin.debug:
    msg: >-
      {{
        users | community.general.to_prettytable(
          header_names=['User Name', 'User Age', 'User Role']
        )
      }}

- name: Display a table with custom alignments
  ansible.builtin.debug:
    msg: >-
      {{
        users | community.general.to_prettytable(
          column_alignments={'name': 'center', 'age': 'right', 'role': 'left'}
        )
      }}

- name: Combine multiple options
  ansible.builtin.debug:
    msg: >-
      {{
        users | community.general.to_prettytable(
          column_order=['role', 'name', 'age'],
          header_names=['Position', 'Full Name', 'Years'],
          column_alignments={'name': 'center', 'age': 'right', 'role': 'left'}
        )
      }}

Return Value

Key

Description

Return value

string

The formatted ASCII table.

Returned: success

Authors

  • Timur Gadiev (@tgadiev)