How to use fetch to add output to txt file, instead of overwriting the file?

Viewed 98

I want to use fetch to gather a line of info from multiple nodes, and store them in the same txt file. right now I have: fetch: src: /path/to/file.txt dest: /ansible/path/to/file.txt flat: yes Instead of adding info to the existing txt file, it overrides the file and deletes the old info.

2 Answers

Fetch all the files that you want to append while renaming them with some combination of ansible_hostname in the name string. All files need to be added in the same folder, the destination name used will make the difference since you get the same file name from all sources it might be ending up overwriting the name. Use a changing variable like ansible_hostname or some sort of node identifier like IP address. Use this variable in building the file name for your fetched file

get a list of all the fetched files in a variable

Iterate thru that variable and then try lookup for each file block={{lookup('file', 'sourceFile')}} You can also iterate over all files in a folder, while appending to the end of the destination file. In your case, I believe blockinfile will be appropriate for this operation.

Related