Although using a shell / application environment, probably if you specifically want to use a file to store variable data, you can do something like this
- hosts: server1 tasks: - shell: cat /etc/file.txt register: some_data - local_action: copy dest=/tmp/foo.txt content="{{some_data.stdout}}" - hosts: localhost tasks: - set_fact: some_data="{{ lookup('file', '/tmp/foo.txt') }}" - debug: var=some_data
As for your requirement to provide the file with a unique name and clear it at the end of playback. I will leave this implementation to you
source share