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

558 views
Skip to first unread message

Michael Kelly

unread,
Apr 8, 2014, 5:56:42 AM4/8/14
to ansible...@googlegroups.com
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



Michael DeHaan

unread,
Apr 9, 2014, 9:54:04 AM4/9/14
to ansible...@googlegroups.com
Change "command:" to "shell:" if you want to use shell features like wildcards, and this should work fine.

Thanks!


--
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/d08978c0-2ce4-4051-8daf-28d5f85596c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages