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

695 views
Skip to first unread message

sgam...@criterionnetworks.com

unread,
Jan 17, 2017, 5:37:35 AM1/17/17
to Ansible Project, Naresh Kumar
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
---
- hosts: atrium
  gather_facts: no

  vars:
    com: ok clean

  tasks:
    - name: running ok clean
      command: chdir=/home/admin/onos onos "{{com}}" warn=no
      register: result
    - debug: var=result


    Here to run onos I have to run 'ok clean' command in /home/admin/onos directory of remote machine, So I changed the working directory using 'chdir'. but after running this playbook I am getting following error

Error message while using command module:

FAILED! => {"changed": false, "cmd": "ok clean", "failed": true, "msg": "[Errno 2] No such file or directory", "rc": 2}

 As per my knowledge because space between ok and clean it considering ok as directory, since I tried some different command like 'ls' it is running perfectly but when I am running 'ls -lt' its giving me similar kind of error.

Even I tried to run same command using shell module as shown below:

playbook.yml
---
- hosts: atrium
  gather_facts: no

  vars:
    com: ok clean

  tasks:
    - name: running ok clean
      shell: chdir=/home/admin/onos onos {{com}} warn=no
      register: result
    - debug: var=result

        But unfortunately am still getting following error:

Error Message while using Shell module:

: FAILED! => {"changed": true, "cmd": "ok clean", "delta": "0:00:00.002467", "end": "2017-01-17 16:01:06.072516", "failed": true, "rc": 127, "start": "2017-01-17 16:01:06.070049", "stderr": "/bin/sh: 1: ok: not found", "stdout": "", "stdout_lines": [], "warnings": []}
 
     can anyone please help me to get through these error.It is ok if am able to execute this command using any one of the module. Thank you .

Dick Visser

unread,
Jan 18, 2017, 3:18:24 AM1/18/17
to ansible...@googlegroups.com, Naresh Kumar
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.


Dick


--


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/3e892001-a55f-4c5a-a48f-b6a838a8b85a%40googlegroups.com.


For more options, visit https://groups.google.com/d/optout.


Johannes Kastl

unread,
Jan 18, 2017, 4:59:02 AM1/18/17
to ansible...@googlegroups.com
On 18.01.17 09:17 Dick Visser wrote:
> 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.

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

signature.asc

sgam...@criterionnetworks.com

unread,
Jan 18, 2017, 6:49:05 AM1/18/17
to Ansible Project, nar...@criterionnetworks.com
Yes Dick,
 
       I tried that way as well still I am getting same error as you can see my playbook
playbook:

---
- hosts: atrium
  gather_facts: no

  tasks:
    - name: running ok clean
      command: ok clean
        chdir=/home/admin/onos

        warn=no
      register: result
    - debug: var=result
 
     Here 'ok clean' is the command to run onos,Which I wanted to pass to remote server still it is giving me following error

Error Message:

 FAILED! => {"changed": false, "cmd": "ok clean", "failed": true, "msg": "[Errno 2] No such file or directory", "rc": 2}
 [WARNING]: Could not create retry file '/etc/ansible-atrium/atrium.retry'.         [Errno 13] Permission denied: u'/etc/ansible-
atrium/atrium.retry'

   I wrote one more playbook to check the root cause of error.
playbook:


---
- hosts: atrium
  gather_facts: no


  tasks:
    - name: running ok clean
      command: "{{item}}"
      args:
        chdir: "onos/"
      with_items:
        - "ls -lt"
        - "ifconfig"
      register: result
   -  debug: result
 
   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]: Could not create retry file '/etc/ansible-atrium/atrium2.retry'.         [Errno 13] Permission denied: u'/etc/ansible-
atrium/atrium2.retry'
 
    It giving me same error for 'ok clean' command. And if I run the same command manually in the the remote VM it running perfectly. So can help me to get rid of this error, Am I missing something here ?

sgam...@criterionnetworks.com

unread,
Jan 18, 2017, 7:15:32 AM1/18/17
to Ansible Project, nar...@criterionnetworks.com
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


On Tuesday, January 17, 2017 at 4:07:35 PM UTC+5:30, sgam...@criterionnetworks.com wrote:

Johannes Kastl

unread,
Jan 18, 2017, 7:21:05 AM1/18/17
to ansible...@googlegroups.com
Hi there,

On 18.01.17 12:49 sgam...@criterionnetworks.com wrote:

> 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...

signature.asc

sgam...@criterionnetworks.com

unread,
Jan 18, 2017, 7:46:34 AM1/18/17
to Ansible Project, nar...@criterionnetworks.com
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:

---
- hosts: atrium
  gather_facts: no


  tasks:
    - name: running ok clean
      command: "{{item}}"
      args:
        chdir: "onos/"
      with_items:
        - "ok clean"
      register: result
   -  debug: result
    
          here "onos/" is directory of remote machine where I am running "ok clean" i.e. "onos-karaf clean" to run onos controller.I ran this command manually several time its working fine,But when comes with ansible its giving errors. Thank you


- shubham


On Tuesday, January 17, 2017 at 4:07:35 PM UTC+5:30, sgam...@criterionnetworks.com wrote:

Johannes Kastl

unread,
Jan 18, 2017, 2:50:46 PM1/18/17
to ansible...@googlegroups.com
On 18.01.17 13:46 sgam...@criterionnetworks.com wrote:
> This 'ok' means 'onos-karaf', When I execute 'ok
> clean' in my remote VM which is having onos installed in it. I

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.


signature.asc

sgam...@criterionnetworks.com

unread,
Jan 20, 2017, 2:26:40 AM1/20/17
to Ansible Project, nar...@criterionnetworks.com
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

On Tuesday, January 17, 2017 at 4:07:35 PM UTC+5:30, sgam...@criterionnetworks.com wrote:

Johannes Kastl

unread,
Jan 20, 2017, 2:32:32 AM1/20/17
to ansible...@googlegroups.com
On 20.01.17 08:26 sgam...@criterionnetworks.com wrote:
> 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

signature.asc

sgam...@criterionnetworks.com

unread,
Jan 20, 2017, 3:27:44 AM1/20/17
to Ansible Project, nar...@criterionnetworks.com
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:

---
- hosts: atrium
  gather_facts: no

  tasks:
    - name: running new session of onos controller
      command: "{{item}}"
      args:
        chdir: "/home/admin/onos/"
        warn: no
      with_items:
        - "onos-karaf clean"

      register: result
    - debug: var=result
 
     And after that I also need to run some more commands,Thats why I was asking you is their any provision for using same environment in ansible.   

On Tuesday, January 17, 2017 at 4:07:35 PM UTC+5:30, sgam...@criterionnetworks.com wrote:

Johannes Kastl

unread,
Jan 20, 2017, 3:34:57 AM1/20/17
to ansible...@googlegroups.com
On 20.01.17 09:27 sgam...@criterionnetworks.com wrote:
> 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}

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



signature.asc
Reply all
Reply to author
Forward
0 new messages