Foreman_salt installation with remote execution fails to import job templates (randomly)

Problem:
During installation of the foreman_salt plugin, on some occasions, the foreman_salt JobTemplates are missing. Manually executing foreman-rake db:seed results in a stack trace (see attachment).

I’m not an expert in this, but what is interesting that there seems to be a dependency between the job templates and if they are imported in the wrong order, the stack trace occurs. E.g. the Salt Run state.highstate - SSH default points to Salt Run function - SSH default as “foreign_input_set”.

Furthermore the seed code in 76-job_templates.rb to import these templates just seem to iterate over the files, without taking this dependency into account.

In fact mimicking the logic seem in the latter file seems to list the files in the reversed order on one host:

$ ruby -e "puts Dir[File.join('app/views/foreman_salt/job_templates/**/*.erb')]"
app/views/foreman_salt/job_templates/salt_run_state_highstate_-_ssh_default.erb
app/views/foreman_salt/job_templates/salt_run_function_-_ssh_default.erb

While it lists them in the working order on another host (where installation works just fine):

$ ruby -e "puts Dir[File.join('app/views/foreman_salt/job_templates/**/*.erb')]"
app/views/foreman_salt/job_templates/salt_run_function_-_ssh_default.erb
app/views/foreman_salt/job_templates/salt_run_state_highstate_-_ssh_default.erb

How is this supposed to work? Is it correct that there is not logic in place to account for the dependencies?

Expected outcome:
The job templates to be imported successfully

Foreman and Proxy versions:
1.23.1

Foreman and Proxy plugin versions:
remote_execution: 1.8.2
foreman_salt: 11.0.1

Distribution and version:
CentOS 7.7

Other relevant data:
seed.log (5.0 KB)

A bugreport was recently written for this: Bug #30661: Rake seed error on fresh installation - Salt - Foreman