# Reading each line in CSV file and use the items and execute the SQL statement against a remote Database.

**URL:** <https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846>\
**Category:** Ansible Project\
**Created:** [February 26, 2018, 6:15am UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846 "2018-02-26T06:15:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raj5](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Raj5](https://forum.ansible.com/u/Raj5)\
**Post date:** [February 26, 2018, 6:15am UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846/1 "2018-02-26T06:15:27Z")

</div>

Hello Ansible Gurus,

I have a CSV file as below.

“DB\_SCHEMA”,“SYSTEM\_USER”,“SYSTEM\_PASSWORD”,“TNS\_NAME”,“TNS\_DESCRIPTION”  
“Client\_Schema1”,“user”,“password”,“TNSNAME”,“(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))”  
“Client\_Schema2”,“user”,“password”,“TNSNAME”,“(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))”  
“Client\_Schema3”,“user”,“password”,“TNSNAME”,“(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))”

I need to read each line of this CSV file and execute a SQL statement against a Remote Database.

This is the play book, which logs in to shared DB and get the CSV file to Local Ansible Master and execute Client Schema on Remote DB upgrade by using the entries from Shared DB.

- name: Run sqlplus command on {{ Shared DB }}  
shell: source ~/.bash\_profile && sqlplus -S ‘{{ SharedDB\_username }}/{{ SharedDB\_password }}@{{ SharedDB\_database\_host }}:1521/{{ SharedDB\_service\_name }}’ @“/etc/ansible/playbooks/database/roles/oracle-sql/files/final\_query.sql”  
delegate\_to: localhost  
tags: clientSchema

- name: Run powershell script  
script: files/DecryptCsv.ps1 -InputFilePath ‘{{ input\_csv\_path }}’   
-OutputFilePath ‘{{ output\_csv\_path }}’   
-PassPhrase ‘{{ pass\_phrase }}’   
-SaltValue ‘{{ salt\_value }}’   
-PasswordIterations {{ password\_iterations }}   
-InitVector ‘{{ init\_vector }}’  
delegate\_to: localhost  
tags: clientSchema

- name: get CSV File Content  
shell: source ~/.bash\_profile && awk -F’,’ ‘!/^#/ && !/^$/ { print }’ “{{ output\_csv\_path }}”  
register: groups\_out  
delegate\_to: localhost

- debug: var=groups\_out.stdout\_lines

- name: Update the Client Schema

shell: source ~/.bash\_profile && sqlplus -S ‘{{ item.1.groups\_out }}/{{ item.2.groups\_out }}@{{ item.4.groups\_out }}’ @“roles/oracle-sql/files/Scripts/client\_hotfix.sql” {{ item.0.groups\_out }}  
sudo: no  
with\_items: “{{ groups\_out.stdout\_lines }}”

Failing with below error message

fatal: [10.202.98.198]: FAILED! =\> {“failed”: true, “msg”: “The task includes an option with an undefined variable. The error was: ‘unicode object’ has no attribute ‘groups\_out’\n\nThe error appears to have been in ‘/etc/ansible/playbooks/database/roles/oracle-sql/tasks/main.yml’: line 57, 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: Update the Schema info\n ^ here\n\nexception type: \<class ‘ansible.errors.AnsibleUndefinedVariable’\>\nexception: ‘unicode object’ has no attribute ‘groups\_out’”}

And also I might need to use When condition to check particular client schema and shared schema version on remote DB then only apply this hotfix. it would be helpful if you guide me here to fix this issue. I tried different options nothing is working for me ☹

Thanks and Regards  
Shekar

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 26, 2018, 3:46pm UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846/2 "2018-02-26T15:46:30Z")

</div>

It says item.X.groups\_out do not exist.  
You would need to show you the content of groups\_out.stdout\_lines

g

---

<div class="post-metadata">

**Author:** ![Raj5](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Raj5](https://forum.ansible.com/u/Raj5)\
**Post date:** [February 26, 2018, 5:00pm UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846/3 "2018-02-26T17:00:57Z")

</div>

Hello Kai,

Thanks for looking into this. here is the content of “groups\_out.stdout\_lines”

“groups\_out.stdout\_lines”: [  
“"DB\_SCHEMA","SYSTEM\_USER","SYSTEM\_PASSWORD","TNS\_NAME","TNS\_DESCRIPTION"”,  
“"Client\_Schema1","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,  
“"Client\_Schema2","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,  
“"Client\_Schema3","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,  
“"Client\_Schema4","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,  
“"Client\_Schema5","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,  
“"Client\_Schema6","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,  
]  
}

---

<div class="post-metadata">

