Getting Error while executing Commands having space in command module and shell module

Hi,

I am using ansible 2.2 to automate login to the remote machine and running onos on it.For that I have written a simple playbook as shown below

playbook.yml

You seem to be specifying a parameter as the command?
As per examples specify the actual command first, and then put the parameters after that.
Preferably on different lines to make it more readable.

http://docs.ansible.com/ansible/command_module.html#examples

Dick

Also, as soon as spaces are involved, trying to quote is always a good
idea:

command: "your command with {{something}} here..."
args:
  chdir: "/foobar"
  warn: "no"

Johannes

Yes Dick,

I tried that way as well still I am getting same error as you can see my playbook
playbook:

Hi Johannes,
As you can see in latest post I ran playbooks by adding quotes in it, But still I getting error. Do you have any idea why I am getting this error ? Is that I am missing something ?

  • shubham

Hi there,

tasks: - name: running ok clean command: ok clean

What kind of command is 'ok'? Is this your actual executable? In your
previous examples I assumed it was called 'onos'.

"[Errno 2] No such file or directory", "rc": 2}

Your command 'ok' does not exist.

As you can see I am running two different commands I include double
quotes before sending commands,Now I am not getting any error If I
run 'ls -lt' since it cames with space and earlier it was giving me
error but now it not.And when I am passing 'ok clean' command I am
getting following error

playbook with 'ok clean': --- - hosts: atrium gather_facts: no

tasks: - name: running ok clean command: "{{item}}" args: chdir:
"onos/" with_items: - "ls -lt" - "ifconfig" - "ok clean" register:
result - debug: var=result

Error message: changed: [192.168.2.129] => (item=ls -lt) changed:
[192.168.2.129] => (item=ifconfig) failed: [192.168.2.129] (item=ok
clean) => {"cmd": "ok clean", "failed": true, "item": "ok clean",
"msg": "[Errno 2] No such file or directory", "rc": 2} [WARNING]:

Still the executable 'ok' is missing...

Johannes

P.S.: Top-posting makes your mails very hard to read...

Hi Johannes,
This ‘ok’ means ‘onos-karaf’, When I execute ‘ok clean’ in my remote VM which is having onos installed in it. It used to run clean up Job and runs the new session of onos. In short it used to run onos controller installed in remote machine. Please consider this example only
playbook:

There is your problem. I guess you have some kind of alias in your VM
that lets you use ok without errors.

Ansible does not use the same environment as your user.

Try this in the VM:
which ok
alias ok

(I guess you get output for the second one.)

And put the full name for the executable in your playbook. If my guess
is right it should work.

Johannes

P.S.: BTW, your path "onos/" is not an absolute path, might be a
problem, too. Better put the full path there.

Hi Johannnes,

I tried ‘alias ok’ its giving me output “onos-karaf” thats ok.And yes this happening because of change of environment.So instead of running command using ansible I am thinking of writing simple python script which will run this ‘‘ok clean’’ command whenever I execute python script using ansible. I am hopping this could work. thanks for all your valuable help. I just want to ask you is their any way that I can get the same or similar environment in ansible which i am getting when I am running command manually to remote VM

  • shubham

Hi Johannnes,

I tried 'alias ok' its giving me output "onos-karaf" thats ok.

No, it explains your problem. You can use 'ok' as command as a user,
but ansible cannot. It does not know your alias.

Use the full name and the task will succeed. Period.

And
yes this happening because of change of environment.So instead of
running command using ansible I am thinking of writing simple
python script which will run this ''ok clean'' command whenever I
execute python script using ansible. I am hopping this could work.

Why would you do that? Why not simply change the wrong command in your
playbook?

thanks for all your valuable help. I just want to ask you is their
any way that I can get the same or similar environment in ansible
which i am getting when I am running command manually to remote VM

Yes. Has been asked on this list several times in the last couple of
weeks. Put the stuff in the environment section of your command task:
https://docs.ansible.com/ansible/playbooks_environment.html

But why do you need the same environment? Just for the one alias?

Johannes

I tried using complete command “onos-karaf clean” as well but its not working its giving me same error as you can see

error message:
failed: [192.168.2.129] (item=onos-karaf clean) => {“cmd”: “onos-karaf clean”, “failed”: true, “item”: “onos-karaf clean”, “msg”: “[Errno 2] No such file or directory”, “rc”: 2}

playbook:

But this time it is due to the fact that executables that are not in
PATH can only be called with their full path. Sorry, I forgot that.

Use "./onos-karaf clean" or "$HOME/onos/onos-karaf ..." or the full
path and it should work. I hope.

Johannes