Ansible: How to change active directory in Ansible Playbook?

This question was in the results for when I was trying to figure out why 'shell' was not respecting my chdir entries when I had to revert to Ansible 1.9. So I will be posting my solution.

I had

- name: task name
  shell:
    cmd: touch foobar
    creates: foobar
    chdir: /usr/lib/foobar

It worked with Ansible > 2, but for 1.9 I had to change it to.

- name: task name
  shell: touch foobar
  args:
    creates: foobar
    chdir: /usr/lib/foobar

Just wanted to share.


There's no concept of current directory in Ansible. You can specify current directory for specific task, like you did in your playbook. The only missing part was the actual command to execute. Try this:

- name: Go to the folder and execute command
  command: chdir=/opt/tools/temp ls

Tags:

Ansible