Trying to run a command when a file matching a regex does not exist.

Hi,

I’m trying to run a command when a file matching a regex does not exist.

My use case is

I am unzipping an artifact - ec2-api-tools.zip
This will unzip to a directory that includes the version number - e.g. ec2-api-tools-1.6.13.0
I want to check if there is a directory that matches the regex ‘ec2-api-tools-*’ and only run the unzip command if it doesn’t exist.
I don’t want to tie the script to a particular version as it will change over time.

This is the approach I took -

  • name: download ec2 command line tools
    command: chdir=~/tools wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip creates=ec2-api-tools.zip

  • name: Register if ec2 directory exists
    command: chdir=~/tools test -d ec2-api-tools-*
    ignore_errors: True
    register: ec2_tools_installed

  • name: unzip ec2-tools
    command: chdir=~/tools unzip ec2-api-tools.zip
    when: ec2_tools_installed.rc == 1

The problem is the 'test -d ec2-api-tools-’ always fails. The command passes fine if I run it from the machine itself. The script will work if I change the ec2-api-tools- to match the exact version number.

So what I’m wondering is -

Why does the * in the ‘test -d ec2-api-tools-*’ command not work correctly?
Is there a better way of accomplishing this that I’m just not seeing? I imagine it’s a common enough use case.

Thanks in advance,

Michael

Change “command:” to “shell:” if you want to use shell features like wildcards, and this should work fine.

Thanks!