**Author:** ![Raj5](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Raj5](https://forum.ansible.com/u/Raj5)\
**Post date:** [February 26, 2018, 11:04pm UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846/4 "2018-02-26T23:04:31Z")

</div>

and also I updated the code now

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 27, 2018, 2:51pm UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846/5 "2018-02-27T14:51:21Z")

</div>

> \>  
> \> "groups\_out.stdout\_lines": [  
> \>  
> \> "\"DB\_SCHEMA\",\"SYSTEM\_USER\",\"SYSTEM\_PASSWORD\",\"TNS\_NAME\",\"TNS\_DESCRIPTION\"",  
> \>  
> \> "\"Client\_Schema1\",\"user\"password\",\"TNS\_NAME\",\"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))\"",  
> \>  
> \> "\"Client\_Schema2\",\"user\"password\",\"TNS\_NAME\",\"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))\"",  
> \>  
> \> "\"Client\_Schema3\",\"user\"password\",\"TNS\_NAME\",\"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))\"",  
> \>  
> \> "\"Client\_Schema4\",\"user\"password\",\"TNS\_NAME\",\"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))\"",  
> \>  
> \> "\"Client\_Schema5\",\"user\"password\",\"TNS\_NAME\",\"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))\"",  
> \>  
> \> "\"Client\_Schema6\",\"user\"password\",\"TNS\_NAME\",\"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))\"",  
> \> ]  
> \> }

groups\_out.stdout\_lines is a list element is a line so you can say groups\_out.stdout\_lines.0 for the fist line  
&nbsp;&nbsp;"\"DB\_SCHEMA\",\"SYSTEM\_USER\",\"SYSTEM\_PASSWORD\",\"TNS\_NAME\",\"TNS\_DESCRIPTION\""

and groups\_out.stdout\_lines.1 for the second line  
&nbsp;&nbsp;"\"Client\_Schema1\",\"user\"password\",\"TNS\_NAME\",\"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))\"",

Since your csv contains the headers of the column this is going to be problematic so I suggest you filter them out in your awk command.

> and also I updated the code now
> 
> ---  
> &nbsp;&nbsp;- name: Run sqlplus command on {{ Shared DB }}  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: source ~/.bash\_profile && sqlplus -S '{{ SharedDB\_username }}/{{  
> SharedDB\_password }}@{{ SharedDB\_database\_host }}:1521/{{  
> SharedDB\_service\_name }}'  
> @"/etc/ansible/playbooks/database/roles/oracle-sql/files/final\_query.sql"  
> &nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
> &nbsp;&nbsp;&nbsp;&nbsp;tags: clientSchema
> 
> &nbsp;&nbsp;- name: Run powershell script  
> &nbsp;&nbsp;&nbsp;&nbsp;script: files/DecryptCsv.ps1 -InputFilePath '{{ input\_csv\_path }}' \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-OutputFilePath '{{ output\_csv\_path }}' \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-PassPhrase '{{ pass\_phrase }}' \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-SaltValue '{{ salt\_value }}' \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-PasswordIterations {{ password\_iterations }} \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-InitVector '{{ init\_vector }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
> &nbsp;&nbsp;&nbsp;&nbsp;tags: clientSchema
> 
> &nbsp;&nbsp;- name: get CSV File Content  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: source ~/.bash\_profile && awk -F',' '!/^#/ && !/^$/ { print }'  
> "{{ output\_csv\_path }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;register: groups\_out  
> &nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost
> 
> &nbsp;&nbsp;- debug: var=groups\_out.stdout\_lines
> 
> &nbsp;&nbsp;- name: Update the Client Schema  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: source ~/.bash\_profile && sqlplus -S '{{ item.1 }}/{{ item.2  
> }}@{{ item.4 }}' @"roles/oracle-sql/files/Scripts/client\_hotfix.sql" {{  
> item.0 }}  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ groups\_out.stdout\_lines }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

with\_items: "{{ groups\_out.stdout\_lines }}" will take the first item into the variable item.  
(I assume the line with the column names are removed)  
"\"Client\_Schema1\",\"user\"password\",\"TNS\_NAME\",\"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))\""

This is one string so item will contain the whole line.

So getting element 0 from a string, item.0, will be the first character and item.1 will be the second and so on.

You need to create list that split on the comma, item.split(',') will create a list and item.split(',')[0] is the fist element "Client\_Schema1" and item.split(',')[1] the second one and so on.  
Since you date contains double qoutes it will contain those qoutes, so if they causing a problem you need to remove them.

> For some reason it's thinking that Item.0 as 1st position of line and  
> item.1 as second position of line.

That is correct since you are doing this on a string...

> I am looking more like DB\_SCHEMA as  
> item.0, SYSTEM\_USER as item.1 and so on......

...and not on a list

> Am I doing something wrong here? and also Is that possible I can ignore  
> line 1 which contains Field information?

