I am pulling JSON via the URI module and want to write the received content out to a file. I am able to get the content and output it to the debugger so I know the content has been received, but I do not know the best practice for writing files.
4 Answers
An important comment from tmoschou:
As of Ansible 2.10, The documentation for ansible.builtin.copy says:
If you need variable interpolation in copied files, use the
ansible.builtin.template module. Using a variable in the content
field will result in unpredictable output.
For more details see this and an explanation
Original answer:
You could use the copy
module, with the content
parameter:
- copy: content="{{ your_json_feed }}" dest=/path/to/destination/file
The docs here: copy module
-
6content="{{ your_json_feed }}" deals with whitespace and newlines. The quotes are important. Jan 23, 2015 at 19:21
-
@RamondelaFuente If I would like to add multiple vars content to the file, I need to use "copy" module multiple times or is there any other way? Jun 29, 2016 at 5:37
-
1@KishoreReddy I imagine you can use the jinja2 syntax to append variables. Something like "{{ variable ~ another_variable }}". Not pretty, but if things get any more complicated you could use the
template:
module and fill it with the variables registered at runtime. Jun 30, 2016 at 12:16 -
This works better when you want to generate a sha256 sum file in the form
shahash /path/to/file
(note 2 spaces between) and then validate from the command line (viasha256sum -c shafile
) what you generated via Ansible. Mar 23, 2017 at 14:49 -
4As of Ansible 2.10, The documentation for
ansible.builtin.copy
says: If you need variable interpolation in copied files, use theansible.builtin.template
module. Using a variable in thecontent
field will result in unpredictable output. For more details see github.com/ansible/ansible/issues/50580 and an explanation at github.com/ansible/ansible/issues/34595#issuecomment-356091161– tmoschouJan 6, 2021 at 1:37
Based on Ramon's answer I run into an error. The problem where spaces in the JSON I tried to write I got it fixed by changing the task in the playbook to look like:
- copy:
content: "{{ your_json_feed }}"
dest: "/path/to/destination/file"
As of now I am not sure why this was needed. My best guess is that it had something to do with how variables are replaced in Ansible and the resulting file is parsed.
Unless you are 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"
-
2While Ramon's answer addresses exactly what was asked, this answer is the best approach in general. For example, if you have a playbook that creates VMs in the cloud and you would like to generate an inventory of hosts for another playbook, a template is the way to go. Jul 10, 2016 at 8:00
-
-
-
@JanusTroelsen Sorry, can you explain why this is better than the accepted answer when writing large files? Feb 26, 2020 at 19:48
-
1@Vorticity because it becomes unwieldy when you have a file embedded in another file. you need escaping and syntax highlighting won't work. the templating mechanism enables inserting things in the middle if you need that. Feb 26, 2020 at 23:07
We can directly specify the destination file with the dest
option now. In the below example, the output json is stored into the /tmp/repo_version_file
- name: Get repository file repo_version model to set ambari_managed_repositories=false
uri:
url: 'http://<server IP>:8080/api/v1/stacks/HDP/versions/3.1/repository_versions/1?fields=operating_systems/*'
method: GET
force_basic_auth: yes
user: xxxxx
password: xxxxx
headers:
"X-Requested-By": "ambari"
"Content-type": "Application/json"
status_code: 200
dest: /tmp/repo_version_file