saltstack-formulas / salt-formula

Yes, Salt can Salt itself!
http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html
Other
197 stars 423 forks source link

[BUG] salt['cmd.run']("id -un") failed on Windows 10 #495

Closed kartnico closed 3 years ago

kartnico commented 3 years ago

Formula commit hash / release tag

commit a1b488790ee189c37e2a50de33def43356fb282a Author: semantic-release-bot semantic-release-bot@martynus.net Date: Wed Dec 23 18:54:47 2020 +0000

Versions reports (master & minion)

minion :

Salt Version:
          Salt: 3002.2

Dependency Versions:
          cffi: 1.12.2
      cherrypy: 17.4.1
      dateutil: 2.8.0
     docker-py: Not Installed
         gitdb: 2.0.5
     gitpython: Not Installed
        Jinja2: 2.10.1
       libgit2: Not Installed
      M2Crypto: Not Installed
          Mako: 1.0.7
       msgpack: 1.0.0
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: 2.19
      pycrypto: Not Installed
  pycryptodome: 3.9.8
        pygit2: Not Installed
        Python: 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]
  python-gnupg: 0.4.4
        PyYAML: 5.3.1
         PyZMQ: 18.0.1
         smmap: 2.0.5
       timelib: 0.2.4
       Tornado: 4.5.3
           ZMQ: 4.3.1

System Versions:
          dist:
        locale: cp1252
       machine: AMD64
       release: 10
        system: Windows
       version: 10 10.0.19041 SP0

master :

Salt Version:
          Salt: 3002.2

Dependency Versions:
          cffi: Not Installed
      cherrypy: Not Installed
      dateutil: Not Installed
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 2.11.1
       libgit2: Not Installed
      M2Crypto: 0.35.2
          Mako: Not Installed
       msgpack: 0.6.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: Not Installed
      pycrypto: Not Installed
  pycryptodome: Not Installed
        pygit2: Not Installed
        Python: 3.6.8 (default, Nov 16 2020, 16:55:22)
  python-gnupg: Not Installed
        PyYAML: 3.13
         PyZMQ: 17.0.0
         smmap: Not Installed
       timelib: Not Installed
       Tornado: 4.5.3
           ZMQ: 4.1.4

System Versions:
          dist: centos 7 Core
        locale: UTF-8
       machine: x86_64
       release: 3.10.0-1160.6.1.el7.x86_64
        system: Linux
       version: CentOS Linux 7 Core

Pillar / config used

salt:
  clean_config_d_dir: False
  minion_remove_config: True
  master_remove_config: True
  py_ver: 'py3'
  install_packages: True
  pygit2: python3-pygit2

  lookup:
    salt-master: 'salt-master'
    salt-minion: 'salt-minion'
    salt-syndic: 'salt-syndic'
    salt-cloud: 'salt-cloud'
    salt-ssh: 'salt-ssh'

  minion:
    backup_mode: minion
    id: {{ grains['id']|lower }}
    schedule:
      - uptodate:
        - function: pkg.uptodate
        - kwargs:
          refresh: True
        - minutes: 1440

Bug details

Describe the bug

---
2020-12-29 07:34:52,754 [salt.state       :3973][CRITICAL][4268] Rendering SLS 'base:salt.minion' failed: Problem running salt function in Jinja template: Unable to run command 'id -un' with the context '{'cwd': 'C:\\Windows\\system32\\config\\systemprofile', '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': 'VM-WINDOWS10-02', 'ComSpec': 'C:\\Windows\\system32\\cmd.exe', 'DriverData': 'C:\\Windows\\System32\\Drivers\\DriverData', 'LOCALAPPDATA': 'C:\\Windows\\system32\\config\\systemprofile\\AppData\\Local', 'NUMBER_OF_PROCESSORS': '2', 'OS': 'Windows_NT', 'Path': 'C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;c:\\salt;C:\\Windows\\system32\\config\\systemprofile\\AppData\\Local\\Microsoft\\WindowsApps', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 6 Stepping 3, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REVISION': '0603', 'ProgramData': 'C:\\ProgramData', 'ProgramFiles': 'C:\\Program Files', 'ProgramFiles(x86)': 'C:\\Program Files (x86)', 'ProgramW6432': 'C:\\Program Files', 'PSModulePath': 'C:\\Program Files\\WindowsPowerShell\\Modules;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': 'VM-WINDOWS10-02$', '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
c:\salt\var\cache\salt\minion\files\base\salt/osfamilymap.yaml(22):
---
[...]
#from template-formula
{%- if grains.os_family == 'MacOS' %}
{%-   set rootuser = salt['cmd.run']("stat -f '%Su' /dev/console") %}
{%-   set rootgroup = salt['cmd.run']("stat -f '%Sg' /dev/console") %}
{%- elif grains.os_family == 'Windows' %}
{%-   set rootuser = salt['cmd.run']("id -un") %}    <======================
{%- endif %}
myii commented 3 years ago

Thanks for the report, @kartnico. As you can see under the Actions tab, we have Windows testing taking place, so I wonder why that's not tripping up on this. In fact, it's picking it up as kitchen:

https://github.com/saltstack-formulas/salt-formula/blob/a1b488790ee189c37e2a50de33def43356fb282a/test/integration/v3000-py3/files/_mapdata/windows-2019-server.yaml#L87

From a quick look around, it appears that the whoami command should work.

https://ss64.com/nt/whoami.html

Equivalent bash command (Linux): whoami - Print the current user id and name (id -un).

Can you try that out and see if it works for you?

kartnico commented 3 years ago

{%- set rootuser = salt['cmd.run']("whoami") %}

works on Windows 10.

myii commented 3 years ago

Tested whoami in GitHub Actions but this causes a different value to be set:

Would need to figure out how to ensure the same value is extracted.

kartnico commented 3 years ago

Could you try this ?

{%- set rootuser = salt['cmd.powershell']("(whoami).split('\')[1]") %}

It works also for me.

myii commented 3 years ago

Could you try this ?

{%- set rootuser = salt['cmd.powershell']("(whoami).split('\')[1]") %}

It works also for me.

@kartnico Due to the nature of cmd.powershell, that ends up returning an empty dict (rootuser: {}). See the following for more info:

Tried various other combinations but finally found one that works properly:

{%-   set rootuser = salt['cmd.run']("whoami").split("\\")[1] %}

That's working on Vagrant (locally) as well as in GitHub Actions:

kartnico commented 3 years ago

It also works for me. Thank you.

dafyddj commented 3 years ago

Closed in error.

myii commented 3 years ago

Thanks for reopening, @dafyddj -- the fix still needs to be provided here, since it was only tested in my fork.

saltstack-formulas-travis commented 3 years ago

:tada: This issue has been resolved in version 1.8.1 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: