Change current directory before tasks are run

1,916 views
Skip to first unread message

Jesse DeFer

unread,
Sep 12, 2014, 12:45:19 PM9/12/14
to ansible...@googlegroups.com
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

Michael Peters

unread,
Sep 12, 2014, 4:01:40 PM9/12/14
to ansible...@googlegroups.com
Does the chdir argument to raw work?

raw: "/usr/sbin/lsof /afs" chdir="/"
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/31dfb506-1544-4093-978d-d1a923dd3e33%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
Sep 12, 2014, 4:07:28 PM9/12/14
to ansible...@googlegroups.com
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?



Michael DeHaan

unread,
Sep 12, 2014, 4:07:50 PM9/12/14
to ansible...@googlegroups.com
Or configure ansible.cfg to use a different temp dir path?

Jesse DeFer

unread,
Sep 12, 2014, 4:40:00 PM9/12/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
Sep 12, 2014, 4:46:39 PM9/12/14
to ansible...@googlegroups.com
If remote temp is outside of your home why would it still prevent things from unmounting?



Jesse DeFer

unread,
Sep 12, 2014, 4:57:10 PM9/12/14
to ansible...@googlegroups.com
The CWD of the shell and sudo are still the home directory.  The chdir needs to happen before sudo is called.
Reply all
Reply to author
Forward
0 new messages