Ansible cannot find files after su to root

226 views
Skip to first unread message

Charles Moga

unread,
Sep 15, 2016, 11:11:06 AM9/15/16
to Ansible Project
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



Dick Davies

unread,
Sep 15, 2016, 11:55:28 AM9/15/16
to ansible list
your 'cd' command runs as a separate command, that's why its' not working.

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

http://docs.ansible.com/ansible/command_module.html
> --
> 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/d33d5d14-9f51-4e40-891b-b29b54004b89%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Charles Moga

unread,
Sep 16, 2016, 12:25:07 PM9/16/16
to Ansible Project, di...@hellooperator.net
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

Kai Stian Olstad

unread,
Sep 16, 2016, 1:23:49 PM9/16/16
to ansible...@googlegroups.com
On 15. sep. 2016 17:10, Charles Moga wrote:
> 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

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
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
Ansible can't stack sudo and su, like "sudo su -". You need to pick one
of them.

--
Kai Stian Olstad

Brian Coca

unread,
Sep 16, 2016, 2:38:12 PM9/16/16
to ansible...@googlegroups.com
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.


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

Charles Moga

unread,
Sep 19, 2016, 4:25:30 AM9/19/16
to Ansible Project
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: <user>

TASK [Debug] *******************************************************************
ok: [127.0.0.1] => {
    "msg": "Workspace is /home/<user>/projects/<confidential>"
}

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

Edgars

unread,
Sep 19, 2016, 5:45:45 AM9/19/16
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages