# Change current directory before tasks are run

**URL:** <https://forum.ansible.com/t/change-current-directory-before-tasks-are-run/16200>\
**Category:** Ansible Project\
**Created:** [September 12, 2014, 4:45pm UTC](https://forum.ansible.com/t/change-current-directory-before-tasks-are-run/16200 "2014-09-12T16:45:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse\_DeFer](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@Jesse\_DeFer](https://forum.ansible.com/u/Jesse_DeFer)\
**Post date:** [September 12, 2014, 4:45pm UTC](https://forum.ansible.com/t/change-current-directory-before-tasks-are-run/16200/1 "2014-09-12T16:45:19Z")

</div>

I am trying to create a playbook to unmount an AFS mounted home directory, but I can’t unmount it because the user running ansible has open files because of ansible. I can’t do this with user with a local home, or ssh configs or anything like that. I need to do it with a regular ansible play and regular user, so no funky ssh configs or other changes to my hosts.

Here is my play:

- name: check if files are open in /afs  
raw: “cd / && /usr/sbin/lsof /afs”  
register: command\_result  
failed\_when: “command\_result.rc != 1”

If I run it with ansible, it will succeed, but ansible-playbook will show two processes spawned by the ansible run (sh and sudo).

Is there a way to change the current directory before tasks are run?

Thanks,  
Jesse

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [September 12, 2014, 8:01pm UTC](https://forum.ansible.com/t/change-current-directory-before-tasks-are-run/16200/2 "2014-09-12T20:01:35Z")

</div>

Does the chdir argument to raw work?

&nbsp;&nbsp;raw: "/usr/sbin/lsof /afs" chdir="/"

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 12, 2014, 8:07pm UTC](https://forum.ansible.com/t/change-current-directory-before-tasks-are-run/16200/3 "2014-09-12T20:07:24Z")

</div>

Another thought might be to use the “at” module to defer execution of this particular command, sleep a bit to let it run, and then proceed?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 12, 2014, 8:07pm UTC](https://forum.ansible.com/t/change-current-directory-before-tasks-are-run/16200/4 "2014-09-12T20:07:47Z")

</div>

Or configure ansible.cfg to use a different temp dir path?

---

<div class="post-metadata">

**Author:** ![Jesse\_DeFer](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@Jesse\_DeFer](https://forum.ansible.com/u/Jesse_DeFer)\
**Post date:** [September 12, 2014, 8:39pm UTC](https://forum.ansible.com/t/change-current-directory-before-tasks-are-run/16200/5 "2014-09-12T20:39:59Z")

</div>

raw doesn’t seem to have a chdir option or it doesn’t have the effect I need, command does but it still shows open files. I already have remote\_tmp set to a different directory. I’m sure I could do something with at, but that’s not the cleanest way to do it. I had thought to set “sudo\_exe = cd / && sudo” but that caused errors about executing sudo.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 12, 2014, 8:46pm UTC](https://forum.ansible.com/t/change-current-directory-before-tasks-are-run/16200/6 "2014-09-12T20:46:34Z")

</div>

If remote temp is outside of your home why would it still prevent things from unmounting?

---

<div class="post-metadata">

**Author:** ![Jesse\_DeFer](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@Jesse\_DeFer](https://forum.ansible.com/u/Jesse_DeFer)\
**Post date:** [September 12, 2014, 8:57pm UTC](https://forum.ansible.com/t/change-current-directory-before-tasks-are-run/16200/7 "2014-09-12T20:57:10Z")

</div>

The CWD of the shell and sudo are still the home directory. The chdir needs to happen before sudo is called.
