我们是否可以使用ansible的URI模块将文件上传到nexus而不是使用 curl --upload-file 作为 shell 任务?
uri
模块有一个 src
参数可以解决这个问题。
所以像这样的任务(适应/完成 Nexus API 的行为):
- name: Push jar to nexus
uri:
url: "{{ nexus_url }}/nexus/content/repositories/{{ path_repository }}/{{ artifact_name }}"
method: PUT
src: "{{ local_file_path }}"
user: "{{ user }}"
password: "{{ password }}"
force_basic_auth: yes
#headers:
# Content-Type: application/octet-stream # To avoid automatic 'application/x-www-form-urlencoded'
status_code:
- 201
我从我的课程中得到了这个例子。我真的不知道他们从哪里得到它,但它确实有效。同时我真的很讨厌Sonatype Nexus那些无用的文档。
---
- name: Upload JAR file via REST API using Ansible
hosts: localhost
gather_facts: no
vars:
app_name: gradle-java-project-1.0-SNAPSHOT
nexus_url: "http://XXX.YYY.ZZZ.AAA:8081"
artifact_name: gradle-java-project
artifact_version: 1.0-SNAPSHOT
jar_file_path: /home/tpolak/IdeaProjects/java-gradle-app/build/libs/gradle-java-project-1.0-SNAPSHOT.jar
nexus_user: admin
nexus_password: nexus
tasks:
- name: Upload JAR file
uri:
# Notes on Nexus upload artifact URL:
# 1 - You can add group name in the url ".../com/my/group/{{ artifact_name }}..."
# 2 - The file name (my-app-1.0-SNAPSHOT.jar) must match the url path of (.../com/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.jar), otherwise it won't work
# 3 - You can only upload file with SNAPSHOT in the version into the maven-snapshots repo, so naming matters
url: "{{ nexus_url }}/repository/maven-snapshots/com/my/{{ artifact_name }}/{{ artifact_version }}/{{ artifact_name }}-{{ artifact_version }}.jar"
method: PUT
src: "{{ jar_file_path }}"
user: "{{ nexus_user }}"
password: "{{ nexus_password }}"
force_basic_auth: yes
# With default "raw" body_format request form is too large, and causes 500 server error on Nexus (Form is larger than max length 200000), So we are setting it to 'json'
body_format: json
status_code: 201
register: response
- name: Print response
debug:
var: response