Further note: just check the timestamp on the commit that introduced
the 'equalto' test (2013-11-22),
so that might not be in the version you are using.

K

Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



On Thu, Jan 2, 2014 at 10:48 AM, Kahlil Hodgson
<[email protected]> wrote:
> Hi Anand,
>
> Looking at the documentation again, it seems I misinterpreted: i did
> not realise that 'none' is a jinja test not a value.  It seems the
> second argument to selectattr is a test (see the last line of the
> backtrace), which makes more sense now that I think of it.
>
> Not sure how to insert an equality test into this using jinja
> constructs.  Docs do not show a builtin equality test.  Hmm had a
> quick dive into the jinja2 code on git hub and found this gem about
> the undocumented 'equalto' test
>
> ```
>
>     This appears to be a useless test as it does exactly the same as the
>     ``==`` operator, but it can be useful when used together with the
>     `selectattr` function:
>
>     .. sourcecode:: jinja
>
>         {{ users|selectattr("email", "equalto", "[email protected]") }}
>
> ```
>
> So I think the following might work:
>
> {{  ... | selectattr('scope', 'equalto', 'local') | ... }}
>
>
> Hope this helps,
>
>
> K
>
> Kahlil (Kal) Hodgson                       GPG: C9A02289
> Head of Technology                         (m) +61 (0) 4 2573 0382
> DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281
>
> Suite 1415
> 401 Docklands Drive
> Docklands VIC 3008 Australia
>
> "All parts should go together without forcing.  You must remember that
> the parts you are reassembling were disassembled by you.  Therefore,
> if you can't get them together again, there must be a reason.  By all
> means, do not use a hammer."  -- IBM maintenance manual, 1925
>
>
>
> On Thu, Jan 2, 2014 at 7:01 AM, Anand Buddhdev <[email protected]> wrote:
>> On Tuesday, 31 December 2013 05:31:18 UTC+1, Kahlil Hodgson wrote:
>>
>> Hi Kahlil,
>>
>>> In Jinja2.7 you have the following filters that can be used to traverse or
>>> extract parts of lists:
>>>
>>> select
>>> selectattr
>>> reject
>>> rejectattr
>>> map
>>>
>>> so I believe that following would work
>>>
>>> {{ ansible_em1.ipv6 | selectattr('scope', 'local') | first |
>>> attr('address') }}
>>>
>>> just off the top of my head and untested.
>>
>> Your example looks logical, and looks like it should work, but I get this
>> error when trying to use it:
>>
>> TASK: [debug msg='{{ansible_em1.ipv6 | selectattr("scope", "local") | first
>> | attr("address")}}'] ***
>>
>> fatal: [ns1.ams.authdns.ripe.net] => Traceback (most recent call last):
>>
>>   File
>> "/Users/anandb/git/gii-ansible/portable/lib/ansible/runner/__init__.py",
>> line 394, in _executor
>>
>>     exec_rc = self._executor_internal(host, new_stdin)
>>
>>   File
>> "/Users/anandb/git/gii-ansible/portable/lib/ansible/runner/__init__.py",
>> line 485, in _executor_internal
>>
>>     return self._executor_internal_inner(host, self.module_name,
>> self.module_args, inject, port, complex_args=complex_args)
>>
>>   File
>> "/Users/anandb/git/gii-ansible/portable/lib/ansible/runner/__init__.py",
>> line 679, in _executor_internal_inner
>>
>>     module_args = template.template(self.basedir, module_args, inject,
>> fail_on_undefined=self.error_on_undefined_vars)
>>
>>   File
>> "/Users/anandb/git/gii-ansible/portable/lib/ansible/utils/template.py", line
>> 319, in template
>>
>>     varname = template_from_string(basedir, varname, vars,
>> fail_on_undefined)
>>
>>   File
>> "/Users/anandb/git/gii-ansible/portable/lib/ansible/utils/template.py", line
>> 539, in template_from_string
>>
>>     res = jinja2.utils.concat(rf)
>>
>>   File "<template>", line 12, in root
>>
>>   File "/Users/anandb/git/gii-ansible/portable/lib/jinja2/filters.py", line
>> 349, in do_first
>>
>>     return next(iter(seq))
>>
>>   File "/Users/anandb/git/gii-ansible/portable/lib/jinja2/filters.py", line
>> 931, in _select_or_reject
>>
>>     if modfunc(func(transfunc(item))):
>>
>>   File "/Users/anandb/git/gii-ansible/portable/lib/jinja2/filters.py", line
>> 925, in <lambda>
>>
>>     name, item, args, kwargs)
>>
>>   File "/Users/anandb/git/gii-ansible/portable/lib/jinja2/environment.py",
>> line 438, in call_test
>>
>>     raise TemplateRuntimeError('no test named %r' % name)
>>
>> TemplateRuntimeError: no test named 'local'
>>
>> I tried to use Google to look for examples on how to use selectattr, but
>> found nothing useful. The Jinja2 documentation for this filter is quite
>> sparse. If anyone has any more suggestions, I'd be grateful.
>>
>> Anand
>>
>> --
>> 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].
>> For more options, visit https://groups.google.com/groups/opt_out.

-- 
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].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to