I want to interact with a seafile server with its REST api.
So far I had no problems translating POST or GET queries into the ansible uri module. However, I have a problem with getting a PUT query to work.
The following works with curl:
curl -X PUT -d "share_type=group&group_id=<groupid>&permission=rw" -H 'Authorization: Token <mysecrettoken>' -H 'Accept: application/json; charset=utf-8; indent=4' https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/
When I translate this to the following ansible task, it fails:
- name: mytask
uri:
url: "https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/"
method: PUT
headers: '{ "Authorization": "Token <mysecrettoken>" }'
body: '{ "share_type": "group", "group_id": "<groupid>", "permission": "rw"}'
body_format: json
return_content: yes
I get the error:
HTTP Error 500: INTERNAL SERVER ERROR", "redirected": false, "server": "nginx", "set_cookie": "SERVERID=<serverid>; path=/", "status": 500, "transfer_encoding": "chunked", "url": "https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/", "vary": "Accept-Language, Cookie"}
In a python script using the requests library, I had to supply the final ?p=/ as params={'p': '/'}. Is this the reason for the failure? How do I correctly submit the parameter then?
You should pass the headers as a YAML hash, not as a JSON string:
- name: mytask
uri:
url: "https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/"
method: PUT
headers:
Authorization: "Token <mysecrettoken>"
body: '{ "share_type": "group", "group_id": "<groupid>", "permission": "rw"}'
body_format: json
return_content: yes
For reference, see the docs, especially the second-to-last example:
- uri:
url: https://your.form.based.auth.example.com/dashboard.php
method: GET
return_content: yes
headers:
Cookie: "{{ login.set_cookie }}"
Related
I am using Ansible's URI module to call a simple GET endpoint but I am getting a 401 error message as shown below.
Here is the playbook configuration
- name: Elasticsearch Cluster health Check
uri:
url: https://elastichost:9200/_cluster/health?&pretty
url_password : xxxxxxx
url_username : xxxxxx
validate_certs : no
method: GET
body_format: json
force_basic_auth: yes
return_content: yes
headers:
Content-Type: "application/json"
register: showhealth
tags: NonProd
- name: Show Elasticsearch Health
debug: var=showhealth
tags: NonProd
Any inputs on how to resolve
specific issue with elastic. elastic is secure, you need token or certificate to connect to api.
And ofc, your's user need correct right.
exemple
- name: Create update_user
uri:
url: 'uri'
method: POST
user: elasticuser
body_format: json
headers:
Content-Type: application/json
body: '{{ item.body }}'
client_cert: 'certificate.crt'
client_key: 'certificate.key'
password: '{{ elastic_password }}'
validate_certs: false
return_content: true
why URI module ignores body. I need to send body to get the response what I need. Python request module works fine.
---
- name: Get info
hosts: local_host
gather_facts: no
vars:
auth_key: 'xxxxxx'
tasks:
- name: Fetch all entries matching
uri:
url: "https://get_changes.com?"
method: GET
headers:
Content-Type: "application/json"
Accept: "application/json"
Authorization: 'Basic {{auth_key}}'
Timeout: '30'
body_format: json
body:
sysparm_fields: "number, state"
sysparm_query:"cur_state=10^number=abcdef^u_ci_infra_type=network^name=Network"
return_content: yes
status_code: 200
register: crb_output
Ah I see. That's true. Args should be in URL itself for GET. forgot about that. Thanks.
What I'm trying to do
Everything concern HTTP requests.
Create something using POST API, if created I can get the resource id
If resource already created, it returns 422 status code
If 422 status code, get resource by name so I can access it's id
Create another resource with the given id inside its request body
The code I have
- name: Create cluster
uri:
url: '{{ rancher_url }}/v3/cluster'
method: POST
body: {"type":"cluster","nodes":[],"rancherKubernetesEngineConfig":{"ignoreDockerVersion":true, "network":{"type":"networkConfig","plugin":"{{ rancher_network_provider }}"}},"name":"{{ rancher_cluster_name }}"}
status_code: 201, 422
body_format: json
headers:
Authorization: "Bearer {{ api_key_result.json.token }}"
validate_certs: "{{ validate_certs }}"
register: cluster_created
- name: Cluster already exists, retrieving id
uri:
url: '{{ rancher_url }}/v3/cluster?name={{ rancher_cluster_name }}'
method: GET
status_code: 200
headers:
Authorization: "Bearer {{ api_key_result.json.token }}"
body_format: json
validate_certs: "{{ validate_certs }}"
register: cluster_found
when: cluster_created.status == 422
- name: Get command to launch for nodes
uri:
url: '{{ rancher_url }}/v3/clusterregistrationtoken'
method: POST
body: {"type":"clusterRegistrationToken","clusterId":"{{cluster_created.json.id | cluster_found.json.data[0].id}}"}
status_code: 201
body_format: json
headers:
Authorization: "Bearer {{ api_key_result.json.token }}"
validate_certs: "{{ validate_certs }}"
register: node_command_result
So basically I want this line to work : {{cluster_created.json.id | cluster_found.json.data[0].id}}
The error I get
TASK [rancher_worker : Get command to launch for nodes] ***************************************************************************************************************************************************************************************
fatal: [51.15.25.38]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got '['. String: {{cluster_created.json.id | cluster_found.json.data[0].id}}"}
What do you think ?
Thanks for your help :)
RestClient post request
I tried post request couple ways
#user = {name: 'xxxxxx', email: 'xxxxxx#gmail.com', password: 'qwertyqqq'}
RestClient.post 'http://localhost:4123/api/users?token=<token>', #user
RestClient::Request.execute(method: :post, url: 'http://localhost:4123/api/v1/users', token: '<token>', payload: '#user', headers: {"Content-Type" => "application/json"})
Error: RestClient::BadRequest: 400 Bad Request or RestClient::UnprocessableEntity: 422 Unprocessable Entity
Success cases
When i made a get request with rest client and with curl is just fine.
RestClient.get 'http://localhost:4123/api/v1/users?token=<token>'
With curl:
Get request:
curl -X GET http://localhost:4123/api/v1/users/1?token=<token>
Post request for helpy:
curl -d '{"name":"xxxx","email":"xxxx#gmail.com","password":"12345678", "admin": true, "role":"admin"}' -H "Content-Type: application/json" -X POST http://localhost:4123/api/v1/users?token=<token>
The difference between the CURL version and the RestClient version is that in the CURL version you send a JSON string as payload but in the RestClient sends the string '#user'.
It should be fine when you actually send JSON:
RestClient.post(
"http://localhost:4123/api/v1/users?token=<token>",
#user.to_json,
{ content_type: :json, accept: :json }
)
I have a working curl command which returns exactly what I want, bunch of JSON:
curl -D- -u username:password -X GET -H "Content-Type: application/json" https://stash.address.net/rest/api/1.0/projects/FOOBAR/repos\?limit\=1000
And I need to transform it into RestClient::Request, but I am still getting 401 back:
RestClient::Request.execute(
method: :get,
headers: {
content_type: 'application/json'
},
username: 'username',
password: 'password',
url: 'https://stash.address.net/rest/api/1.0/projects/FOOBAR/repos',
params: {
limit: '1000'
},
verify_ssl: false
)
Did I forget something? Is there something missing from my request? Isn't it exactly same as the curl command above?
From the documentation I don't see any mention of the username and params options. They suggest to interpolate the value in the URL.
RestClient.get 'https://username:password#stash.address.net/rest/api/1.0/projects/FOOBAR/repos', { accept: :json, params: { limit: 1000 }}