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
}
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”] }}
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 ‘{’):
# 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'