community.general.from_csv filter – Converts CSV text input into list of dicts

Note

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

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.

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

New in community.general 2.3.0

Synopsis

  • Converts CSV text input into list of dictionaries.

Input

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

Parameter

Comments

Input

string / required

A string containing a CSV document.

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.from_csv(key1=value1, key2=value2, ...)

Parameter

Comments

delimiter

string

A one-character string used to separate fields.

When using this parameter, you change the default value used by dialect.

The default value depends on the dialect used.

dialect

string

The CSV dialect to use when parsing the CSV file.

Possible values include excel, excel-tab or unix.

Default: "excel"

fieldnames

list / elements=string

A list of field names for every column.

This is needed if the CSV does not have a header.

skipinitialspace

boolean

Whether to ignore any whitespaces immediately following the delimiter.

When using this parameter, you change the default value used by dialect.

The default value depends on the dialect used.

Choices:

  • false

  • true

strict

boolean

Whether to raise an exception on bad CSV input.

When using this parameter, you change the default value used by dialect.

The default value depends on the dialect used.

Choices:

  • false

  • true

Examples

- name: Parse a CSV file's contents
  ansible.builtin.debug:
    msg: >-
      {{ csv_data | community.general.from_csv(dialect='unix') }}
  vars:
    csv_data: |
      Column 1,Value
      foo,23
      bar,42
  # Produces the following list of dictionaries:
  #   {
  #     "Column 1": "foo",
  #     "Value": "23",
  #   },
  #   {
  #     "Column 1": "bar",
  #     "Value": "42",
  #   }

Return Value

Key

Description

Return value

list / elements=dictionary

A list with one dictionary per row.

Returned: success

Authors

  • Andrew Pantuso (@Ajpantuso)

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.