Writing to csv file

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.

Hello Asha,

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

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

Regards
        Racke

Since you already using shell, do everything in one task

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

Actually I gave those as a sample..both commands will be different.. requirement is to merge both.

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}'

If you are certain that your two lists will be the same size, then you can try using an index variable. See

https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#tracking-progress-through-a-loop-with-index-var

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

–EbH