Import excel file values to ansible

3,233 views
Skip to first unread message

Suporter

unread,
Apr 20, 2017, 4:31:17 AM4/20/17
to Ansible Project
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

Suporter

unread,
Apr 21, 2017, 12:59:32 AM4/21/17
to Ansible Project
any help?

Jordan Borean

unread,
Apr 21, 2017, 1:25:02 AM4/21/17
to Ansible Project
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

Suporter

unread,
Apr 21, 2017, 4:20:37 AM4/21/17
to Ansible Project
Thanks for the reply


On Thursday, April 20, 2017 at 2:01:17 PM UTC+5:30, Suporter wrote:

Brian Coca

unread,
Apr 21, 2017, 4:05:01 PM4/21/17
to Ansible Project
Ansible has a 'csv' lookup plugin that can read the data, so somthing like:

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


----------
Brian Coca

Suporter

unread,
Apr 23, 2017, 4:12:07 AM4/23/17
to Ansible Project
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?


On Thursday, April 20, 2017 at 2:01:17 PM UTC+5:30, Suporter wrote:

Brian Coca

unread,
Apr 24, 2017, 3:41:09 PM4/24/17
to Ansible Project
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.

----------
Brian Coca

SUMIT SAHAY

unread,
May 10, 2018, 3:24:36 AM5/10/18
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages