How to save a variable-variable to a temporary file that is automatically deleted at the end of the playback program?

In order to perform some operations locally (and not on the remote computer), I need to put the contents of the variable to be changed into a temporary file.

Please note that I am looking for a solution that takes care of creating a temporary file in the place where it can be written (without hardcoded names), as well as taking care of deleting the file, since we do not want to leave things behind.

+5
source share
2 answers

Instead of doing this with a file, why just use the environment ? This will allow you to easily work with the variable, and it will be alive through an unoccupied session, and you can easily get it at any stage or outside of them.

+1
source

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

+1
source

Source: https://habr.com/ru/post/1247209/


All Articles