Search code examples
resourceschef-infraexecute

chef execute resource does not working on Rhel 8


I am using execute resource in cookbook recipe. the recipe is working normaly while recipe calling on cli. the recipe does not working schedule with cron. i send error log below;

* execute[DNS Recording with Centrify] action run
    
    ================================================================================
    Error executing action `run` on resource 'execute[DNS Recording with Centrify]'
    ================================================================================
    
    Errno::ENOENT
    -------------
    No such file or directory - addns
    
    Resource Declaration:
    ---------------------
    # In /var/chef/cache/cookbooks/rhel8/recipes/centrify.rb
    
     48:                        execute 'DNS Recording with Centrify' do
     49:                                 command 'addns -U -m'
     50:                                user "root"
     51:                                 ignore_failure false
     52:                        end
     53:                end
    
    Compiled Resource:
    ------------------
    # Declared in /var/chef/cache/cookbooks/rhel8/recipes/centrify.rb:48:in `from_file'
    
    execute("DNS Recording with Centrify") do
      action [:run]
      default_guard_interpreter :execute
      command "addns -U -m"
      declared_type :execute
      cookbook_name "rhel8"
      recipe_name "centrify"
      domain nil
      user "root"
    end

i found the reason. the reason appear baceuse of different PATH. cookbook uses different PATH in the cli and in different cron. Mycommand binary in the cli PATH. i can not use static PATH because of cli PATH changes according to OS, verison and distribution.

how can set cli PATH in the cookbook according to all linux?

Regards


Solution

  • From the question it appears you want to have the respective PATH environment variable available for the execute resource.

    You can try two options:

    1. Set the environment variable PATH while executing command:

      execute 'DNS Recording with Centrify' do
        command 'addns -U -m'
        user 'root'
        environment(
          { 'PATH' => ENV['PATH'] }
        )
      end
      
    2. Or use default_env property of resource to see if makes the PATH available:

      execute 'DNS Recording with Centrify' do
        command 'addns -U -m'
        user 'root'
        default_env true
      end