Change current directory

27 views
Skip to first unread message

Kevin Kwon

unread,
May 14, 2019, 8:20:02 PM5/14/19
to Ansible Project


Hi..

I want to change the current directory in remote host by Ansible.
so, i have created the playbook like below. but, it is not working correctly.

would you please let me know how can i do change the current directory in the remote host?

Kevin



=======================================================================================================================

---
- name: Testing to change current direcotry
  hosts: Ansible-01
  tasks:
  - name: confirm current Directory in the switch
    become: yes
    become_method: sudo
    command: 'pwd'
    register: exec_result
  - debug: var=exec_result.stdout_lines

  - name: Go to "/tmp" Directory in the switch
    become: yes
    become_method: sudo
    command: ' cd /tmp '

  - name: confirm current Directory in the switch
    become: yes
    become_method: sudo
    command: 'pwd'
    register: exec_result
  - debug: var=exec_result.stdout_lines


======================================================================================================================

admin@ubuntu-Ansible:~$ ansible-playbook Change-dir.yml

PLAY [Testing to change current direcotry] *******************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [Ansible-01]

TASK [confirm current Directory in the switch] ***************************************************************************************************************
changed: [Ansible-01]

TASK [debug] *************************************************************************************************************************************************
ok: [Ansible-01] => {
    "exec_result.stdout_lines": [
        "/home/admin"
    ]
}

TASK [Go to "/tmp" Directory in the switch] ****************************************************************************************************************
fatal: [Ansible-01]: FAILED! => {"changed": false, "cmd": "cd /tmp", "msg": "[Errno 2] No such file or directory", "rc": 2}
        to retry, use: --limit @/home/admin/Change-dir.retry

PLAY RECAP ***************************************************************************************************************************************************
Ansible-01           : ok=3    changed=1    unreachable=0    failed=1

admin@ubuntu-Ansible:~$

James Cassell

unread,
May 14, 2019, 8:24:52 PM5/14/19
to Ansible List


On Tue, May 14, 2019, at 8:20 PM, Kevin Kwon wrote:
>
>
> Hi..
>
> I want to change the current directory in remote host by Ansible.
> so, i have created the playbook like below. but, it is not working
> correctly.
>

What problem are you actually trying to solve? Maybe try `args: chdir=/mydir`

V/r,
James Cassell
> *"/home/admin"*
> ]

Brian Coca

unread,
May 15, 2019, 10:47:11 AM5/15/19
to Ansible Project
Consider that EACH task get's it's own environment and session, the
first task hat does a cd, does NOT affect subsequent tasks.


--
----------
Brian Coca

Kevin Kwon

unread,
May 17, 2019, 12:42:44 AM5/17/19
to Ansible Project

Hi.. All
Thank you so much your help!
After changed the my playbook like below, working directory is changed.

Kevin

  - name: Go to /cftmp Directory and check the Direcotry in the switch
    become: yes
    become_method: sudo
    command: 'pwd'
    args:
      chdir: /cftmp
    register: exec_result
  - debug: var=exec_result.stdout_lines



2019년 5월 15일 수요일 오전 9시 20분 2초 UTC+9, Kevin Kwon 님의 말:
Reply all
Reply to author
Forward
0 new messages