Created
December 19, 2018 15:30
-
-
Save maxamillion/00bbf799900461ac8c5dcbfbbdb7961b to your computer and use it in GitHub Desktop.
Ansible filtering facts with templates instead of json_query filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 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