Optimization help

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

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
        Racke

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}}”

user: “{{ restapi_user }}”

password: “{{ restapi_password }}”

force_basic_auth: true

follow_redirects: all

Regards
Rahul

Hi Rahul,

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

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
   include_tasks: resturlcall.yml
   loop:
     - {name: '{{ item1Conf|default() }}', resource: 'item1'}
     - {name: '{{ item2Conf|default() }}', resource: 'item2'}
     - {name: '{{ item3Conf|default() }}', resource: 'item3'}
   when: item.name

Cheers,

  -vlado

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

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

It will skip it.

Regards
          Racke

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

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

Yes

Regards
          Racke