have you tried the difference filter
- http://docs.ansible.com/ansible/playbooks_filters.html
I'm not sure if you can combine a for loop and an if condition like that, I
would have used something like this
set_fact: instance_ids1="{{ instance_ids | difference(excluded_ids) }}"
or
set_fact: instance_ids1="{% for item in instance_ids %}{% if item not in
excluded_ids %}{{ item }}{% endif %}{% endfor %}"
On Wednesday, July 6, 2016 at 3:29:59 PM UTC+5:30, Cos Bug wrote:
>
> Hi All,
> Enter code here...
>
>
> I'm using a Jinja2 expression to exclude form one list the elements
> present in another list but it doesn't work as expected.
>
> Here is an extract from a larger playbook which captures the input and out
> put before and after 'set_fact' statement:
>
> - debug: var=excluded_ids
>
> - debug: var=instance_ids
>
> - name: Exclude them from instance_ids
> set_fact: instance_ids1="{% for item in instance_ids if item not in
> excluded_ids %}{{ item }}{% endfor %}"
>
> - debug: var=instance_ids1
>
> and here the strange output of *instance_ids1* variable:
>
> TASK: [debug var=excluded_ids] ************************************
> ok: [127.0.0.1] => {
> "var": {
> "excluded_ids": [
> "i-bae34407"
> ]
> }
> }
>
> TASK: [debug var=instance_ids] ************************************
> ok: [127.0.0.1] => {
> "var": {
> "instance_ids": [
> "i-bae34407",
> "i-d2e4436f"
> ]
> }
> }
>
> TASK: [Exclude them from instance_ids] ****************************
> ok: [127.0.0.1] => {"ansible_facts": {"instance_ids1": "*u, ud26f*"}}
>
>
> TASK: [debug var=instance_ids1] ***********************************
> ok: [127.0.0.1] => {
> "var": {
> "instance_ids1": "*u, ud26f*"
> }
> }
>
> As you can see I get some rubbish in *instance_ids1*.
>
> But if I run a separate playbook defined like this:
>
> ---
> #
> - hosts: localhost
> connection: local
> gather_facts: no
>
> vars:
>
> instance_ids:
> - "i-bae34407"
> - "i-d2e4436f"
>
> excluded_ids:
> - "i-bae34407"
>
> tasks:
>
> - debug: var=excluded_ids
>
> - debug: var=instance_ids
>
> - set_fact: instance_ids1="{% for item in instance_ids if item not in
> excluded_ids %}{{ item }}{% endfor %}"
>
> - debug: var=instance_ids1
>
>
> I get the correct/expected behavior:
>
> TASK: [debug var=excluded_ids]
> ************************************************
> ok: [127.0.0.1] => {
> "var": {
> "excluded_ids": [
> "i-bae34407"
> ]
> }
> }
>
> TASK: [debug var=instance_ids]
> ************************************************
> ok: [127.0.0.1] => {
> "var": {
> "instance_ids": [
> "i-bae34407",
> "i-d2e4436f"
> ]
> }
> }
>
> TASK: [set_fact instance_ids1="{% for item in instance_ids if item not in
> excluded_ids %}{{ item }}{% endfor %}"] ***
> ok: [127.0.0.1] => {"ansible_facts": {"instance_ids1": "i-d2e4436f"}}
>
> TASK: [debug var=instance_ids1]
> ***********************************************
> ok: [127.0.0.1] => {
> "var": {
> "instance_ids1": "*i-d2e4436f*"
> }
> }
>
>
>
> Why Ansible / Jinja2 treats these list variables differently ?
> Are they stored internally as different types ?
>
> In debug statements in both cases they appear to be similar (at least
> printing out) ...
>
> Thanks & Regards,
> Constantin
>
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/150322c0-51f7-4424-88f1-903d2d765015%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.