Remove it in you awk where you remove empty lines and lines starting with #

---

<div class="post-metadata">

**Author:** ![Raj5](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Raj5](https://forum.ansible.com/u/Raj5)\
**Post date:** [February 27, 2018, 5:39pm UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846/6 "2018-02-27T17:39:22Z")

</div>

> > > “groups\_out.stdout\_lines”: [
> > > 
> > > “"DB\_SCHEMA","SYSTEM\_USER","SYSTEM\_PASSWORD","TNS\_NAME","TNS\_DESCRIPTION"”,
> > > 
> > > “"Client\_Schema1","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,
> > > 
> > > “"Client\_Schema2","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,
> > > 
> > > “"Client\_Schema3","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,
> > > 
> > > “"Client\_Schema4","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,
> > > 
> > > “"Client\_Schema5","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,
> > > 
> > > “"Client\_Schema6","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,  
> > > ]  
> > > }
> 
> groups\_out.stdout\_lines is a list element is a line so you can say groups\_out.stdout\_lines.0 for the fist line  
> “"DB\_SCHEMA","SYSTEM\_USER","SYSTEM\_PASSWORD","TNS\_NAME","TNS\_DESCRIPTION"”
> 
> and groups\_out.stdout\_lines.1 for the second line  
> “"Client\_Schema1","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”,
> 
> Since your csv contains the headers of the column this is going to be problematic so I suggest you filter them out in your awk command.
> 
> > and also I updated the code now
> > 
> > * * *
> > 
> > - name: Run sqlplus command on {{ Shared DB }}  
> > shell: source ~/.bash\_profile && sqlplus -S ‘{{ SharedDB\_username }}/{{  
> > SharedDB\_password }}@{{ SharedDB\_database\_host }}:1521/{{  
> > SharedDB\_service\_name }}’  
> > @“/etc/ansible/playbooks/database/roles/oracle-sql/files/final\_query.sql”  
> > delegate\_to: localhost  
> > tags: clientSchema
> > 
> > - name: Run powershell script  
> > script: files/DecryptCsv.ps1 -InputFilePath ‘{{ input\_csv\_path }}’   
> > -OutputFilePath ‘{{ output\_csv\_path }}’   
> > -PassPhrase ‘{{ pass\_phrase }}’   
> > -SaltValue ‘{{ salt\_value }}’   
> > -PasswordIterations {{ password\_iterations }}   
> > -InitVector ‘{{ init\_vector }}’  
> > delegate\_to: localhost  
> > tags: clientSchema
> > 
> > - name: get CSV File Content  
> > shell: source ~/.bash\_profile && awk -F’,’ ‘!/^#/ && !/^$/ { print }’  
> > “{{ output\_csv\_path }}”  
> > register: groups\_out  
> > delegate\_to: localhost
> > 
> > - debug: var=groups\_out.stdout\_lines
> > 
> > - name: Update the Client Schema  
> > shell: source ~/.bash\_profile && sqlplus -S ‘{{ item.1 }}/{{ item.2  
> > }}@{{ item.4 }}’ @“roles/oracle-sql/files/Scripts/client\_hotfix.sql” {{  
> > item.0 }}  
> > with\_items: “{{ groups\_out.stdout\_lines }}”  
> > delegate\_to: localhost
> 
> with\_items: “{{ groups\_out.stdout\_lines }}” will take the first item into the variable item.  
> (I assume the line with the column names are removed)  
> “"Client\_Schema1","user"password","TNS\_NAME","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SANDBOX1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))"”
> 
> This is one string so item will contain the whole line.
> 
> So getting element 0 from a string, item.0, will be the first character and item.1 will be the second and so on.
> 
> You need to create list that split on the comma, item.split(‘,’) will create a list and item.split(‘,’)[0] is the fist element “Client\_Schema1” and item.split(‘,’)[1] the second one and so on.  
> Since you date contains double qoutes it will contain those qoutes, so if they causing a problem you need to remove them.
> 
> > For some reason it’s thinking that Item.0 as 1st position of line and  
> > item.1 as second position of line.
> 
> That is correct since you are doing this on a string…
> 
> > I am looking more like DB\_SCHEMA as  
> > item.0, SYSTEM\_USER as item.1 and so on…
> 
> …and not on a list
> 
> > Am I doing something wrong here? and also Is that possible I can ignore  
> > line 1 which contains Field information?
> 
> Remove it in you awk where you remove empty lines and lines starting with #
> 
> –  
> Kai Stian Olstad

Thanks Kai for detailed response, this looks more cleaner.

