saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Install Salt from the Salt package repositories here:
https://docs.saltproject.io/salt/install-guide/en/latest/
Apache License 2.0
14.19k stars 5.48k forks source link

Trying to pass cwd to unless fails on windows #58790

Open markododa opened 4 years ago

markododa commented 4 years ago

I am trying to run cmd.run on windows, which relies on cwd, setting it under cmd.run works just fine, however i can't pass it to unless or onlyif no matter what i try.

  cmd.run:
   - cwd: {{cwd}}
   - unless:
     - fun: cmd.run
       args:
         - keytool -list -keystore "{{cacertspath}}" -storepass {{salt['pillar.get']('java_cacert_pass', 'changeit')}} -alias {{cert_alias}}
         - {{cwd}}

The error i get is

salt.exceptions.CommandExecutionError: Unable to run command 'keytool -list -keystore "C:\Program Files\Java\jre1.8.0_251\lib\security\cacerts" -storepass changeit -alias eyn3nhupzs' with the context '{'cwd': 'C:\\Program Files\\Java\\jre1.8.0_251\\bin\', 'shell': False, 'env': {'ALLUSERSPROFILE': 'C:\ProgramData', 'APPDATA': 'C:\windows\system32\config\systemprofile\AppData\Roaming', 'CommonProgramFiles': 'C:\Program Files\Common Files', 'CommonProgramFiles(x86)': 'C:\Program Files (x86)\Common Files', 'CommonProgramW6432': 'C:\Program Files\Common Files', 'COMPUTERNAME': 'java-test', 'ComSpec': 'C:\windows\system32\cmd.exe', 'FP_NO_HOST_CHECK': 'NO', 'LOCALAPPDATA': 'C:\windows\system32\config\systemprofile\AppData\Local', 'NUMBER_OF_PROCESSORS': '1', 'OS': 'Windows_NT', 'Path': 'C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;c:\salt;', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 79 Stepping 1, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REVISION': '4f01', 'ProgramData': 'C:\ProgramData', 'ProgramFiles': 'C:\Program Files', 'ProgramFiles(x86)': 'C:\Program Files (x86)', 'ProgramW6432': 'C:\Program Files', 'PSModulePath': 'C:\windows\system32\WindowsPowerShell\v1.0\Modules\', 'PUBLIC': 'C:\Users\Public', 'SystemDrive': 'C:', 'SystemRoot': 'C:\windows', 'TEMP': 'C:\windows\TEMP', 'TMP': 'C:\windows\TEMP', 'USERDOMAIN': 'WORKGROUP', 'USERNAME': 'java-test$', 'USERPROFILE': 'C:\windows\system32\config\systemprofile', 'windir': 'C:\windows'}, 'stdin': None, 'stdout': -1, 'stderr': -2, 'with_communicate': True, 'timeout': None, 'bg': False}', reason: [WinError 2] The system cannot find the file specified

markododa commented 4 years ago

Seems like onlyif and unless always fail silently, using the - fun notation only sends the error to the master. Here is salt version from the master:

Salt Version:
           Salt: 3000.3

Dependency Versions:
           cffi: 1.14.3
       cherrypy: Not Installed
       dateutil: 2.7.3
      docker-py: Not Installed
          gitdb: 2.0.5
      gitpython: 2.1.11
         Jinja2: 2.10
        libgit2: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.6.2
   mysql-python: Not Installed
      pycparser: 2.20
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.16 (default, Oct 10 2019, 22:02:15)
   python-gnupg: Not Installed
         PyYAML: 3.13
          PyZMQ: 17.1.2
          smmap: 2.0.5
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.1

System Versions:
           dist: debian 10.6 
         locale: UTF-8
        machine: x86_64
        release: 4.19.0-11-cloud-amd64
         system: Linux
        version: debian 10.6 

Windows minion is installed from Salt-Minion-3001.1-Py3-AMD64-Setup.exe Might be because of python versions difference.

Here is a sample state

just_cd:
  cmd.run:
    - cmd: cd
    - cwd: 'C:\Program Files'

only_if_args:
  cmd.run:
   - cmd: cd
   - cwd: 'C:\Program Files'
   - onlyif:
     - fun: cmd.run
       args:
         - cd 

only_if_short_cwd:
  cmd.run:
   - cmd: cd
   - cwd: 'C:\Program Files'
   - onlyif:
     - fun: cmd.run
       args:
         - cd
         - 'C:\'
cd:
  cmd.run:
   - cwd: 'C:\Program Files'
   - onlyif: cd
cmcmarrow commented 4 years ago

I got onlyif and unless to fail silently too.

max-arnold commented 3 years ago

I'm not 100% sure, but the cwd issue might be fixed in 3002: https://github.com/saltstack/salt/pull/57825