Optimization help

27 views
Skip to first unread message

Rahul Kumar

unread,
Dec 11, 2019, 9:33:57 AM12/11/19
to ansible...@googlegroups.com
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

Stefan Hornburg (Racke)

unread,
Dec 11, 2019, 10:05:05 AM12/11/19
to ansible...@googlegroups.com
On 12/11/19 3:33 PM, Rahul Kumar wrote:
> 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

>
> - 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
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAGH8rEy6yh4hbRRnitfRWsp7gs7vYKxtDiv98NazfZ6MQc9F2Q%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAGH8rEy6yh4hbRRnitfRWsp7gs7vYKxtDiv98NazfZ6MQc9F2Q%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Rahul Kumar

unread,
Dec 11, 2019, 10:18:48 AM12/11/19
to ansible...@googlegroups.com
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

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7c5e1930-bb12-92e6-4928-08b3a646a10b%40linuxia.de.

Vladimir Botka

unread,
Dec 11, 2019, 11:17:22 AM12/11/19
to Rahul Kumar, ansible...@googlegroups.com
Hi Rahul,
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

Rahul Kumar

unread,
Dec 11, 2019, 12:39:43 PM12/11/19
to Vladimir Botka, ansible...@googlegroups.com

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

Stefan Hornburg (Racke)

unread,
Dec 11, 2019, 12:42:27 PM12/11/19
to ansible...@googlegroups.com
On 12/11/19 6:39 PM, Rahul Kumar wrote:
>
> Thanks vlado! What will happen if let’s say item2conf is not defined ?
>

It will skip it.

Regards
Racke
>    when: item.name <http://item.name>
>
> Cheers,
>
>         -vlado
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAGH8rEzmM2QzxTqkj7mG2YaUuAVZ2dwWfQRLcxs%2BZ-hEFObvgA%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAGH8rEzmM2QzxTqkj7mG2YaUuAVZ2dwWfQRLcxs%2BZ-hEFObvgA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc

Rahul Kumar

unread,
Dec 11, 2019, 12:51:03 PM12/11/19
to ansible...@googlegroups.com

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

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3c923738-3af7-ebc3-e9fd-9399ad658127%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Dec 11, 2019, 12:53:53 PM12/11/19
to ansible...@googlegroups.com
On 12/11/19 6:50 PM, Rahul Kumar wrote:
>
> Ok it will skip and will continue to next item. Right ?

Yes

Regards
Racke

>
> On Wed, 11 Dec 2019 at 11:12 PM, Stefan Hornburg (Racke) <ra...@linuxia.de <mailto:ra...@linuxia.de>> wrote:
>
> On 12/11/19 6:39 PM, Rahul Kumar wrote:
> >
> > Thanks vlado! What will happen if let’s say item2conf is not defined ?
> >
>
> It will skip it.
>
> Regards
>           Racke
>
> > On Wed, 11 Dec 2019 at 9:47 PM, Vladimir Botka <vbo...@gmail.com <mailto:vbo...@gmail.com>
> <mailto:vbo...@gmail.com <mailto:vbo...@gmail.com>>> wrote:
> >
> >     Hi Rahul,
> >
> >     On Wed, 11 Dec 2019 20:03:32 +0530
> >     Rahul Kumar <rajput...@gmail.com <mailto:rajput...@gmail.com> <mailto:rajput...@gmail.com
> >        when: item.name <http://item.name> <http://item.name>
> >
> >     Cheers,
> >
> >             -vlado
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/ansible-project/CAGH8rEzmM2QzxTqkj7mG2YaUuAVZ2dwWfQRLcxs%2BZ-hEFObvgA%40mail.gmail.com
> >
> <https://groups.google.com/d/msgid/ansible-project/CAGH8rEzmM2QzxTqkj7mG2YaUuAVZ2dwWfQRLcxs%2BZ-hEFObvgA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/3c923738-3af7-ebc3-e9fd-9399ad658127%40linuxia.de.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAGH8rExy_qL8QbT5BdUYVQKCUYvAVWK3rJeUgB0GWeht2N%3DHpQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAGH8rExy_qL8QbT5BdUYVQKCUYvAVWK3rJeUgB0GWeht2N%3DHpQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc
Reply all
Reply to author
Forward
0 new messages