community.general.read_csv – Read a CSV file¶
Note
This plugin is part of the community.general collection (version 1.3.6).
To install it use: ansible-galaxy collection install community.general
.
To use it in a playbook, specify: community.general.read_csv
.
Parameters¶
Notes¶
Note
Ansible also ships with the
csvfile
lookup plugin, which can be used to do selective lookups in CSV files from Jinja.
Examples¶
# 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
- ansible.builtin.debug:
msg: 'User {{ users.dict.dag.name }} has UID {{ users.dict.dag.uid }} and GID {{ users.dict.dag.gid }}'
# 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
- ansible.builtin.debug:
msg: 'User {{ users.list.1.name }} has UID {{ users.list.1.uid }} and GID {{ users.list.1.gid }}'
# 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
Return Values¶
Common return values are documented here, the following are the fields unique to this module:
Authors¶
Dag Wieers (@dagwieers)