community.general.read_csv (8.5.0) — module

Read a CSV file

Authors: Dag Wieers (@dagwieers)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

Description

Read a CSV file and return a list or a dictionary, containing one dictionary per row.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Example CSV file with header
#
#   name,uid,gid
#   dag,500,500
#   jeroen,501,500

# Read a CSV file and access user 'dag'
- name: Read users from CSV file and return a dictionary
  community.general.read_csv:
    path: users.csv
    key: name
  register: users
  delegate_to: localhost
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- ansible.builtin.debug:
    msg: 'User {{ users.dict.dag.name }} has UID {{ users.dict.dag.uid }} and GID {{ users.dict.dag.gid }}'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Read a CSV file and access the first item
- name: Read users from CSV file and return a list
  community.general.read_csv:
    path: users.csv
  register: users
  delegate_to: localhost
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- ansible.builtin.debug:
    msg: 'User {{ users.list.1.name }} has UID {{ users.list.1.uid }} and GID {{ users.list.1.gid }}'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Example CSV file without header and semi-colon delimiter
#
#   dag;500;500
#   jeroen;501;500

# Read a CSV file without headers
- name: Read users from CSV file and return a list
  community.general.read_csv:
    path: users.csv
    fieldnames: name,uid,gid
    delimiter: ';'
  register: users
  delegate_to: localhost

Inputs

    
key:
    description:
    - The column name used as a key for the resulting dictionary.
    - If O(key) is unset, the module returns a list of dictionaries, where each dictionary
      is a row in the CSV file.
    type: str

path:
    aliases:
    - filename
    description:
    - The CSV filename to read data from.
    required: true
    type: path

strict:
    description:
    - Whether to raise an exception on bad CSV input.
    - When using this parameter, you change the default value used by O(dialect).
    - The default value depends on the dialect used.
    type: bool

unique:
    default: true
    description:
    - Whether the O(key) used is expected to be unique.
    type: bool

dialect:
    default: excel
    description:
    - The CSV dialect to use when parsing the CSV file.
    - Possible values include V(excel), V(excel-tab) or V(unix).
    type: str

delimiter:
    description:
    - A one-character string used to separate fields.
    - When using this parameter, you change the default value used by O(dialect).
    - The default value depends on the dialect used.
    type: str

fieldnames:
    description:
    - A list of field names for every column.
    - This is needed if the CSV does not have a header.
    elements: str
    type: list

skipinitialspace:
    description:
    - Whether to ignore any whitespaces immediately following the delimiter.
    - When using this parameter, you change the default value used by O(dialect).
    - The default value depends on the dialect used.
    type: bool

Outputs

dict:
  description: The CSV content as a dictionary.
  returned: success
  sample:
    dag:
      gid: 500
      name: dag
      uid: 500
    jeroen:
      gid: 500
      name: jeroen
      uid: 501
  type: dict
list:
  description: The CSV content as a list.
  returned: success
  sample:
  - gid: 500
    name: dag
    uid: 500
  - gid: 500
    name: jeroen
    uid: 501
  type: list

See also