IBM / ansible-for-i

the tool is to provide several customized modules for Ansible to manage IBM i systems.
GNU General Public License v3.0
55 stars 51 forks source link

Result SQL to CSV file #130

Open Ludovic-Menard opened 2 years ago

Ludovic-Menard commented 2 years ago

Hello all, I would like to execute a SQL request and I want to have the result to a CSV format file. I am looking for informations on Ansible documentation but I don't find. Can you help me ? Thanks Ludo

changlexc commented 2 years ago

Then Ansible for IBM i SQL query related module put the result into a dict of python object. I don't think now it has the ability to directly generate an CSV.

volmcat commented 5 months ago

Bumping, I have been trying to register SQL output from ibmi_sql_query module and dump to csv with no success. Any ideas to how this can be done?

Working with the following 2 tasks, query then build out csv with lineinfile but its not recognozing the registered ptf_output as a variable to pass

"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'row'. 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'row'\n\nThe error appears to be in '/runner/project/Playbooks/patching/queryptfinfo.yml': line 38, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Build out CSV file\n ^ here\n",

duieolson commented 1 month ago

This worked for me. My example takes the results of every host i've run it for and puts it in to a localhost directory to consolidate all the host systems into one csv file. Hope it helps.

samplesqltocsv.txt

krka01 commented 1 month ago

Ansible generates output in json format and like @duieolson writes you need to build the csv file.

The example samplesqltocsv.txt that @duieolson uploaded requires you to know and use the column names when the cvs file is built.

I wanted a more dynamic approach. The results from the SQL statement is returned as a list of dicts so you can use the dict2items to split them in key/value pairs and then use the key to build the column header and the values to populate the data section of the csv file.

Edit: Seems like hyphen - signs are treated like bullets even if inserting code using the code button. Also the indent does not seems to work. So I will also upload the code as a attachement. ibmi-sql-to-csv-file.yml.txt

`# ansible-playbook ibmi-sql-to-csv-file.yml -e "variable_host=xxx"