Task execution only for special items within a loop

42 views
Skip to first unread message

Tom Bartsch

unread,
Sep 5, 2017, 7:53:53 AM9/5/17
to Ansible Project
Hi there,

I have the following playbook:

---
- hosts: "{{ host }}"
vars:
bundle_artifacts:
- artifactId: "someArtifact"
version: "1.0.0"
extension: "zip"
- artifactId: "kietmyartifact-properties"
version: "5.0.0"
extension: "jar"
- artifactId: "kiatmyartifact-properties"
version: "5.0.0"
extension: "jar"
- artifactId: "kiprmyartifact-properties"
version: "5.0.0"
extension: "jar"

tasks:
- set_fact:
bundle_artifacts_without_non_current_env_properties: "{{ (bundle_artifacts_without_non_current_env_properties | default([])) + [{'artifactId':item.artifactId, 'version':item.version,'extension':item.extension}] }}"
with_items: "{{ bundle_artifacts }}"
when: ('"{{ product }}-properties" not in item.artifactId') or ('"{{ env }}{{ product }}-properties" in item.artifactId')



env is a property that depends on the host the execution is for. Values of env could be: et, at, qs or pr.
product is a property that depends on the host the execution is for too. Assume the value is here: myartifact


Now my problem is that after executing the task above bundle_artifacts_without_non_current_env_properties contains the same elements like bundle_artifacts.

I would like to have the following artifacts in bundle_artifacts_without_non_current_env_properties if product was "myartifact" and env was "at":

someArtifact and kiatmyartifact-properties.

The artifacts kietmyartifact-properties and kiprmyartifact-properties should be ignored.


Does anybody know what I have to do to get this work?

Regards,
Tom

J Hawkesworth

unread,
Sep 5, 2017, 10:03:29 AM9/5/17
to Ansible Project
I am not sure I have fully understood your example but if the goal is to install someArtifact and a corresponding someArtifact-properties, but also allow for there not to be a someArtifact-properties, (in which case you would just install 'someArtifact', then you might be able to make use of default(omit) syntax - see the example here:  http://docs.ansible.com/ansible/latest/playbooks_filters.html#omitting-parameters

Hope this helps,

Jon

Tom Bartsch

unread,
Sep 6, 2017, 2:31:19 AM9/6/17
to Ansible Project
Hi,

it's quite simple what I would like to do.

Like described in the yaml code of my question:

I have a list of artifacts. Now I want to make a new list of artifacts. The new list has to be only a subset of the original list.
Every host has a variable called env and every host has a variable called product.

Let's assume for this example that product is "myartifact" and env is "at".
So the new list should only contain the item with the artifactId someArtifact and the item with the artifactId kiatmyartifact-properties because the 2 chars of env (here: at) are matching the third and fourth char of kiatmyartifact-properties which is not satisfied by the artifactIds kietmyartifact-properties and kiprmyartifact-properties.

So the condition for putting an item into the new list should be:


{{ product }}-properties not in item.artifactId
or
{{ env }}{{ product }}-properties in item.artifactId

That's all.

Regards,
Tom

Tom Bartsch

unread,
Sep 7, 2017, 10:47:14 AM9/7/17
to Ansible Project
Has anybody an idea or is this impossible?

James Tanner

unread,
Sep 7, 2017, 10:57:10 AM9/7/17
to ansible...@googlegroups.com
Calling set_fact with an item loop is not going to slice the original list. Your best best is to make a custom filter plugin to handle this.

An example might look like:

- set_fact:
     newlist: "{{ bundle_artifacts|artifact_reduce(product, env) }}"




On Thu, Sep 7, 2017 at 10:47 AM, 'Tom Bartsch' via Ansible Project <ansible...@googlegroups.com> wrote:
Has anybody an idea or is this impossible?

--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/baae668e-5ee9-4a7e-89b3-f6a5ba8da10e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Tom Bartsch

unread,
Oct 18, 2017, 8:50:58 AM10/18/17
to Ansible Project
Hi,

ok now I try to develop a custom filter. Now my problem is that I am not very familiar with python.

What I try to do is to handover Python an multimensional array of artifacts and a string variable named env which contains for example "at".
Each element of the array has 3 keys (artifactId, version and extension) with respectively 3 values.

Then I like to iterate over this array and find out which of the array elements has an artifactId that contains "atmyartifact-properties".
If the artifactId of an element contains "atmyartifact-properties" I like to add it to a new array.

That's all.
I hope someone with Python skills could give me a code snippet to put my plan into action? 



Am Donnerstag, 7. September 2017 16:57:10 UTC+2 schrieb James Tanner:
Calling set_fact with an item loop is not going to slice the original list. Your best best is to make a custom filter plugin to handle this.

An example might look like:

- set_fact:
     newlist: "{{ bundle_artifacts|artifact_reduce(product, env) }}"



On Thu, Sep 7, 2017 at 10:47 AM, 'Tom Bartsch' via Ansible Project <ansible...@googlegroups.com> wrote:
Has anybody an idea or is this impossible?

--
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.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages