EnemyBagJones
EnemyBagJones

Reputation: 982

hostvars for localhost (Ansible master) only grabbing a few facts

I have this line in my playbook:

- debug: msg="{{ hostvars['127.0.0.1'] }}"

and for some reason it only grabs a handful of facts, primarily around setup of the master.

- hosts: mfg-pc:master
  become: yes
  become_method: sudo
  gather_facts: True
  tasks:
    - debug: msg="{{ hostvars['127.0.0.1'] }}"

An example of the facts gathered:

{
    "msg": {
        "ansible_check_mode": false,
        "ansible_connection": "local",
        "ansible_python_interpreter": "/usr/bin/python",
        "ansible_version": {
            "full": "2.2.0.0",
            "major": 2,
            "minor": 2,
            "revision": 0,
            "string": "2.2.0.0"
        },
        "group_names": [
            "ungrouped"
        ],
        "groups": {
            "all": [

Unfortunately, it doesn't grab the full list of variables that I get with:

ansible -m setup 127.0.0.1

I need the full list of facts as I'm trying to utilize the host (Ansible master) time to time-stamp a file I generate. This behavior seems inconsistent with another machine I was running it on, and I can't for the life of me determine why. Any guidance would be appreciated.

Upvotes: 0

Views: 9146

Answers (2)

techraf
techraf

Reputation: 68489

You don't gather facts from localhost in your playbook so you shouldn't expect Ansible to have them.

You must add a play which will execute setup (implicitly) against your localhost, for example:

---
- hosts: localhost
  connection: local

- hosts: mfg-pc:master
  become: yes
  become_method: sudo
  gather_facts: True
  tasks:
    - debug: msg="{{ hostvars['127.0.0.1'] }}"

Upvotes: 3

Nelson G.
Nelson G.

Reputation: 5441

Check parameter gather_subset into your configuration file ansible.cfg. To returns all facts it must be set to all:

gather_subset = all

If needed, to localize your configuration file ansible.cfg, type:

ansible --version

It must returns a line like this :

config file = /home/.../ansible.cfg

Upvotes: 0

Related Questions