Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to change active directory in Ansible Playbook?

Tags:

ansible

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

When I run my playbook, I get:

TASK: [Go to the folder] *****************************  failed: [host] => {"failed": true, "rc": 256} msg: no command given 

Any help is much appreciated.

like image 236
indolent Avatar asked Oct 14 '13 21:10

indolent


People also ask

How do I change the path in Ansible?

You can change into a directory before running a command with ansible with chdir .

What is chdir in Ansible?

You can change and specify the directory path where you want to run the command using the chdir parameter. This parameter is available for both command and shell module. You can also change the default shell by specifying the absolute path of the require shell in the executable parameter.

What is Local_action in Ansible?

In an Ansible playbook, when local_action is used, Ansible will run the module work mentioned under it on the controller node. Mostly modules used with local_action are shell and command. As you can do almost all the tasks using these modules on the controller node.

How do I use Ansible playbook as a specific user?

To run an Ansible task as a specific user, rather than the normal root user, you can use the become_user directive and pass the user's username to execute the task. This is quite like using the sudo -u command in Unix.


1 Answers

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 
like image 105
Marcin Płonka Avatar answered Oct 03 '22 07:10

Marcin Płonka