ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
823 stars 1.52k forks source link

Hiera lookup failed due to hiera binary not found #4462

Open hungtran84 opened 2 years ago

hungtran84 commented 2 years ago

Summary

I tried to lookup for my value in puppet master with the sample playbook as below

---
- name: Test hiera plugins
  gather_facts: true
  hosts: puppet

  tasks:
    - name: retrieve password from puppet
      become: true
      run_once: true
      environment:
        ANSIBLE_HIERA_BIN: '/opt/puppetlabs/bin/hiera'
        ANSIBLE_HIERA_CFG: '/etc/puppetlabs/code/environments/production/hiera.yaml'
      debug:
        msg: "{{ lookup('community.general.hiera', 'foo') }}"

And get this error

ASK [retrieve password from puppet] ********************************************************************************
fatal: [peu1ew1lnoc001]: FAILED! => 
  msg: 'An unhandled exception occurred while running the lookup plugin ''community.general.hiera''. Error was a <class ''FileNotFoundError''>, original message: [Errno 2] No such file or directory: b''/usr/bin/hiera'''

Issue Type

Bug Report

Component Name

hiera lookup

Ansible Version

$ ansible --version
2.10.8

Community.general Version

$ ansible-galaxy collection list community.general
# /usr/local/lib/python3.9/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 2.5.1  

# /Users/hungts/.ansible/collections/ansible_collections
Collection        Version
----------------- -------
community.general 4.6.1  

Configuration

$ ansible-config dump --only-changed

OS / Environment

Centos 7

Steps to Reproduce

---
- name: Test hiera plugins
  gather_facts: true
  hosts: puppet

  tasks:
    - name: retrieve password from puppet
      become: true
      run_once: true
      environment:
        ANSIBLE_HIERA_BIN: '/opt/puppetlabs/bin/hiera'
        ANSIBLE_HIERA_CFG: '/etc/puppetlabs/code/environments/production/hiera.yaml'
      debug:
        msg: "{{ lookup('community.general.hiera', 'foo') }}"

Expected Results

I expected to get the value of the key

Actual Results

TASK [retrieve password from puppet] ********************************************************************************
fatal: [peu1ew1lnoc001]: FAILED! => 
  msg: 'An unhandled exception occurred while running the lookup plugin ''community.general.hiera''. Error was a <class ''FileNotFoundError''>, original message: [Errno 2] No such file or directory: b''/usr/bin/hiera'''

Code of Conduct

ansibullbot commented 2 years ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

felixfontein commented 2 years ago

!component =plugins/lookup/hiera.py

ansibullbot commented 2 years ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 2 years ago

cc @jparrill click here for bot help

felixfontein commented 2 years ago

If your hiera binary is located somewhere else, you should point the environment variable ANSIBLE_HIERA_BIN to the correct position (see the plugin's documentation).

hungtran84 commented 2 years ago

@felixfontein yes, I already set those environment variable in my sample playbook above but somehow, lookup plugins doesn't pick that up.

tasks:
    - name: retrieve password from puppet
      become: true
      run_once: true
      environment:
        ANSIBLE_HIERA_BIN: '/opt/puppetlabs/bin/hiera'
        ANSIBLE_HIERA_CFG: '/etc/puppetlabs/code/environments/production/hiera.yaml'
      debug:
        msg: "{{ lookup('community.general.hiera', 'foo') }}"
felixfontein commented 2 years ago

AFAIK environment: affects only modules, but not other plugins (like lookups). You have to set a proper environment variable before calling ansible-playbook. Especially when looking at the code of this lookup, the way it is written it picks up the environment variables on load time. I guess it should be adjusted to the Ansible config system; then it's also easy to add support for other ways of configuring it (for example by Ansible variables).