Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible Synchronize With Wildcard

I'm trying to synchronize a file with a wildcard:

- name: Install Services jar
  synchronize: src="{{repo}}/target/all-services-*.jar" dest=/opt/company

I'm doing this so that I don't have to update ansible every time our version number gets bumped. However, this throws a file not found exception when it is run. Does ansible support this? And if so, how can I do it?

like image 850
cscan Avatar asked Jun 16 '16 20:06

cscan


3 Answers

Ansible module synchronize uses rsync and supports custom options for rsync through parameter rsync_opts (since 1.6) which could be used to filter file.

Example:

- name: sync source code
  synchronize:
  src: "/path/to/local/src"
  dest: "{{lookup('env','HOME')}}/remote/src"
  rsync_opts:
  - "--include=*.py"
  - "--exclude=*.pyc"
  - "--delete"
like image 173
scrutari Avatar answered Nov 05 '22 13:11

scrutari


This can be done with ansible's with_lines:

- name: Install services jar
  synchronize: src="{{item}}" dest=/opt/company/
  with_lines: "find {{ core_repo }}/service-packaging/target/ -name all-services*.jar | grep -v original"
like image 2
cscan Avatar answered Nov 05 '22 13:11

cscan


If you're already using a with_items or related, then you might not be able to use with_lines instead. For example, if you're trying to run

- name: Install Services jar
  synchronize: src="{{repo}}/target/{{ item }}-*.jar" dest=/opt/company
  with_items:
    - service1
    - service2

When synchronize invokes rsync, it wraps the src and dest in quotes which breaks the wildcard/glob expansion.

I've been able to shell out to rsync directly to bypass this behavior.

- name: Install Services jar
  shell: rsync -azPihv {{repo}}/target/{{ item }}-*.jar {{ inventory_hostname }}:/opt/company
  register: rsync_cmd
  changed_when: rsync_cmd.stdout.find('xfer') != -1
  with_items:
    - service1
    - service2

(You may need to use xfr instead of xfer depending on your rsync.)

like image 1
Cody A. Ray Avatar answered Nov 05 '22 11:11

Cody A. Ray