user3880406
user3880406

Reputation:

Writing to a particular cell in csv file from Ansible

I'm trying to find if any option to write to a particular cell in CSV file from Ansible. I can use Ansible lookup plugin type "csvfile" to lookup for a value in the csv file but I need to add text to that file. Consider below example:

empID,name,mark
111,user1
112,user2
113,user3

I need to add the mark for each user, running the playbook should prompt for the empID and marks:

---
- name: Update data
  hosts: localhost
  vars_prompt:
    - name: empid
      prompt: EMP_ID
      private: no
    - name: marks
      prompt: MARKS
      private: no
  tasks:
    - name: Lookup
      debug:
        msg: "{{ lookup('csvfile', '{{ empid }} file=data.csv delimiter=, col=2') }}"

Need help to write to CSV file column C rows 1,2,3....

Upvotes: 1

Views: 5276

Answers (2)

PuzzledShaw
PuzzledShaw

Reputation: 113

As @mdaniel mentioned, there's no out-of-box write_csv module in Ansible. Without creating your own module, the only workaround I can think of is the following:

  1. You read in the CSV file with read_csv module and register the data table as a dictionary variable.
  2. You add the new values to the dict variable recursively with the key "mark". (Check this post for modifying values in dict variable)
  3. You loop over the dict variable and output each line to a new file with the lineinfile module. The file can be created with.csv extension, and in each line, the values are separated with delimiters (, or ;).

Here is an example:

---
- hosts: localhost
  gather_facts: no

  tasks:
    - name: Read from CSV file
      community.general.read_csv:
        path: data.csv
        key: empID
        delimiter: ','
      register: response
    - name: Create a dict variable from the CSV file
      set_fact:
        data_dict: "{{  response.dict  }}"
    - name: Update values in dictionary recursively
      set_fact:
        data_dict: "{{ data_dict | combine(new_item, recursive=true) }}"
      vars:
        new_item: "{ '{{ item.key }}': { 'mark': 'some_value' } }"
      with_dict: "{{ data_dict }}"
    - name: Debug
      debug:
        msg: "{{  data_dict  }}"
    - name: Save ddata_dict headers to a new CSV file
      lineinfile:
        path: new_data.csv
        line: empID,name,mark
        create: yes
    - name: Save data_dict values to a new CSV file
      lineinfile:
        path: new_data.csv
        line: "{{ item.value.empID }},{{ item.value.name }},{{ item.value.mark }}"
      loop: "{{ data_dict | dict2items }}"

And the outputted CSV file is:

empID,name,mark
111,user1,some_value
112,user2,some_value
113,user3,some_value

Upvotes: 4

mdaniel
mdaniel

Reputation: 33231

There doesn't appear to be a provided write_csv mechanism in ansible, partially because ansible is not a general purpose computing platform. However, you can easily write your own module which behaves as you wish, or you may be able to find an existing one out in ansible galaxy

Upvotes: 2

Related Questions