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?
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"
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"
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With