Skip to content

Instantly share code, notes, and snippets.

@maxamillion
Created December 19, 2018 15:30
Show Gist options
  • Save maxamillion/00bbf799900461ac8c5dcbfbbdb7961b to your computer and use it in GitHub Desktop.
Save maxamillion/00bbf799900461ac8c5dcbfbbdb7961b to your computer and use it in GitHub Desktop.
Ansible filtering facts with templates instead of json_query filter
{% set results = {} %}
{% for fact in ansible_facts %}
{% if ansible_facts[fact] is mapping and 'pciid' in ansible_facts[fact] %}
{% set x=results.__setitem__(ansible_facts[fact]['pciid'], ansible_facts[fact]['device']) %}
{% endif %}
{% endfor %}
{{ results|to_json}}
---
- name: test
hosts: localhost
vars:
test_var: "{{ ansible_facts | dict2items | json_query('[?value.pciid].value.{key: pciid, value: device}') | items2dict}}"
tasks:
- set_fact:
test_var2: '{{ lookup("template", "fact_template.j2") | from_json }}'
- name: debug output
debug:
var: test_var
- name: debug output
debug:
var: test_var2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment