Import excel file values to ansible

i have an excel file with 2 columns and multiple rows of data, , basically like a key value pair, i want to import those as it is into win_Regedit for creating registry entries, how can i do it? i want the column1 row1 to be the name in win_regedit and column2 row1 to be the data in win_regedit

any help?

You are probably best off using a flat text file for this as Ansible can’t natively parse Excel files. A very basic (can’t guarantee to work) example of using JSON for this would be

JSON File:

[
{
“path”: “HKLM:\Software\MyCompany”,
“name”: “hello”,
“data”: “world”
},
{

“path”: “HKLM:\Software\MyCompany”,
“name”: “hi”,
“data”: “ansible”

}
]

Ansible Tasks:

If the JSON file is on your Windows box

  • name: read remote file
    slurp:
    src: C:\json_file.txt
    register: reg_changes_raw

  • name: convert raw slurp to json object
    set_fact:
    reg_changed: “{{ reg_changes_raw | b64decode | from_json }}”

If the JSON file in on your Ansible host

  • name: read local file
    set_fact:
    reg_changes: “{{ lookup(file, ‘json_file.json’) | from_json }}”

Loop through JSON for win_regedit

  • name: win_regedit tasks
    win_regedit:
    path: “{{item.path}}”
    name: “{{item.name}}”
    data: “{{item.data}}”
    with_items: “{{reg_changes}}”

How you implement this is up to you but if it is the same reg keys you need to update and it changes only sporadically you should probably just stick with using the group_vars instead of dealing with external files

Thanks for the reply

Ansible has a 'csv' lookup plugin that can read the data, so somthing like:

- lineinfile: ...
  with_csvfile: '/path/to/excel.csv' ...

can you post an example to read csv file, and also when i have a compressed json ansible is not able to read it, can i decompress the json to make it work?

For an example http://docs.ansible.com/ansible/playbooks_lookups.html#the-csv-file-lookup

The include_vars/vars_file will read JSON (as it is a subset of YAML),
Ansible does not support compressed files directly.

Hi Brian,

I want to read the content of the file using ansible playbook, is there a way to do this.

If something is there kindly share the solution.

Regards
Sumit Sahay