> TASK [oracle-sql : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> Tuesday 27 February 2018 11:30:36 -0600 (0:00:00.224) 0:00:05.277 \*\*\*\*\*\*  
> “groups\_out.stdout\_lines”: [  
> “Client\_Schema1,user,password,TNS\_NAME,(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=sandbox1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))”,  
> “Client\_Schema2,user,password,TNS\_NAME,(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=sandbox1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))”,  
> “Client\_Schema3,user,password,TNS\_NAME,(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=sandbox1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))”,  
> “Client\_Schema4,user,password,TNS\_NAME,(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=sandbox1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))”,  
> “Client\_Schema5,user,password,TNS\_NAME,(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=sandbox1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))”,  
> “Client\_Schema6,user,password,TNS\_NAME,(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=sandbox1)(PORT=1521))(CONNECT\_DATA=(SERVER=DEDICATED)(SERVICE\_NAME=orcl)))”,  
> ]  
> }

> * * *
> 
> - name: Run sqlplus command on {{ Shared DB }}  
> shell: source ~/.bash\_profile && sqlplus -S ‘{{ SharedDB\_username }}/{{ SharedDB\_password }}@{{ SharedDB\_database\_host }}:1521/{{ SharedDB\_service\_name }}’ @“/etc/ansible/playbooks/database/roles/oracle-sql/files/final\_query.sql”  
> delegate\_to: localhost  
> tags: clientSchema
> 
> - name: Run powershell script  
> script: files/DecryptCsv.ps1 -InputFilePath ‘{{ input\_csv\_path }}’   
> -OutputFilePath ‘{{ output\_csv\_path }}’   
> -PassPhrase ‘{{ pass\_phrase }}’   
> -SaltValue ‘{{ salt\_value }}’   
> -PasswordIterations {{ password\_iterations }}   
> -InitVector ‘{{ init\_vector }}’  
> delegate\_to: localhost  
> tags: clientSchema
> 
> - name: Get CSV File Content with out header  
> shell: source ~/.bash\_profile && awk -F’,’ ‘!/^#/ && !/^$/ && (NR!=1) { print }’ “{{ output\_csv\_path }}” | sed ‘s/"//g’  
> register: groups\_out  
> delegate\_to: localhost
> 
> - debug: var=groups\_out.stdout\_lines  
> delegate\_to: localhost
> 
> - name: Update the Client Schema  
> shell: source ~/.bash\_profile && sqlplus -S ‘{{ item.1 }}/{{ item.2 }}@{{ item.4 }}’ @“roles/oracle-sql/files/Scripts/client\_hotfix.sql” {{ item.0 }}  
> with\_items: “{{ groups\_out.stdout\_lines }}”  
> delegate\_to: localhost

How ever split option not working for me, Can you please provide sample snippet if possible for me to fetch individual items and split with comma based.  
Your help is highly appreciated on this.

Thanks and Regards  
Shekar

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 27, 2018, 5:54pm UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846/7 "2018-02-27T17:54:06Z")

</div>

You just need to replace your items like this:

&nbsp;&nbsp;- name: Update the Client Schema  
&nbsp;&nbsp;&nbsp;&nbsp;shell: source ~/.bash\_profile && sqlplus -S '{{ item.split(",").1 }}/{{ item.split(",").2 }}@{{ item.split(",").4 }}' @"roles/oracle-sql/files/Scripts/client\_hotfix.sql" {{ item.split(",").0 }}  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ groups\_out.stdout\_lines }}"  
&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

---

<div class="post-metadata">

**Author:** ![Raj5](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Raj5](https://forum.ansible.com/u/Raj5)\
**Post date:** [February 27, 2018, 6:02pm UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846/8 "2018-02-27T18:02:41Z")

</div>

Awesome Thanks Kai, This works like a charm. Sorry I misunderstood your statement earlier and trying to split that ahead of my execution of client\_hotfix.sql

---

<div class="post-metadata">

**Author:** ![Raj5](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Raj5](https://forum.ansible.com/u/Raj5)\
**Post date:** [March 7, 2018, 12:02am UTC](https://forum.ansible.com/t/reading-each-line-in-csv-file-and-use-the-items-and-execute-the-sql-statement-against-a-remote-database/26846/9 "2018-03-07T00:02:57Z")

</div>

@Kai Stian Olstad

On top of what we done, I have a requirement to compare shared Schema version comparison, updated code is below.

Step:1 Fetch the Shared Schema version Value and register for future reference.  
Step:2 Stream Line CSV file and register the Output  
Step:3 Use the Streamlined CSV file and Client schema values in Column 1, Get the client schema Build version, these values are array of stdout\_lines. How can I register this?  
Step:4 And use the Registered values and upgrade client schema when Shared Schema Version and Client Schema Version matches.(This is tricky part)

Can I combine Step:3 and Step:4 ? Any clues.

`
