# Filtering local facts

**URL:** <https://forum.ansible.com/t/filtering-local-facts/16949>\
**Category:** Ansible Project\
**Created:** [December 3, 2014, 4:20pm UTC](https://forum.ansible.com/t/filtering-local-facts/16949 "2014-12-03T16:20:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_Kasten](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Rick\_Kasten](https://forum.ansible.com/u/Rick_Kasten)\
**Post date:** [December 3, 2014, 4:20pm UTC](https://forum.ansible.com/t/filtering-local-facts/16949/1 "2014-12-03T16:20:35Z")

</div>

```auto
I am writing facts on my targets that I want to collect using ansible. In this example, I have a facts.d/prefs.fact and a facts.d/java.fact. I can see this works:

# ansible tomcat_servers -i inventory -m setup -a 'filter=ansible_local'
tomcat02 | success >> {
    "ansible_facts": {
        "ansible_local": {
            "prefs": {
                "java": {
                    "java_home": "/com/envs/inventory/tomcat02/java/jdk1.7.0_55",
                    "java_version": "jdk1.7.0_55"
                }
            },
            "java": {
                "java": 
                    "JAVA_HOME": "/com/envs/inventory/tomcat02/java/jdk1.7.0_55",
                    "java_version": "jdk1.7.0_55"
                }
            }
        }
    },
    "changed": false
}

But how do I get only the contents of facts.d/java.fact? These examples all fail with the same results:

# ansible tomcat_servers -vvv -i inventory -m setup -a 'filter=ansible_local[java]'
# ansible tomcat_servers -vvv -i inventory -m setup -a 'filter=ansible_local.java'
# ansible tomcat_servers -vvv -i inventory -m setup -a 'filter=ansible_local["java"]'
# ansible tomcat_servers -vvv -i inventory -m setup -a 'filter=ansible_local.java.JAVA_HOME'
tomcat02 | success >> {
    "ansible_facts": {},
    "changed": false
}

```

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [December 3, 2014, 4:45pm UTC](https://forum.ansible.com/t/filtering-local-facts/16949/2 "2014-12-03T16:45:46Z")

</div>

Filtering isn’t very deep, but that’s not really the point of the feature. Basically the filter is there for humans doing browsing, not the API or scripts.

In templates, you would just access the variable as normal.

{{ ansible\_local.java.JAVA\_HOME }}

or equivalently:

{{ ansible\_local[“java”][“JAVA\_HOME”] }}

---

<div class="post-metadata">

**Author:** ![Rick\_Kasten](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Rick\_Kasten](https://forum.ansible.com/u/Rick_Kasten)\
**Post date:** [December 3, 2014, 5:29pm UTC](https://forum.ansible.com/t/filtering-local-facts/16949/3 "2014-12-03T17:29:51Z")

</div>

I totally understand and agree, but I am a human doing browsing.

I figured it out using _jq_ (you need _sed_ to cut out the "tomcat02 | success \>\> " portion before the first ‘{’):

```auto
# ansible tomcat_servers -i inventory -m setup -a 'filter=ansible_local' | sed 's/.*>> //' | jq -r '.[][]["java"]'
{
  "java": {
    "java_version": "jdk1.7.0_55",
    "JAVA_HOME": "/axeda/envs/rick_6.8.0.7/tomcat02/java/jdk1.7.0_55"
  }
}

The *jq* filter '.[][]["java"]' reduces the output to just the contents of "ansible_local", since we're already filtering for 'filter=ansible_local'

```
