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
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
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
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.
Brian_Coca
(Brian Coca)
September 16, 2016, 6:38pm
5
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.
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
Edgars
(Edgars)
September 19, 2016, 9:45am
7
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: