Export debug msg to a file

Hello

Need some assistance to send to a text file a message that appears in debug msg.
my uri task give me the flow msg
“msg”: [

“JOB-1”,
“JOB-2”,
" JOB- 3 ",
" JOB- 4 ",
" JOB- 5 ",
" JOB- 6 ",
" JOB- 7 "
]
and I need to add task on the playbook to send this msg to specific file like this

cat jobs_lists.txt

JOB-1
JOB-2
JOB- 3
JOB- 4
JOB- 5
JOB- 6
JOB- 7

Appreciate your help.
Regards

The text you print in the debug task is previously stored in a variable?
If that’s the case, you can write the variable stdout_lines attribute to a local file using lineinfile module and delegate_to: localhost, so this is writted locally.

The text you print in the debug task is previously stored in a variable? No

  • name: Display all Jobs Names
    ansible.builtin.debug:
    msg: “{{ Jobs.json | json_query(‘Jobs.Jobs[*].Name’)}}”

ok: [localhost] => {
“changed”: false,
“msg”: [
“JOB-1”,
“JOB-2”,
“JOB-3”,
“JOB-4”,
“JOB-9”,
“JOB-8”,
“JOB-7”
]
}

and then I need task to save the list of the job name on specific file:

cat jobs_lists.txt

JOB-1
JOB-2
JOB- 3
JOB- 4
JOB- 5
JOB- 6
JOB- 7

Regards,

The text you print in the debug task is previously stored in a variable? *No*
- name: Display all Jobs Names
ansible.builtin.debug:
msg: "{{ Jobs.json | json_query('Jobs.Jobs[*].Name')}}"

ok: [localhost] => {
"changed": false,
"msg": [
"JOB-1",
"JOB-2",
"JOB-3",
"JOB-4",
"JOB-9",
"JOB-8",
"JOB-7"
]
}

and then I need task to save the list of the job name on specific file:

cat jobs_lists.txt

    JOB\-1
    JOB\-2
    JOB\- 3 
    JOB\- 4 
    JOB\- 5 
    JOB\- 6 
    JOB\- 7

Regards,

You can use a copy task with is delegated to localhost:

  - name: Write job file
    copy:
      content: "{{ Jobs.json | json_query('Jobs.Jobs[*].Name') | join('\n') }}"
      dest: /tmp/job_lists.txt
    delegate_to: localhost

Regards
         Racke

Just what I need.

Thanks for your support