Writing to csv file

33 views
Skip to first unread message

Asha Omanakuttan

unread,
May 29, 2019, 10:45:01 AM5/29/19
to Ansible Project
I need some contents to be written in csv format to a file.

shell: ps -ef | grep java | awk '{print $1}'
register: username

shell: ps -ef | grep java | awk '{print $2}'
register: id

The output for username will be similar to :
root
admin
test_admin

The output for id will be similar to:
1232
4343
2233

so I want this to be written to a csv file as

root,1232
admin,4343
test_admin,2233

Please suggest.

Stefan Hornburg (Racke)

unread,
May 29, 2019, 11:02:34 AM5/29/19
to ansible...@googlegroups.com
Hello Asha,

why not one task which already gives you the desired output:

ps -ef | grep java | awk '{print $1","$2}'

Regards
Racke

--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Kai Stian Olstad

unread,
May 29, 2019, 11:08:11 AM5/29/19
to ansible...@googlegroups.com
Since you already using shell, do everything in one task

- shell: ps -ef | awk '/java/ { print $1","$2 }' > /path/my.csv


--
Kai Stian Olstad

Asha Omanakuttan

unread,
May 30, 2019, 5:49:31 AM5/30/19
to Ansible Project
Actually I gave those as a sample..both commands will be different.. requirement is to merge both.

Asha Omanakuttan

unread,
May 30, 2019, 5:57:46 AM5/30/19
to Ansible Project
Both are different commands. If I use shell and >>test.csv for each task, it gets appended as
Root
Admin
Test_admin
1232
4343
2233.
But I need it in other way.


For eg
Shell: ps -ef | grep Java | awk '{print $1}'
Shell: ps -ef | grep Java | grep app | awk '{print $2}'

Eric Hymowitz

unread,
May 30, 2019, 9:12:58 AM5/30/19
to Ansible Project
If you are certain that your two lists will be the same size, then you can try using an index variable.  See


Iterate through one list, declare an index variable, and use the index variable to get the appropriate member out of the second list.

--EbH
Reply all
Reply to author
Forward
0 new messages