Changing `fact_path` in `ansible.cfg` does nothing

74 views Asked by At

I am trying to change the default path of /etc/ansible/facts.d for storing Custom Facts to a different directory. As of now, if I store my Custom Facts in this path, I can retrieve them along with the default Ansible Facts in the output of

ansible myhost -m setup | less

There is nothing wrong with the Custom Facts and I can see the expected output.

However, if I add the Custom Facts to a different directory, as explained in the documentation, called /home/ansible/facts.d/custom.fact and define its path in the /etc/ansible/ansible.cfg by adding fact_path=/home/ansible/facts.d/ to it, I can no longer see the Custom Facts in the output of ansible myhost -m setup | less.

My ansible.cfg now contains the following:

# (string) This option allows you to globally configure a custom path for 'local_facts' for the implied :ref:`ansible_collections.ansible.builtin.setup_module` task when using fact gathering.
# If not set, it will fallback to the default from the ``ansible.builtin.setup`` module: ``/etc/ansible/facts.d``.
# This does **not** affect  user defined tasks that use the ``ansible.builtin.setup`` module.
# The real action being created by the implicit task is currently    ``ansible.legacy.gather_facts`` module, which then calls the configured fact modules, by default this will be ``ansible.builtin.setup`` for POSIX systems but other platforms might have different defaults.

fact_path='/home/ansible/facts.d/'

I have also tried removing the single-quotes, replacing this path with ~/facts.d/ and $HOME/facts.d/ but nothing worked.

I also tried defining fact_path=/home/ansible/facts.d/ explicitly in my playbook. However this has not worked out. The playbook now starts in the following way:

---
- hosts: kna
  become: yes
  ignore_errors: no
  fact_path: /home/ansible/AnsibleCustomFacts/facts.d/
  gather_facts: yes

# continued playbook

How do I change the fact_path so that I would be able to get get the combined custom and default facts in the output of ansible mygroup -m setup | less?

1

There are 1 answers

1
U880D On

With an ansible.cfg file

[defaults]

inventory               = ./host
fact_path               = /home/ansible_user/test/facts
# fact_path               = /etc/ansible/facts.d
fact_caching            = yaml
fact_caching_connection = /tmp/ansible/facts_cache
fact_caching_timeout    = 129600

an existing and accessible Custom Fact path /home/ansible_user/test/facts and an Custom Fact file test.fact with content of

{
  "custom": "ishere"
}

a minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: true

  tasks:

  - debug:
      var: ansible_local

will result into an output of

TASK [debug] *******
ok: [localhost] =>
  ansible_local:
    test:
      custom: ishere

as well an ad-hoc command of

ansible localhost --module-name setup --args 'fact_path=/home/ansible_user/test/facts filter=ansible_local'

will result into an output of

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_local": {
            "test": {
                "custom": "ishere"
            }
        }
    },
    "changed": false
}

Summary

In an ad-hoc command one will need to specify the module configuration and parameter arguments separately again, otherwise a command

ansible localhost --module-name setup --args 'filter=ansible_local'

will result into an output of

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_local": {
            "default": {
                "custom": "indefault"
            }
        }
    },
    "changed": false
}

if there would be the respective Custom Fact file, or just only

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_local": {}
    },
    "changed": false
}

Documentation

Similar Q&A