我尝试下载最新安装文件来自我们的 JFrog 存储库,任务如下:
- name: Download Server.msi file from JFrog repository
ansible.windows.win_get_url:
url: "{{ jfrog_url }}/Product/20.100.999.4112/x64/Server.msi"
dest: C:\Server.msi
url_username: "{{ jfrog_username }}"
url_password: "{{ jfrog_password }}"
validate_certs: no
目前,URL 指向特定版本(即/20.100.999.4112/
)
如您所见,文件夹名称包含构建的版本号。
我如何编辑此任务以便每次都下载最新文件?
答案1
我如何编辑此任务以使其每次都下载最新文件?
一个最小的示例剧本(...这与在生产环境中的工作类似)将首先捕获最新版本
---
- hosts: localhost # controlnode.example.com
become: false
gather_facts: false
vars:
jfrog_url: artifactory.example.com
tasks:
- name: Get latest version from internal repository
uri:
url: "https://{{ jfrog_url }}/api/storage/Product/?lastModified"
method: GET
url_username: "{{ jfrog_username }}"
url_password: "{{ jfrog_password }}"
validate_certs: true
return_content: true
status_code: 200
body_format: json
register: response
check_mode: false
- set_fact:
filename: '{{ response.json.uri.split("/")[8] }}'
version: '{{ response.json.uri.split("-")[2] }}'
请注意,需要调整的解析和拆分任务,response.json.uri
以便从自定义数据结构中获取正确的数据filename
。version
之后可以下载
- name: Download file from internal repository
delegate_to: windows.example.com
win_get_url:
url: "https://{{ jfrog_url }}/Product/{{ filename }}"
url_username: "{{ jfrog_username }}"
url_password: "{{ jfrog_password }}"
dest: "C:\Server-{{ version }}.msi"
文档