saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Get access to the Salt software package repository here:
https://repo.saltproject.io/
Apache License 2.0
13.98k stars 5.47k forks source link

[FEATURE REQUEST] Environment specific salt modules #66495

Open jtraub91 opened 1 month ago

jtraub91 commented 1 month ago

Is your feature request related to a problem? Please describe. Salt's fileserver has a concept of environments, which can work decently well to isolate different versions of states and other files to operate on minions under different circumstances. Salt even includes the ability to set the default saltenv and/or lock_saltenv in the minion config. However, one aspect in which this falls short is when you have custom modules in different environments. Currently the minion does not have a concept of a version of a module per environment, and is simply the last one synced to the minion's cache wins. This is especially strange, since salt supports and defaults to syncing modules from multiple environments via the saltutil.sync_modules command. This default behavior does not make sense in the scenario where different versions of modules of the same name exist in multiple environments. The problem is that this could prevent sharing masters / minions in the scenario where they are running jobs simultaneously with different modules for different "environments", in production or development scenarios.

Describe the solution you'd like The solution to this could simply be to have a directory in the minion's module cache, per environment, that it would use based on the saltenv that is active for a given command.

Describe alternatives you've considered There does not seem to be a way to allow for simultaneous executions on the same minion with different salt environments specified specifically when they are attempting to use different versions of the the same module in the different environments, due to the fact that only one can be cached on the minion at a single time.

jtraub91 commented 1 month ago

Consider the following setup:

Master and minion on a single VM.

# /etc/salt/master.d/master.conf
user: root
file_roots:
  base:
    - /srv/salt/base
  dev:
    - /srv/salt/dev
# /etc/salt/minion.d/minion.conf
master: localhost
# /srv/salt/base/echo_module.sls
echo_base:
  cmd.run:
    - name: echo {{ salt.env.tag() }}
# /srv/salt/dev/echo_module.sls
echo_dev:
  cmd.run:
    - name: echo {{ salt.env.tag() }}
# /srv/salt/base/_modules/env.py
def tag():
    return "base_tag"
# /srv/salt/dev/_modules/env.py
def tag():
    return "dev_tag"
# /srv/salt/base/top.sls
base:
  "*":
    - echo_module

dev:
  "*":
    - echo_module
# /srv/salt/dev/top.sls
base:
  "*":
    - echo_module

dev:
  "*":
    - echo_module

Now after syncing modules, both environments will be synced because they are both defined in the top file

# salt \* saltutil.sync_modules
salt-modules-env:
    - modules.env
    - modules.env

And thus only whichever module got cached last will be the one in use, even when specifying different saltenv during the state calls.

root@salt-modules-env:~# salt \* state.sls echo_module saltenv=base
salt-modules-env:
----------
          ID: echo_base
    Function: cmd.run
        Name: echo dev_tag
      Result: True
     Comment: Command "echo dev_tag" run
     Started: 15:53:34.263269
    Duration: 9.128 ms
     Changes:   
              ----------
              pid:
                  12415
              retcode:
                  0
              stderr:
              stdout:
                  dev_tag

Summary for salt-modules-env
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   9.128 ms
root@salt-modules-env:~# salt \* state.sls echo_module saltenv=dev
salt-modules-env:
----------
          ID: echo_dev
    Function: cmd.run
        Name: echo dev_tag
      Result: True
     Comment: Command "echo dev_tag" run
     Started: 15:53:40.182158
    Duration: 18.539 ms
     Changes:   
              ----------
              pid:
                  12535
              retcode:
                  0
              stderr:
              stdout:
                  dev_tag

Summary for salt-modules-env
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  18.539 ms

This feature request is suggesting to have salt use the correct module during each state call, respectively, based on the saltenv specified.

max-arnold commented 1 month ago

Have you tried something like that https://salt.tips/patching-salt-modules/#version-aware-override