# Ansible cannot find files after su to root

**URL:** <https://forum.ansible.com/t/ansible-cannot-find-files-after-su-to-root/22558>\
**Category:** Ansible Project\
**Created:** [September 15, 2016, 3:10pm UTC](https://forum.ansible.com/t/ansible-cannot-find-files-after-su-to-root/22558 "2016-09-15T15:10:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Moga](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Charles\_Moga](https://forum.ansible.com/u/Charles_Moga)\
**Post date:** [September 15, 2016, 3:10pm UTC](https://forum.ansible.com/t/ansible-cannot-find-files-after-su-to-root/22558/1 "2016-09-15T15:10:22Z")

</div>

Hello,  
I hope someone can answer this. I have a bunch of commands (shell scripts ) in a specific directory I need to run as root user. So I do:

- name: Becoming root  
command: sudo su -

- name: Changing directory  
command: cd “{{ command\_directory }}”

- name: Sourcing commands  
command: source mycommand.sh

The problem is keep getting:

fatal: [127.0.0.1]: FAILED! =\> {“changed”: false, “cmd”: “cd full\_path\_to\_my\_command\_directory”, “failed”: true, “msg”: “[Errno 2] No such file or directory”, “rc”: 2}

If I do manually, it works fine.

Any suggestions? I have tried become but exactly the same result. It just seems like once I change to root, cd command does not work anymore.

Thanks and regards  
Charles

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [September 15, 2016, 3:54pm UTC](https://forum.ansible.com/t/ansible-cannot-find-files-after-su-to-root/22558/2 "2016-09-15T15:54:38Z")

</div>

your 'cd' command runs as a separate command, that's why its' not working.

Try adding a chdir= option to your script command, see :

&nbsp;&nbsp;&nbsp;&nbsp;[http://docs.ansible.com/ansible/command\_module.html](http://docs.ansible.com/ansible/command_module.html)

---

<div class="post-metadata">

**Author:** ![Charles\_Moga](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Charles\_Moga](https://forum.ansible.com/u/Charles_Moga)\
**Post date:** [September 16, 2016, 4:25pm UTC](https://forum.ansible.com/t/ansible-cannot-find-files-after-su-to-root/22558/3 "2016-09-16T16:25:07Z")

</div>

Hi Dick,  
Thanks for response. I have already tried that and it was not working. I think the problem is related t the fact that I am running this playbook with connection=local. I have a feeling that even though this is locally, ansible still sees 127.0.0.1 as a separate entity on which the directory does not exist. Not sure if I am talking sense though.

Regards  
Charles

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 16, 2016, 5:23pm UTC](https://forum.ansible.com/t/ansible-cannot-find-files-after-su-to-root/22558/4 "2016-09-16T17:23:24Z")

</div>

You can't use Ansible like this.  
This is three individual tasks, so the "sudo su -" would run but does not have any impact on the next command.

And the cd will not change the path for the next command.

I highly recommend reading a Ansible book and/or reading this  
[https://docs.ansible.com/ansible/playbooks\_intro.html](https://docs.ansible.com/ansible/playbooks_intro.html)  
to get a basic understanding of how Ansible work.

To run tings as sudo or su you need to use become  
[https://docs.ansible.com/ansible/become.html](https://docs.ansible.com/ansible/become.html)  
Ansible can't stack sudo and su, like "sudo su -". You need to pick one of them.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 16, 2016, 6:38pm UTC](https://forum.ansible.com/t/ansible-cannot-find-files-after-su-to-root/22558/5 "2016-09-16T18:38:04Z")

</div>

This is how that would be sourced with su (just not sure that is useful either):

- command: source mycommand.sh chdir=“{{ command\_directory }}”

​ become: True

^ the issue is that ‘sudo su -’ is not supported, you either need full sudo or be able to run 'su’​ directly.

---

<div class="post-metadata">

**Author:** ![Charles\_Moga](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Charles\_Moga](https://forum.ansible.com/u/Charles_Moga)\
**Post date:** [September 19, 2016, 8:25am UTC](https://forum.ansible.com/t/ansible-cannot-find-files-after-su-to-root/22558/6 "2016-09-19T08:25:29Z")

</div>

Thanks for Brian,  
I have tried all these suggestions but with no luck.

- name: Sourcing commands from helpers file  
command: source helpers.sh chdir=“{{workspace }}”  
become: true  
become\_user: 

TASK [Debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“msg”: “Workspace is /home//projects/”  
}

TASK [Sourcing commands for database management from helpers file] \*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [127.0.0.1]: FAILED! =\> {“changed”: false, “cmd”: “source helpers.sh”, “failed”: true, “msg”: “[Errno 2] No such file or directory”, “rc”: 2}

I file is definitely there and the path is right. I must be missing something fundamental, possibly simple.

Regards  
Charles

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [September 19, 2016, 9:45am UTC](https://forum.ansible.com/t/ansible-cannot-find-files-after-su-to-root/22558/7 "2016-09-19T09:45:44Z")

</div>

source is a bash function not a command. This is why it cannot find such file, because there is no such file “source”. Try to use shell module instead of command.

Edgars

pirmdiena, 2016. gada 19. septembris 10:25:30 UTC+2, Charles Moga rakstīja:
