# Calling rest API from Ansible

**URL:** <https://forum.ansible.com/t/calling-rest-api-from-ansible/35367>\
**Category:** Ansible Project\
**Created:** [June 17, 2021, 6:57pm UTC](https://forum.ansible.com/t/calling-rest-api-from-ansible/35367 "2021-06-17T18:57:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@Rodrigues](https://forum.ansible.com/u/Rodrigues)\
**Post date:** [June 17, 2021, 6:57pm UTC](https://forum.ansible.com/t/calling-rest-api-from-ansible/35367/1 "2021-06-17T18:57:08Z")

</div>

Hi all,

I have the below task which requires to call a rest API in order to call another API.  
Basically i have a dict of portgroups which i will need to call an API to get the ID from. {{ portgroupID }}

Then run the below code.  
I guess i could do it in a separate task, but then how do i link the ID with item.portgroup\_name? Is there any easy way? provided that these are coming from different sources?

- name: Creating Mac Discovery Pol Binding  
uri:  
url: "https://{{ hostname }}/policy/api/v1/infra/segments/nsx-{{ item.portgroup\_name | replace(’ ', ‘-’) }}/segment-discovery-profile-binding-maps/{{ portgroupID }}  
force\_basic\_auth: yes  
validate\_certs: False  
headers:  
Accept: “application/json”  
Content-Type: “application/json”  
user: “{{ username }}”  
password: “{{ password }}”  
method: PATCH  
body: “{{ lookup(‘file’,‘files/macbind.json’) }}”  
status\_code: “200”  
body\_format: json  
with\_items: “{{ jsondata.dvs\_portgroup\_info[lookup(‘vars’, ‘dvsName’)] }}”  
when:
- item.portgroup\_name.find(‘DVUplinks’) == -1
- item.portgroup\_name.find(‘nsx-’) == -1
