Is it possible to use a wildcard for JAR file names in an Ansible playbook?

Viewed 171

I have an ansible file that I use to move a JAR file from one host to another host.

This looks like this:

- hosts: all
  tasks:

    - name:
        "move jar"
      synchronize:
        src: ../target/my-project-1.0.3-SNAPSHOT.jar
        dest: ~/

The problem is that the snapshot version (currently 1.0.3) will keep incrementing. I was wondering if there is a way to use a wildcard? I tried putting my-project-*-SNAPSHOT.jar but it did not work? Is it possible?

1 Answers

This is possible using the fileglob lookup.

- name: Move jar
  synchronize:
    src: "{{ item }}"
    dest: "~"
  with_fileglob: 
    - "../target/my-project-*-SNAPSHOT.jar"

Given the playbook

- hosts: localhost
  gather_facts: no

  tasks:
    - name: Move jar
      synchronize:
        src: "{{ item }}"
        dest: "~"
      with_fileglob: 
        - "../target/my-project-*-SNAPSHOT.jar"

The resulting play would be

/usr/local/ansible/play # ansible-playbook play.yml --inventory ../inventory.yml 

PLAY [localhost] **********************************************************************************

TASK [Move jar] ***********************************************************************************
changed: [localhost] => (item=/usr/local/ansible/play/../target/my-project-1.1.0-SNAPSHOT.jar)

PLAY RECAP ****************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

/usr/local/ansible/play # ls ~
my-project-1.1.0-SNAPSHOT.jar
Related