Rest API’s calls are now a days the main
API to integrate with different applications. If you want to connect to an
existing infrastructure we can expose the infrastructure as rest API and other
applications can you the API to make calls.
Ansible also provides us a way to make the
Rest calls using URI module. The URI module allows us to send XML or JSON payload
and get the necessary details. In this article we will see how we can use the
URI module and make the Rest calls. As for the article I will be using the Nexus
artifactory to connect which run on the 8081 Port. The URL are specified in the
vars/main.yml file.
Make
a Get Call - Using the Ansible URI module to make get call is
pretty easy. We can use
- name: Maker a Call
uri:
url: "{{nexus_url}}/repositories"
method: GET
user: admin
password: admin123
force_basic_auth: yes
return_content: yes
In the above snippet, I am making a Rest
call to the “nexus_url/respositories” URL by passing them the user name and
password along with the method as “Get” and the return_content. We can use the Ansible debug to check the obtained
response.
Make
a Post call – Making a Post call needs a little extra
details. Now for making a post call we need the JSON data. We can both create a
file with JSON data and pass that to the file or we can include the JSON data
at the same place. Lets create a JSON file first as
{
"data": {
"repoType": "proxy",
"id": "somerepo1",
"name": "Some Repo Name1",
"browseable": true,
"indexable": true,
"notFoundCacheTTL": 1440,
"artifactMaxAge": -1,
"metadataMaxAge": 1440,
"itemMaxAge": 1440,
"repoPolicy": "RELEASE",
"provider": "maven2",
"providerRole":
"org.sonatype.nexus.proxy.repository.Repository",
"downloadRemoteIndexes": true,
"autoBlockActive": true,
"fileTypeValidation": true,
"exposed": true,
"checksumPolicy": "WARN",
"remoteStorage": {
"remoteStorageUrl":
"http://puppet.jas.com:8081/local",
"authentication": null,
"connectionSettings": null
}
}
}
Save the file as proxy.json and move the
file to the files location. Now we can use the proxy.json using the lookup in
ansible as,
- name: Make a Proxy Repository
uri:
url: "{{ nexus_url }}/repositories"
method: POST
body: "{{ lookup('file','proxy.json') }}"
user: admin
password: admin123
force_basic_auth: yes
body_format:
json
HEADER_Content-Type: application/json
HEADER_Accept: application/json,version=2
return_content: yes
status_code: 201
IN the above snippet I passed the JSON file
an argument to the Ansible lookup.
Besides this I passed the user name , password and most importantly the
body_format , Header fields etc. We also need to pass the return code of the
Rest call so that Ansible will compare the return status code with the status
code that we defined. In the above example I passed the status code as 201. In this
case the Ansible will check the rest call status code and passed status code to
check if that is success or not. Even though the rest call is success if the
status codes are not same Ansible will throw an error.
If we want to pass the JSON format in the
body itself without using the file, we can use
- name: Make a Hosted Repository
uri:
url: "{{nexus_url}}/{{ nexus_endpoint }}/{{ nexus_repositories_service }}"
method: POST
body: >
{
data: {
"repoType": "{{ hosted_repo_data.repoType }}",
"id": "{{ repoId }}",
"name": "{{ repoName }}",
"provider": "{{ repoProvider}}",
"browseable": {{ hosted_repo_data.browseable }},
"repoPolicy": {{ hosted_repo_data.repoPolicy }},
"providerRole": {{ hosted_repo_data.providerRole }}
}
}
user: "{{ nexus_admin }}"
password: "{{ nexus_admin_password }}"
force_basic_auth: yes
body_format: json
HEADER_Content-Type: application/json
HEADER_Accept: application/json,version=2
return_content: yes
status_code: 201
uri:
url: "{{nexus_url}}/{{ nexus_endpoint }}/{{ nexus_repositories_service }}"
method: POST
body: >
{
data: {
"repoType": "{{ hosted_repo_data.repoType }}",
"id": "{{ repoId }}",
"name": "{{ repoName }}",
"provider": "{{ repoProvider}}",
"browseable": {{ hosted_repo_data.browseable }},
"repoPolicy": {{ hosted_repo_data.repoPolicy }},
"providerRole": {{ hosted_repo_data.providerRole }}
}
}
user: "{{ nexus_admin }}"
password: "{{ nexus_admin_password }}"
force_basic_auth: yes
body_format: json
HEADER_Content-Type: application/json
HEADER_Accept: application/json,version=2
return_content: yes
status_code: 201
The body can be added directory to the Body
method in the Ansible JSON call.
That’s all about Ansible URL module and
rest calls. Hope this helps.
Nice presentation about the topic.
ReplyDeleteI'm working for a similar environment where I need to call Cherwell Incident management API through Ansible.
I got stuck at one point and not able to proceed.
Could you please suggest.
Please share your contact once. I got stuck at one point from where I cannot able to proceed further.
ReplyDeleteSome genuinely nice and useful information on this website, likewise I
ReplyDeletebelieve the design and style has excellent features.
Can I just say what a comfort to find someone who truly understands what they're discussing
ReplyDeleteon the net. You definitely understand how to bring an issue to light
and make it important. More people ought to read this
and understand this side of your story. It's surprising
you aren't more popular given that you certainly have the gift.