I have taken just a sample from a huge data that I wish to read from variables and dump in the file.
cat junk.txt
{:,:"(s):"}}
This data in the junk is not in my control and comes as output from a database script execution.
Below is my attempt to dump the data from junk.txt to a log file github.out and the junk data should keep getting appended to the github.out throughout my play which is something I m not able to achieve.
---
- name: "Play 1-Find the details here"
hosts: localhost
gather_facts: no
tasks:
- name: Read junk file
raw: "cat {{ playbook_dir }}/junk.txt"
register: junkdata
# - name: Dump data to file
# raw: echo "{{ junkdata.stdout }}" >> "{{ playbook_dir }}/outputjunk.txt"
- name: Direct script execution in Standard logs for GitHub actions (TXTstdout)
ansible.builtin.copy:
content: ">> TXT SUCCESS OUTPUT1- {{ junkdata.stdout }}"
dest: "{{ playbook_dir }}/github.out"
force: no
- name: Direct script execution in Standard logs for GitHub actions (TXTstdout)
ansible.builtin.copy:
content: ">> TXT SUCCESS OUTPUT2- {{ junkdata.stdout }}"
dest: "{{ playbook_dir }}/github.out"
force: no
The commented section if uncommented to redirected content to log file gives me the below error:
TASK [Dump data to file]
Monday 27 November 2023 10:21:22 -0600 (0:00:00.035)
0:00:00.087 *******fatal: [localhost]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 1, "stderr": "/bin/sh: -c: line 0: syntax error near unexpected token
('\n/bin/sh: -c: line 0:
echo "{:,:"(s):"}}'\n", "stderr_lines": ["/bin/sh: -c: line 0: syntax error near unexpected token('", "/bin/sh: -c: line 0:
echo "{:,:"(s):"}}'"], "stdout": "", "stdout_lines": []}
I therefore decided to use the copy
module to dump the data to a file which it does.
But I wanted each content to keep appending to github.out which it does not.
Expected / Desired output:
$ cat github.out
>> TXT SUCCESS OUTPUT1- {:,:"(s):"}}
>> TXT SUCCESS OUTPUT2- {:,:"(s):"}}
Current output:
$ cat github.out
>> TXT SUCCESS OUTPUT1- {:,:"(s):"}}
And if the github.out exists from the previous run then nothing gets appended.
Can you please suggest?
Quote raw module like below to fix the first error:
raw: "echo {{ junkdata.stdout }} >> {{ playbook_dir }}/outputjunk.txt"
copy
module overwrites the content. So use lineinfile
instead, with a loop
like:
- name: Direct script execution in Standard logs for GitHub actions (TXTstdout)
loop:
- ">> TXT SUCCESS OUTPUT1- {{ junkdata.stdout }}"
- ">> TXT SUCCESS OUTPUT2- {{ junkdata.stdout }}"
ansible.builtin.lineinfile:
path: "{{ playbook_dir }}/github.out"
create: true
line: "{{ item }}"
$ cat github.out
>> TXT SUCCESS OUTPUT1- {:,:"(s):"}}
>> TXT SUCCESS OUTPUT2- {:,:"(s):"}}