Example 13
hosts.yml
centos:
hosts:
provisioner:
ansible_connection: local
centos1:
ansible_host: '192.168.77.22'
ubuntu:
hosts:
ubuntu1:
ansible_host: '192.168.77.23'
files/samplefacts1.fact
#!/bin/bash
printf '{"kernel_package": "%s"}\n' "$(uname -r)"
files/samplefacts2.fact
#!/bin/bash
echo [kernel_package]
echo package=`uname -r`
playbook.yml
- name: Example playbook
hosts: provisioner
fact_path: "{{ playbook_dir }}/files"
tasks:
- debug:
msg: "{{ ansible_local.samplefacts1.kernel_package }}"
- debug:
msg: "{{ ansible_local.samplefacts2.kernel_package.package }}"
Tip
Your custom facts can be executable scripts providing that they produce a valid JSON or INI output.
Execute the following command
ansible-playbook playbook.yml
Bonus round
Ansible looks for custom facts in /etc/ansible/facts.d on the remote hosts. Create a playbook that will copy one of the facts to centos1 and story it in the aformentioned directory as an executable under the name kernel.fact. Use the file module to make sure that the necessary directory exists and the copy module to send the fact file.