ansible.builtin.csvfile (v2.16.5) — lookup

read data from a TSV or CSV file

| "added in version" 1.5 of ansible.builtin"

Authors: Jan-Piet Mens (@jpmens) <jpmens(at)gmail.com>

Install Ansible via pip

Install with pip install ansible-core==2.16.5

Description

The csvfile lookup reads the contents of a file in CSV (comma-separated value) format. The lookup looks for the row where the first column matches keyname (which can be multiple words) and returns the value in the O(col) column (default 1, which indexed from 0 means the second column in the file).

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name:  Match 'Li' on the first column, return the second column (0 based index)
  ansible.builtin.debug: msg="The atomic number of Lithium is {{ lookup('ansible.builtin.csvfile', 'Li file=elements.csv delimiter=,') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: msg="Match 'Li' on the first column, but return the 3rd column (columns start counting after the match)"
  ansible.builtin.debug: msg="The atomic mass of Lithium is {{ lookup('ansible.builtin.csvfile', 'Li file=elements.csv delimiter=, col=2') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Define Values From CSV File, this reads file in one go, but you could also use col= to read each in it's own lookup.
  ansible.builtin.set_fact:
    loop_ip: "{{ csvline[0] }}"
    int_ip: "{{ csvline[1] }}"
    int_mask: "{{ csvline[2] }}"
    int_name: "{{ csvline[3] }}"
    local_as: "{{ csvline[4] }}"
    neighbor_as: "{{ csvline[5] }}"
    neigh_int_ip: "{{ csvline[6] }}"
  vars:
    csvline: "{{ lookup('ansible.builtin.csvfile', bgp_neighbor_ip, file='bgp_neighbors.csv', delimiter=',') }}"
  delegate_to: localhost

Inputs

    
col:
    default: '1'
    description: column to return (0 indexed).

file:
    default: ansible.csv
    description: name of the CSV/TSV file to open.

default:
    description: what to return if the value is not found in the file.

encoding:
    default: utf-8
    description: Encoding (character set) of the used CSV file.
    version_added: '2.1'
    version_added_collection: ansible.builtin

delimiter:
    default: TAB
    description: field separator in the file, for a tab you can specify V(TAB) or V(\\t).

Outputs

_raw:
  description:
  - value(s) stored in file column
  elements: str
  type: list

See also