Skip to content

Instantly share code, notes, and snippets.

@DrPsychick
Last active September 2, 2018 01:24
Show Gist options
  • Save DrPsychick/12cbee2b36c82ef8441ef00af47fcadf to your computer and use it in GitHub Desktop.
Save DrPsychick/12cbee2b36c82ef8441ef00af47fcadf to your computer and use it in GitHub Desktop.
ansible dict filter with attribute name same as built-in method
# 'values' is a built-in method and therefore always exists. This is especially useful for "influxDB" API output from an uri command.
# test attribute type with selectattr('fieldname', TEST), see http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-tests
ansible localhost -m command -a "echo {{ [ { 'values': 'foo' }, { 'name': 'bar' } ] |selectattr('values', 'string') |map(attribute='values') |list }}"
# alternatively filter:
ansible localhost -m command -a "echo {{ [ { 'values': 'foo' }, { 'name': 'bar' } ] |rejectattr('values', 'callable') |map(attribute='values') |list }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment