Skip to content

[BUG] prereq'd state with onlyif/unless + fun: onlyif requisite ignored #65283

Open

Description

@lkubb

Description
A state that is prerequired and itself has an onlyif (or unless) requisite that calls an execution module will run regardless of outcome of the onlyif evaluation.

Setup

  • on-prem machine
  • VM (Virtualbox, KVM, etc. please specify)
  • VM running on a cloud service, please be explicit and add details
  • container (Kubernetes, Docker, containerd, etc. please specify)
  • or a combination, please be explicit
  • jails if it is FreeBSD
  • classic packaging
  • onedir packaging
  • used bootstrap to install

Steps to Reproduce the behavior

Copy stuff:
  file.copy:
    - name: /tmp/aa
    - source: /tmp/a
    - onlyif:
      - fun: file.file_exists
        path: /tmp/a

Do sth else:
  cmd.run:
    - name: true
    - prereq:
      - Copy stuff

Expected behavior

local:
----------
          ID: Do sth else
    Function: cmd.run
      Result: True
     Comment: No changes detected
     Started: 06:41:33.485261
    Duration: 0.007 ms
     Changes:
----------
          ID: Copy stuff
    Function: file.copy
        Name: /tmp/aa
      Result: True
     Comment: onlyif condition is false
     Started: 06:41:33.485360
    Duration: 8.103 ms
     Changes:

Summary for local
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time:   8.110 ms

Screenshots

[WARNING ] no `fun` argument in onlyif: OrderedDict([('path', '/tmp/a')])
[ERROR   ] Source file "/tmp/a" is not present
local:
----------
          ID: Do sth else
    Function: cmd.run
      Result: True
     Comment: No changes detected
     Started: 06:37:15.121723
    Duration: 0.008 ms
     Changes:
----------
          ID: Copy stuff
    Function: file.copy
        Name: /tmp/aa
      Result: False
     Comment: Source file "/tmp/a" is not present
     Started: 06:37:15.121817
    Duration: 80.259 ms
     Changes:

Summary for local
------------
Succeeded: 1
Failed:    1
------------
Total states run:     2
Total run time:  80.267 ms

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)
Salt Version:
          Salt: 3006.3

Python Version:
        Python: 3.10.13 (main, Sep  6 2023, 02:11:27) [GCC 11.2.0]

Dependency Versions:
          cffi: 1.14.6
      cherrypy: unknown
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 3.1.2
       libgit2: Not Installed
  looseversion: 1.0.2
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.0.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     packaging: 22.0
     pycparser: 2.21
      pycrypto: Not Installed
  pycryptodome: 3.9.8
        pygit2: Not Installed
  python-gnupg: 0.4.8
        PyYAML: 6.0.1
         PyZMQ: 23.2.0
        relenv: 0.13.10
         smmap: Not Installed
       timelib: 0.2.4
       Tornado: 4.5.3
           ZMQ: 4.3.4

System Versions:
          dist: rocky 9.2 Blue Onyx
        locale: utf-8
       machine: x86_64
       release: 5.14.0-284.18.1.el9_2.x86_64
        system: Linux
       version: Rocky Linux 9.2 Blue Onyx

Additional context
The prereq results in the state to be run twice. During the first run, fun (and args) is popped from the passed-in lowstate chunk and is thus unavailable for the second (non-test) run:

salt/salt/state.py

Lines 1028 to 1034 in fb717a8

def _run_check_function(self, entry):
"""Format slot args and run unless/onlyif function."""
fun = entry.pop("fun")
args = entry.pop("args") if "args" in entry else []
cdata = {"args": args, "kwargs": entry}
self.format_slots(cdata)
return self.functions[fun](*cdata["args"], **cdata["kwargs"])

