1

What causes facts to become unavailable within a playbook? I'm trying to access Ansible's ansible_date_time fact, but I can't seem to figure out how to access it. Following Ansible date variable, it should simply be available in a playbook such as:

---
# test.yml
- hosts: localhost
  tasks:
    - debug: var=ansible_date_time

Which when run as:

ansible-playbook test.yml

Should produce the output:

PLAY [localhost] **************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [debug var=ansible_date_time] *******************************************
ok: [localhost] => {
    "ansible_date_time": {
        "date": "2015-07-09",
        "day": "09",
        "epoch": "1436461166",
        "hour": "16",
        "iso8601": "2015-07-09T16:59:26Z",
        "iso8601_micro": "2015-07-09T16:59:26.896629Z",
        "minute": "59",
        "month": "07",
        "second": "26",
        "time": "16:59:26",
        "tz": "UTC",
        "tz_offset": "+0000",
        "weekday": "Thursday",
        "year": "2015"
    }
}

PLAY RECAP ********************************************************************
localhost      : ok=2    changed=0    unreachable=0    failed=0

However when I run the playbook, I receive:

PLAY [localhost] ************************************************************** 

TASK: [debug var=ansible_date_time] ******************************************* 
ok: [localhost] => {
    "var": {
        "ansible_date_time": "ansible_date_time"
    }
}

PLAY RECAP ******************************************************************** 
localhost                  : ok=1    changed=0    unreachable=0    failed=0   

What would cause ansible_date_time to be unavailable?


Update: The contents of /etc/ansible/ansible.cfg is:

[defaults]
sudo_user=root
gathering=explicit
Community
  • 1
  • 1
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
  • The playbook works as intended for me. Did you change the ansible.cfg? – ThoFin Jul 16 '15 at 10:01
  • @ThoFin I checked *ansible.cfg* and it looks like that's the culprit. Of course the documentation doesn't mention the *gathering* setting in [Turning Off Facts](https://docs.ansible.com/playbooks_variables.html#turning-off-facts). – Uyghur Lives Matter Jul 16 '15 at 14:25
  • @ThoFin If you post an answer about make sure *gathering* is not set to *explicit* in *ansible.cfg*, I'll mark it as the accepted answer. – Uyghur Lives Matter Jul 16 '15 at 14:30

1 Answers1

1

The ansible.cfg file contains a setting for gathering of facts.
The documentation is here: gathering
implicit is default and will gather facts.
explicit will change the behaviour.
Both settings can be overwritten in a playbook.

ThoFin
  • 1,467
  • 14
  • 20