Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible : Using regular expression in the module "copy"

The context

I'm trying to build a continuous-integration platform and in a Ansible playbook I use the maven-dependency-plugin:get to download an artifact from a Nexus on the master which will be deployed on a remote server using the Ansible module copy.

In order to make a lightweight and generic playbook, I defined a variable called app_version that, if it's defined, I download the given version and if not I download the Latest version from the Nexus repository. The artifact is downloaded in a given directory (/tmp/delivery/). Below my Ansible task:

- hosts: local
  tasks:
    - name: "Downloading the war"
      shell: >
           mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:get -DgroupId=App-DartifactId=App-Dversion={{ app_version if app_version is defined else 'LATEST' }} -Dpackaging=war -DrepositoryId=my-nexus -Ddest=/tmp/delivery/


My problem

To assure the playbook genericity, I need to write a regular expression in the Ansible module copy to pick the artifact having the pattern app*.war but it looks like the module don't provide this ability. Below my copy tastk:

- hosts: agir
  tasks:
    - name: "Copying the war"
      copy: src=/tmp/delivery/app*.war dest=/folder/app.war  owner=user group=group mode=0755

How can I use a regular expression in the module copy ?


I'm using Ansible 1.8

like image 873
Radouane ROUFID Avatar asked Jul 25 '16 15:07

Radouane ROUFID


1 Answers

You can use:

  - name: "Copying the war"
      copy: src={{item}} dest=/folder/app.war  owner=user group=group mode=0755
      with_fileglob: /tmp/delivery/app*.war

But what if there are more than one file?
I would recommend to register stdout of previous shell command and get filename from there if possible.

like image 190
Konstantin Suvorov Avatar answered Sep 27 '22 21:09

Konstantin Suvorov