![如何使用 Ansible win_get_url 模組下載最新版本?](https://rvso.com/image/769291/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20Ansible%20win_get_url%20%E6%A8%A1%E7%B5%84%E4%B8%8B%E8%BC%89%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%EF%BC%9F.png)
我嘗試下載最新安裝文件從我們的 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"
文件