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
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
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
Has anybody an idea or is this impossible?
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) }}”
http://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html#filter-plugins
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?