Sasank Kathera
Sasank Kathera

Reputation: 1

Cd command is not working in ansible when using in -a tag

I am using the below command and i am getting an error [Errno 2] No such file or directory: b'cd'

ansible servers -a "cd /etc/ansible"

I know that we can make use of playbooks but I just don't want to create, is there any possibility of executing the above command? please let me know. Thank you.

Upvotes: -3

Views: 721

Answers (1)

U880D
U880D

Reputation: 12144

According your question I understand that you like to execute ad-hoc commands on remote Managed Nodes, using an inventory like

[servers]
test1.example.com
test2.example.com

[test]
test1.example.com
test2.example.com

The Ansible Engine can be installed on one Control Node only to make connections to the remote Managed Nodes to manage them. Therefore a command like

[email protected]:~$ ansible test --user ${REMOTE_ACCOUNT} --ask-pass -m shell --args 'cd /etc/ansible'
SSH password:
test1.example.com | FAILED | rc=1 >>
/bin/sh: line 0: cd: /etc/ansible: No such file or directory non-zero return code
test2.example.com.example.com | FAILED | rc=1 >>
/bin/sh: line 0: cd: /etc/ansible: No such file or directory non-zero return code

may fail since Ansible usually isn't and don't need be installed at all on the remote Managed Nodes. You may test your connection and execution in general via

[email protected]:~$ ansible test --user ${REMOTE_ACCOUNT} --ask-pass -m shell --args 'cd /tmp'
SSH password:
test1.example.com | CHANGED | rc=0 >>

test2.example.com | CHANGED | rc=0 >>

Because of your mentioned Error Code 2 ([Errno 2], RC2) you may also have a look into whats the difference between Ansible raw, shell and command or difference between shell and command in Ansible.

Upvotes: 0

Related Questions