Writing a variable to a file in Ansible

I pull out the JSON through the URI module and want to write the received content to a file. I can get the content and output it to the debugger, so I know that the content is received, but I don’t know what is the best way to write files.

+48
file ansible-playbook
Oct 29 '14 at 18:39
source share
3 answers

You can use the copy module with the content parameter:

 - copy: content="{{ your_json_feed }}" dest=/path/to/destination/file 

Docs here: copy module

+92
Oct. 29 '14 at 21:12
source share

If you are not writing very small files, you should probably use templates .

Example:

 - name: copy upstart script template: src: myCompany-service.conf.j2 dest: "/etc/init/myCompany-service.conf" 
+4
May 22 '16 at 17:06
source share

Based on Ramon's answer, I ran into an error. The problem where I tried to write places in JSON, I fixed it by changing the task in the playbook to look like this:

 - copy: content: "{{ your_json_feed }}" dest: "/path/to/destination/file" 

At the moment, I'm not sure why this was necessary. My best guess is that it has something to do with replacing variables in Ansible, and the resulting file is parsed.

+2
Jan 15 '16 at 8:09
source share



All Articles