Ansible date variable

Question:

I’m trying to learn how to use Ansible facts as variables, and I don’t get it. When I run…

…it lists all of the facts of my system. I selected one at random to try and use it, ansible_facts.ansible_date_time.date, but I can’t figure out HOW to use it. When I run…

So, it’s CLEARLY there. But when I run…

What am I not getting here? How do I use Facts as variables?

Answer:

The command ansible localhost -m setup basically says “run the setup module against localhost”, and the setup module gathers the facts that you see in the output.

When you run the echo command these facts don’t exist since the setup module wasn’t run. A better method to testing things like this would be to use ansible-playbook to run a playbook that looks something like this:


Because this runs as a playbook facts for localhost are gathered before the tasks are run. The output of the above playbook will be something like this:

Leave a Reply