# bash script not writing to file with ansible, but does locally

**URL:** <https://forum.ansible.com/t/bash-script-not-writing-to-file-with-ansible-but-does-locally/22562>\
**Category:** Ansible Project\
**Created:** [September 15, 2016, 7:30pm UTC](https://forum.ansible.com/t/bash-script-not-writing-to-file-with-ansible-but-does-locally/22562 "2016-09-15T19:30:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard\_kappler](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@richard\_kappler](https://forum.ansible.com/u/richard_kappler)\
**Post date:** [September 15, 2016, 7:30pm UTC](https://forum.ansible.com/t/bash-script-not-writing-to-file-with-ansible-but-does-locally/22562/1 "2016-09-15T19:30:27Z")

</div>

I have a bash script ( I know, it’s a bit sloppy):

`

#!/bin/sh

df -h \>\> mongo-rebuild-validate.log 2\>&1

/opt/mongodb/bin/mongo localhost:27017 /opt/mongodb/mongodb-create-visionnode.js \>\> mongo-rebuild-validate.log 2\>&1

/opt/mongodb/bin/mongoimport --host 127.0.0.1 --db visionnode --collection ftpUser --file /opt/mongodb/ftpUserJSON-Linux.js \>\> mongo-rebuild-validate.log 2\>&1

df -h \>\> mongo-rebuild-validate.log 2\>&1

`

on remotes servers that works fine, does what I designed it to do, including writing all the stdout and stderr to the intended log.

However, when try to run this script remotely using:

`

---

<div class="post-metadata">

**Author:** ![Matt\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matt\_Davis](https://forum.ansible.com/u/Matt_Davis)\
**Post date:** [September 15, 2016, 7:46pm UTC](https://forum.ansible.com/t/bash-script-not-writing-to-file-with-ansible-but-does-locally/22562/2 "2016-09-15T19:46:58Z")

</div>

Since your script doesn’t specify an absolute path for the output, I suspect it’s writing it to the Ansible module tempdir, which is being promptly deleted as soon as the task finished. Try adding a chdir=/some/permanent/path to the end of the command task to run the script from a non-ephemeral location.

---

<div class="post-metadata">

**Author:** ![richard\_kappler](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@richard\_kappler](https://forum.ansible.com/u/richard_kappler)\
**Post date:** [September 16, 2016, 4:24pm UTC](https://forum.ansible.com/t/bash-script-not-writing-to-file-with-ansible-but-does-locally/22562/3 "2016-09-16T16:24:07Z")

</div>

That was it, once I set an absolute path to the log, it worked. Thanks Matt!
