0

The ansible_local var is not being populated when using the setup module to gather custom facts on windows.

Playbook:

---
- name: Testing and Debug PB
  hosts: all
  tasks:
  
    - name: Gather Facts
      setup:
        fact_path: 'c:\custom_facts'

    - debug:
        msg: "{{ hostvars[inventory_hostname] }}"

    - debug:
        msg: "{{ ansible_local }}"

Contents of "C:\custom_facts\foo.fact" on remote host:

[general]
asdf=1
bar=2

Playbook Output:

ansible-playbook -i inv/dev 301_pb_testing.yml -vv
ansible-playbook 2.9.12
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/alex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.6.9 (default, Jul 17 2020, 12:50:27) [GCC 8.4.0]
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: 301_pb_testing.yml ************************************************************************************************************************************************1 plays in 301_pb_testing.yml

PLAY [Testing and Debug PB] *************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************task path: /home/alex/git.sigma/SDG-Tech-DevOps/ansible/301_pb_testing.yml:2
ok: [demi-win00]
META: ran handlers

TASK [facts : Gather Facts] *************************************************************************************************************************************************task path: /home/alex/git.sigma/SDG-Tech-DevOps/ansible/roles/facts/tasks/main.yml:14
ok: [demi-win00]

TASK [debug] ****************************************************************************************************************************************************************task path: /home/alex/git.sigma/SDG-Tech-DevOps/ansible/301_pb_testing.yml:15

---------REMOVED SOME HOSTVAR OUTPUT FOR BREVITY AND PRIVACY--------

ok: [demi-win00] => 
  msg: |-
    ansible_architecture: 64-bit
    ansible_bios_date: 03/31/2014
    ansible_bios_version: rel-1.13.0-0-gf21b5a4aeb02-prebuilt.qemu.org
    ansible_check_mode: false
    ansible_connection: winrm
    ansible_date_time:
  ......
    ansible_facts:
   ......
    ansible_kernel: 10.0.17763.0
    ansible_lastboot: '2020-08-29 11:07:46Z'
    ansible_machine_id: S-1-5-21-49790847-4020553596-361474921
   ......

---------REMOVED SOME HOSTVAR OUTPUT FOR BREVITY AND PRIVACY--------

TASK [debug] ****************************************************************************************************************************************************************task path: /home/alex/git.sigma/SDG-Tech-DevOps/ansible/301_pb_testing.yml:18
fatal: [demi-win00]: FAILED! => 
  msg: |-
    The task includes an option with an undefined variable. The error was: 'ansible_local' is undefined
  
    The error appears to be in '/home/alex/git.sigma/SDG-Tech-DevOps/ansible/301_pb_testing.yml': line 18, column 7, but may
    be elsewhere in the file depending on the exact syntax problem.
  
    The offending line appears to be:
  
  
        - debug:
          ^ here
ansible 2.9.12
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/alex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.9 (default, Jul 17 2020, 12:50:27) [GCC 8.4.0]

The ansible Controller is running debian, The remote is Windows Server 2019

I would expect ansible_local to be populated once setup is run over a valid custom fact, but It doesn't. What am I doing wrong?

1

According to documentation:

NOTE - For windows clients, the results will be added to a variable named after the local file (without extension suffix), rather than ansible_local.

According to your example it should be in ansible_foo, but take into consideration that the files in the path must be PowerShell scripts (with extension .ps1) so maybe foo.fact wasn't actually executed.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.