Reputation: 656
Are we able to use URI module of ansible to upload a file to nexus rather than using curl --upload-file as a shell task ?
Upvotes: 1
Views: 7935
Reputation: 11
I've gotten this example from my course. I really don't know from where they got it, but it works. At the same time I really hate Sonatype Nexus useless documentation.
---
- 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
Upvotes: 1
Reputation: 11605
Since ansible 2.7, uri
module has a src
parameter that should do the trick.
So something like this as a task (to adapt/complete with the behavior of the 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
Upvotes: 1