Activity

  1. added
    bug
    on Sep 25, 2023
  2. added
    Requisite
    on Sep 25, 2023
  3. OrangeDog commented on Sep 25, 2023

    @OrangeDog
    Contributor

    The lowstate will need to be deep-cloned at an early stage. Hopefully there isn't a feature that relies on being able to change it globally in a state run...

  4. added this to the Argon v3008 milestone on Sep 25, 2023
  5. changed the title [-][BUG] `prereq`'d state with `onlyif`+ `fun`: onlyif requisite ignored[/-] [+][BUG] `prereq`'d state with `onlyif`/`unless` + `fun`: onlyif requisite ignored[/+] on Nov 1, 2023
  6. Pheric commented on Nov 1, 2023

    @Pheric

    This also occurs when using unless instead of onlyif. Why is this scheduled for Argon?

  7. sticky-note commented on Feb 3, 2024

    @sticky-note

    I think this bugs does not occur any more, the following snippet run correctly in a stateful manner and all cases are correctly covered (watch_in, prereq_in, require_in, unless) :

    {%- set master_modules_deps = ['pygit2', 'python-ldap', 'psycopg2-binary'] %}
    {%- set master_modules_deps_run_deps =  [ "git" ] %}
    {%- set master_modules_deps_build_deps = [ "gcc", "libldap2-dev", "libsasl2-dev" ] %} # <- Debian specific
    
    master_modules_deps_run_deps_pkg_installed:
      pkg.latest:
        - names: {{ master_modules_deps_run_deps }}
        - update: True
    
    master_modules_deps_build_deps_pkg_installed:
      pkg.latest:
        - names: {{ master_modules_deps_build_deps }}
        - update: True
    
    master_modules_deps_downgrade_pip_workaround_pip_installed:
      pip.installed:
        - name: pip==22.3.1
    
    {%- for master_modules_dep in master_modules_deps %}
    master_modules_dep_{{ master_modules_dep }}_pip_installed:
      pip.installed:
        - name: {{ master_modules_dep }}
        - upgrade: True
        - unless:  # dep installed and up to date
          - fun: pip.is_installed
            pkgname: {{ master_modules_dep }}
          - fun: cmd.run
            cmd: ! salt-pip list --outdated | grep {{ master_modules_dep }}
        - prereq_in:
          - pkg: master_modules_deps_run_deps_pkg_installed
          - pkg: master_modules_deps_build_deps_pkg_installed
          - pip: master_modules_deps_downgrade_pip_workaround_pip_installed
        - require_in:
          - pkg: master_modules_deps_build_dep_pkg_removed
        - watch_in:
          - service: salt-master
    {%- endfor %}
    
    master_modules_deps_build_dep_pkg_removed:
      pkg.removed:
        - names: {{ master_modules_deps_build_deps }}
    
    master_pip_pip_update:
      pip.installed:
        - name: pip
        - upgrade: true
        - onlyif:
          - fun: pip.upgrade_available
            pkg: pip
    

    I just get a warning that is irrelevant as the states and logic runs correctly:

    [WARNING ] no `fun` argument in unless: OrderedDict([('pkgname', 'pygit2')])
    [WARNING ] no `fun` argument in unless: OrderedDict([('pkgname', 'python-ldap')])
    [WARNING ] no `fun` argument in unless: OrderedDict([('pkgname', 'psycopg2-binary')])
    

    It is great to finally have this kind of sls run correctly, it wasn't the case few months ago

  8. lkubb commented on Feb 3, 2024

    @lkubb
    ContributorAuthor

    @sticky-note Are you absolutely certain it's running as expected? The error log message is the same as before and is logged just before the instruction is skipped.

  9. sticky-note commented on Feb 4, 2024

    @sticky-note

    In my case, with this code snippet, all cases are handled correctly, I am running:

    salt-call --versions
    Salt Version:
              Salt: 3006.6
     
    Python Version:
            Python: 3.10.13 (main, Nov 15 2023, 04:34:27) [GCC 11.2.0]
     
    Dependency Versions:
              cffi: 1.16.0
          cherrypy: 18.6.1
          dateutil: 2.8.1
         docker-py: Not Installed
             gitdb: Not Installed
         gitpython: Not Installed
            Jinja2: 3.1.3
           libgit2: 1.7.1
      looseversion: 1.0.2
          M2Crypto: Not Installed
              Mako: Not Installed
           msgpack: 1.0.2
      msgpack-pure: Not Installed
      mysql-python: Not Installed
         packaging: 22.0
         pycparser: 2.21
          pycrypto: Not Installed
      pycryptodome: 3.19.1
            pygit2: 1.14.0
      python-gnupg: 0.4.8
            PyYAML: 6.0.1
             PyZMQ: 23.2.0
            relenv: 0.14.2
             smmap: Not Installed
           timelib: 0.2.4
           Tornado: 4.5.3
               ZMQ: 4.3.4
     
    System Versions:
              dist: debian 12 bookworm
            locale: utf-8
           machine: x86_64
           release: 6.1.0-17-amd64
            system: Linux
           version: Debian GNU/Linux 12 bookworm
    

    prereq_in and require_in are correctly run when they should and the (AND) condition on unless also.
    The only case when this code snippets does nothing is when all master_modules_deps are installed AND up to date.

    Hope this is useful

  10. darkpixel commented on Dec 13, 2024

    @darkpixel
    Contributor

    There's definitely something odd with requisites going on in some areas:

    This runs 100% of the time even when Get-PackageProvider returns the NuGet Package Provider info:

    ensure_nuget:
      module.run:
        - psget.bootstrap: []
        - unless:
            - fun: cmd.powershell
              cmd: 'Get-PackageProvider NuGet'
    

    If I switch it to use cmd.run it appears to work fine:

    ensure_nuget:
      module.run:
        - psget.bootstrap: []
        - unless:
            - fun: cmd.run
              cmd: 'Get-PackageProvider NuGet'
              shell: powershell
    
    # *or*
    
    ensure_nuget:
      cmd.run:
        - name: 'Get-PackageProvider -Name NuGet -ForceBootstrap'
        - shell: powershell
        - unless:
            - fun: cmd.run
              cmd: 'Get-PackageProvider NuGet'
              shell: powershell
    

    Unless seems to break with cmd.powershell.

    As I understand it, cmd.powershell returns a list or dictionary which should evaluate to false if no objects are returned, but also should evaluate to True if any data is returned. I'm guessing there might be something b0rked, but maybe this is unrelated/user-error.

  11. b-a-t commented on Oct 16, 2025

    @b-a-t

    Salt 3006.16 is here. Still for the state like(related to #52256):

    auditd_rules:
      file.managed:
        - name: "/etc/audit/rules.d/audit.rules"
        - source: salt://{{ slspath }}/files/audit.rules.jinja
        - template: jinja
        - user: root
        - group: root
        - mode: 600
        - makedirs: True
        - listen_in:
          - cmd: auditd_svc
    
     # Due to special handling of `auditd` in RHEL you can't restart
    # it with the `systemctl` and have to use `service` instead.
    # See: https://github.com/saltstack/salt/issues/52256
    auditd_svc:
      cmd.run:
        - name: "service auditd restart"
        - unless:
          - fun: service.status
            name: auditd.service
    

    I see:

    [WARNING ] no `fun` argument in unless: OrderedDict([('name', 'auditd.service')])
    

    Luckily, the state still works.

  12. 3 remaining items

  13. added this to the Argon v3008.2 milestone on Jun 24, 2026
  14. added
    severity-medium
    on Oct 16, 2026
  15. dwoz commented on Oct 16, 2026

    @dwoz
    Contributor

    Thanks for the original report. This issue has been open for more than three years without recent activity and carries a severity label below severity-high. As part of a backlog cleanup, we're closing older low- and medium-severity issues so triage can focus on reports against currently supported releases.

    Closing isn't a judgement that the problem wasn't real — only that the codebase has moved through several releases since this was filed, and much of the surrounding code has changed.

    If you — or anyone else — can still reproduce this on the 3008.x LTS release, please leave a comment with a reproduction and a maintainer will reopen. The original reporter can also reopen directly.

  16. lkubb commented on Oct 17, 2026

    @lkubb
    ContributorAuthor

    Original reproduction on 3008.3 (I can't reopen myself):

    ❯ salt-call --version
    salt-call 3008.3 (Argon)
    
    ❯ salt-call --local state.apply repro
    [WARNING ] no `fun` argument in onlyif: OrderedDict({'path': '/tmp/a'})
    [ERROR   ] Source file "/tmp/a" is not present
    local:
    ----------
              ID: Do sth else
        Function: cmd.run
          Result: True
         Comment: No changes detected
         Started: 11:59:32.884801
        Duration: 0.015 ms
         Changes:
    ----------
              ID: Copy stuff
        Function: file.copy
            Name: /tmp/aa
          Result: False
         Comment: Source file "/tmp/a" is not present
         Started: 11:59:32.884869
        Duration: 1.722 ms
         Changes:
    
    Summary for local
    ------------
    Succeeded: 1
    Failed:    1
    ------------
    Total states run:     2
    Total run time:   1.737 ms
    
  17. dwoz commented on Oct 22, 2026

    @dwoz
    Contributor

    Reopening based on the follow-up comment above.

  18. reopened this on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Requisitebugseverity-medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions