# Optimization help

**URL:** <https://forum.ansible.com/t/optimization-help/31805>\
**Category:** Ansible Project\
**Created:** [December 11, 2019, 2:33pm UTC](https://forum.ansible.com/t/optimization-help/31805 "2019-12-11T14:33:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [December 11, 2019, 2:33pm UTC](https://forum.ansible.com/t/optimization-help/31805/1 "2019-12-11T14:33:32Z")

</div>

Hi Ansible Gurus,  
How can I optimize below code ? below is the sufficient code to understand the problem…

- name: Application item1Conf provisioning  
include\_tasks: resturlcall.yml  
with\_items:

- { name: ‘{{item1Conf}}’, resource: ‘item1’ }  
when: item1Conf is defined

- name: Application item2Conf provisioning  
include\_tasks: resturlcall.yml  
with\_items:

- { name: ‘{{item2Conf}}’, resource: ‘item2’ }  
when: item2Conf is defined

- name: Application item3Conf provisioning  
include\_tasks: resturlcall.yml  
with\_items:

- { name: ‘{{item3Conf}}’, resource: ‘item3’ }  
when: item3Conf is defined

Regards  
Rahul

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 11, 2019, 3:04pm UTC](https://forum.ansible.com/t/optimization-help/31805/2 "2019-12-11T15:04:35Z")

</div>

> Hi Ansible Gurus,  
> How can I optimize below code ? below is the sufficient code to understand the problem...

The code doesn't tell me anything what your objectives are and which tasks you are actually  
running.

There is no point in optimizing code when it might be wrong in the first place.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [December 11, 2019, 3:18pm UTC](https://forum.ansible.com/t/optimization-help/31805/3 "2019-12-11T15:18:16Z")

</div>

Hi stefan ,  
It is for rest api call for individual REST end points (Resource/service end points).  
and Below is the resturlcall.yaml which is included in above code.  
Basically i want to POST the data on REST url only when particular variable is defined. (as shown in above code item1Conf,item2Conf, item3Conf and these variable carries actual data to be posted)  
resturlcall.yml -

#tasks:

- name: application resource provisioning

uri:

url: https://{{ hostname}}/myconfig/rest/{{item.resource}}

body\_format: json

method: PUT

body: “{{[item.name](http://item.name)}}”

user: “{{ restapi\_user }}”

password: “{{ restapi\_password }}”

force\_basic\_auth: true

follow\_redirects: all

Regards  
Rahul

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 11, 2019, 4:17pm UTC](https://forum.ansible.com/t/optimization-help/31805/4 "2019-12-11T16:17:03Z")

</div>

Hi Rahul,

> Hi Ansible Gurus,  
> How can I optimize below code ? below is the sufficient code to understand  
> the problem...
> 
> - name: Application item1Conf provisioning  
> &nbsp;&nbsp;include\_tasks: resturlcall.yml  
> &nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;- { name: '{{item1Conf}}', resource: 'item1' }  
> &nbsp;&nbsp;when: item1Conf is defined
> 
> - name: Application item2Conf provisioning  
> &nbsp;&nbsp;include\_tasks: resturlcall.yml  
> &nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;- { name: '{{item2Conf}}', resource: 'item2' }  
> &nbsp;&nbsp;when: item2Conf is defined
> 
> - name: Application item3Conf provisioning  
> &nbsp;&nbsp;include\_tasks: resturlcall.yml  
> &nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;- { name: '{{item3Conf}}', resource: 'item3' }  
> &nbsp;&nbsp;when: item3Conf is defined

Default filter is needed to loop all items in one task because \*item\* is  
evaluated before \*when\*. Empty variable evaluates to False. For example

- name: Application provisioning  
&nbsp;&nbsp;&nbsp;include\_tasks: resturlcall.yml  
&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {name: '{{ item1Conf|default() }}', resource: 'item1'}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {name: '{{ item2Conf|default() }}', resource: 'item2'}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {name: '{{ item3Conf|default() }}', resource: 'item3'}  
&nbsp;&nbsp;&nbsp;when: item.name

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [December 11, 2019, 5:39pm UTC](https://forum.ansible.com/t/optimization-help/31805/5 "2019-12-11T17:39:13Z")

</div>

Thanks vlado! What will happen if let’s say item2conf is not defined ?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 11, 2019, 5:41pm UTC](https://forum.ansible.com/t/optimization-help/31805/6 "2019-12-11T17:41:37Z")

</div>

> Thanks vlado! What will happen if let’s say item2conf is not defined ?

It will skip it.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [December 11, 2019, 5:50pm UTC](https://forum.ansible.com/t/optimization-help/31805/7 "2019-12-11T17:50:32Z")

</div>

Ok it will skip and will continue to next item. Right ?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 11, 2019, 5:53pm UTC](https://forum.ansible.com/t/optimization-help/31805/8 "2019-12-11T17:53:04Z")

</div>

> Ok it will skip and will continue to next item. Right ?

Yes

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
