New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support omit within lists #58587
Comments
Files identified in the description: If these files are inaccurate, please update the |
We are conflicted about adding this feature, and it has come up in the past. Should you want to pursue this further, please add this to the core IRC meeting agenda for additional discussion: https://github.com/ansible/community/issues?utf8=✓&q=is%3Aissue+is%3Aopen+label%3Ameeting_agenda+label%3Acore Example PR from a previous attempt: #39179 |
!component =lib/ansible/executor/task_executor.py |
Files identified in the description: If these files are inaccurate, please update the |
You can already do this: - win_feature:
name: '{{ namestuff|reject('equalto', omit)|list }}'
state: absent
vars:
namestuff:
- "{% if not fs_installed_smb_v1|default(false) %}FS-SMB1{% else %}{{ omit }}{% endif %}"
- "foo"
- "bar" |
From IRC discussion; I think this should work: - win_feature:
name: "{{ name_list | filter("equal_to", omit) | list }}"
state: absent
vars:
name_list:
- "{% if not fs_installed_smb_v1|default(false) %}FS-SMB1{% else %}{{ omit }}{% endif %}"
- "foo"
- "bar" |
another way, construct it w/o omited - set_fact:
namestuff: ' {{ namestuff|default([])|union([item]) }}'
when: item != omit
loop:
- "{% if not fs_installed_smb_v1|default(false) %}FS-SMB1{% else %}{{ omit }}{% endif %}"
- "foo"
- "bar"
- win_feature: name={{namestuff}} |
closing as per meeting minutes, several ways to achieve this already exist and only feature missing would be recursive data structure filtering, which would be a reason for a filter, but not putting this in core. |
Would be nice if some of the tricks mentioned here could end up somewhere in the docs. I mean in particular the |
SUMMARY
omit should also be supported within lists to allow jinja2 expressions to add elements to lists.
ISSUE TYPE
COMPONENT NAME
omit
core
ADDITIONAL INFORMATION
As an administrator I want to have conditional elements in a task, so that I can simplify my playbook and roles and have less module invocations and a faster run.
The first example currently produces the following python list: [ '__omit_place_holder__375d4b174292829ef0a2d0878acbe8842696711d' ], therefore module execution will fail, as the omit place holder string gets passed onto the module.
The second example instead produces [''], and module execution will fail, because of the empty string element.
And the third one works as expected, but is not very intuitive and lists with more parameters tend to get messy really quickly, like:
Also they are prown for race conditions like in this example if gitlab_expose_ssh is false but any of the following gitlab_expose_http, gitlab_expose_https, gitlab_expose_prometheus is true, it will fail, as all of these elements have a prepended semicolon. Sure, that one could also be fixed with e.g.
(gitlab_expose_ssh and gitlab_expose_http) | ternary(',','')
, but even that would not cover all cases and more or less considerable as write only code (you can write it, but shortly after you're unable to read it again).The text was updated successfully, but these errors were encountered: