Trying to run script from inside directory

65 views
Skip to first unread message

John Oliver

unread,
May 7, 2015, 5:04:52 PM5/7/15
to ansible...@googlegroups.com
I'm using 'unarchive' to extract a tarball to /tmp/  Inside, I have a shell script with includes references to relative paths.  This made my script fail as it was looking for the files relative to whatever directory it's running from.  Looking at 'shell' and 'command' I see a chdir argument, but I cannot get it to work.  If I:

    command: insert_certs.sh chdir=/tmp/dod_java_certs/

I get:

TASK: [Run DoD certificate installer] ***************************************** 
failed: [vdara] => {"cmd": "insert_certs.sh", "failed": true, "rc": 2}
msg: [Errno 2] No such file or directory

If I:

    command: insert_certs.sh
    args:
      chdir=/tmp/dod_java_certs/

I get:

TASK: [Run DoD certificate installer] ***************************************** 
fatal: [vdara] => args must be a dictionary, received chdir=/tmp/dod_java_certs/

I get the same problems when using 'shell'.  I was, though, able to:

    shell: "cd /tmp/dod_java_certs; ./insert_certs.sh"

So how would one correctly use the chdir argument?

Yassen Damyanov

unread,
May 8, 2015, 5:31:04 AM5/8/15
to ansible...@googlegroups.com

On Friday, May 8, 2015 at 12:04:52 AM UTC+3, John Oliver wrote:
I'm using 'unarchive' to extract a tarball to /tmp/  Inside, I have a shell script with includes references to relative paths.  This made my script fail as it was looking for the files relative to whatever directory it's running from.  Looking at 'shell' and 'command' I see a chdir argument, but I cannot get it to work.  If I:

    command: insert_certs.sh chdir=/tmp/dod_java_certs/

I get:

TASK: [Run DoD certificate installer] ***************************************** 
failed: [vdara] => {"cmd": "insert_certs.sh", "failed": true, "rc": 2}
msg: [Errno 2] No such file or directory

This looks fine at a first glance. Check if script has (1) executable permissions; (2) proper shebang at script's first line.
(I assume you are sure you do have the script stored on remote host's /tmp/dod_java_certs/insert_certs.sh at time of executing this ... do you?)

 
If I:

    command: insert_certs.sh
    args:
      chdir=/tmp/dod_java_certs/

I get:

TASK: [Run DoD certificate installer] ***************************************** 
fatal: [vdara] => args must be a dictionary, received chdir=/tmp/dod_java_certs/

Here the reason is you violate the yaml format;
the correct args definition would be (note the semicolon after chdir):

    command: insert_certs.sh
    args:
      chdir: /tmp/dod_java_certs/

(see http://docs.ansible.com/command_module.html  "Examples")

HTH,
-Yassen

Yassen Damyanov

unread,
May 8, 2015, 6:17:15 AM5/8/15
to ansible...@googlegroups.com


On Friday, May 8, 2015 at 12:31:04 PM UTC+3, Yassen Damyanov wrote:

On Friday, May 8, 2015 at 12:04:52 AM UTC+3, John Oliver wrote:
I'm using 'unarchive' to extract a tarball to /tmp/  Inside, I have a shell script with includes references to relative paths.  This made my script fail as it was looking for the files relative to whatever directory it's running from.  Looking at 'shell' and 'command' I see a chdir argument, but I cannot get it to work.  If I:

    command: insert_certs.sh chdir=/tmp/dod_java_certs/

I get:

TASK: [Run DoD certificate installer] ***************************************** 
failed: [vdara] => {"cmd": "insert_certs.sh", "failed": true, "rc": 2}
msg: [Errno 2] No such file or directory

This looks fine at a first glance. Check if script has (1) executable permissions; (2) proper shebang at script's first line.
(I assume you are sure you do have the script stored on remote host's /tmp/dod_java_certs/insert_certs.sh at time of executing this ... do you?)

You probably need:
   command: ./insert_certs.sh chdir=/tmp/dod_java_certs/

otherwise it looks for insert_certs.sh in PATH, not cwd.
Reply all
Reply to author
Forward
0 new messages