Ansible 解档模块 - “src”参数的模式匹配

hau*_*ron 1 deployment continuous-deployment ansible

构建配置中,CI 服务器构建一个Service包,为其分配特定的Version,然后将其归档到文件:Service-Version.tgz

部署配置中,同一个 CI 服务器会下载此类名称可变的包。此配置需要复制存档、解压并将服务部署到某个主机上。

用 Ansible 代码表达:

 - name: Unpack Service on remote host
   unarchive: src="{{ src_dir }}/Service-*.tgz" dest="{{ host_dest_dir }}"
Run Code Online (Sandbox Code Playgroud)

理想情况下:Ansible 会尝试匹配模式中文件名的任何内容。

事实上,这是行不通的:

fatal: [127.0.0.1]: FAILED! => {"changed": false, "failed": true, "msg": "Unable to find '(...)/deploy/Service-*.tgz' in expected paths."}
Run Code Online (Sandbox Code Playgroud)

如何让 Ansible 接受“src”的可变名称?

(我想我可以通过 grep 目录创建一个注册真实姓名的任务,但这也许可以Ansible 本身中完成?)

Yer*_*roc 6

正如之前对您的问题的评论所指出的,另一种方法(假设 Ansible 2+)是使用模块find。这看起来类似于:

- name: Find Service package
  find: paths="{{ src_dir }}" patterns="Service-*.tgz"
  register: find_result
- name: Unpack service on remote host
  unarchive: src="{{ item.path }}" dest="{{ host_dest_dir }}"
  with_items: "{{ find_result.files }}"
Run Code Online (Sandbox Code Playgroud)