Skip to content

salt.states.grains.list_present adds duplicates names #31427

Closed

Description

@githubcdr

Hi,

I noticed that using the 'grains' state multiple times results in duplicate list items. One would expect that the grains state immediately updates the roles grains after modifying it.

For example this state

# set roles
roles-nginx:
  grains.list_present:
    - name: roles
    - value:
      - webserver
      - webserver-nginx
    - require:
      - service: nginx-service

roles-nginx-php-fpm:
  grains.list_present:
    - name: roles
    - value:
      - php
      - php-fpm
      - webserver
      - webserver-nginx
      - webserver-nginx-php-fpm

yields this result

zsh 386 # salt minion grains.item roles
minion:
    ----------
    roles:
        - webserver
        - webserver-nginx
        - php
        - php-fpm
        - webserver
        - webserver-nginx
        - webserver-nginx-php-fpm

Is this a bug or a feature? It feels wrong to force a grain update in the state file?

Activity

  1. added
    needs-more-info
    on Feb 23, 2016
  2. added this to the Blocked milestone on Feb 23, 2016
  3. jfindlay commented on Feb 23, 2016

    @jfindlay
    Contributor

    @githubcdr, thanks for reporting. What version of salt are you using?

  4. githubcdr commented on Feb 23, 2016

    @githubcdr
    ContributorAuthor

    2015.8.7

  5. jfindlay commented on Feb 23, 2016

    @jfindlay
    Contributor

    @githubcdr, I get the following results on 2015.8.3 and 2015.8.7.

    jmoney-main ~ master # cat /srv/salt/test.sls
    roles:
      grains.list_present:
        - name: roles
        - value:
          - elliptic
          - parabolic
          - hyperbolic
    
    additional-roles:
      grains.list_present:
        - name: roles
        - value:
          - hyperbolic
          - diabolic
    jmoney-main ~ master # salt jmoney-main state.show_sls test
    jmoney-main:
        ----------
        additional-roles:
            ----------
            __env__:
                base
            __sls__:
                test
            grains:
                |_
                  ----------
                  name:
                      roles
                |_
                  ----------
                  value:
                      - hyperbolic
                      - diabolic
                - list_present
                |_
                  ----------
                  order:
                      10001
        roles:
            ----------
            __env__:
                base
            __sls__:
                test
            grains:
                |_
                  ----------
                  name:
                      roles
                |_
                  ----------
                  value:
                      - elliptic
                      - parabolic
                      - hyperbolic
                - list_present
                |_
                  ----------
                  order:
                      10000
    jmoney-main ~ master # salt jmoney-main state.apply test
    jmoney-main:
    ----------
              ID: roles
        Function: grains.list_present
          Result: True
         Comment: Append value ['elliptic', 'parabolic', 'hyperbolic'] to grain roles
         Started: 12:20:47.135999
        Duration: 200.115 ms
         Changes:
                  ----------
                  new:
                      ----------
                      roles:
                          - elliptic
                          - parabolic
                          - hyperbolic
                          - hyperbolic
                          - diabolic
    ----------
              ID: additional-roles
        Function: grains.list_present
            Name: roles
          Result: True
         Comment: Append value ['hyperbolic', 'diabolic'] to grain roles
         Started: 12:20:47.336386
        Duration: 83.804 ms
         Changes:
                  ----------
                  new:
                      ----------
                      roles:
                          - elliptic
                          - parabolic
                          - hyperbolic
                          - hyperbolic
                          - diabolic
    
    Summary for jmoney-main
    ------------
    Succeeded: 2 (changed=2)
    Failed:    0
    ------------
    Total states run:     2
    Total run time: 283.919 ms
    jmoney-main ~ master # salt jmoney-main grains.item roles
    jmoney-main:
        ----------
        roles:
            - elliptic
            - parabolic
            - hyperbolic
            - hyperbolic
            - diabolic
    
  6. modified the milestones: Approved, Blocked on Feb 23, 2016
  7. added
    bug
    severity-medium
    Core
    P2
    Confirmed
    and removed
    needs-more-info
    on Feb 23, 2016
  8. 43 remaining items

  9. barneysowood commented on Sep 4, 2025

    @barneysowood
    Collaborator

    Ok, this bug is actually more complex, I just noted that the return from the first state has the items from the second state included in its output?!

    Also, applying the state again does the right thing..

    ❯ sudo salt-call grains.get roles
    local:
        - elliptic
        - parabolic
        - hyperbolic
        - hyperbolic
        - diabolic
    
    ❯ sudo salt-call state.apply grains-present
    local:
    ----------
              ID: roles
        Function: grains.list_present
          Result: True
         Comment: Value ['elliptic', 'parabolic', 'hyperbolic'] is already in grain roles
         Started: 15:32:27.186966
        Duration: 1.901 ms
         Changes:
    ----------
              ID: additional-roles
        Function: grains.list_present
            Name: roles
          Result: True
         Comment: Value ['hyperbolic', 'diabolic'] is already in grain roles
         Started: 15:32:27.189092
        Duration: 1.059 ms
         Changes:
    
    Summary for local
    ------------
    Succeeded: 2
    Failed:    0
    ------------
    Total states run:     2
    Total run time:   2.960 ms
  10. added 5 commits that reference this issue on Oct 5, 2025
    c9d0888
    1959920
    e8d0d98
    651972e
    31504ee
  11. Sxderp commented on Oct 6, 2025

    @Sxderp
    Contributor

    Ok, this bug is actually more complex, I just noted that the return from the first state has the items from the second state included in its output?!

    While visually confusing, this is not an issue. This happens because the state grains.list_present returns the output of the module grains.append. The module grains.append is working with the "global" grains object and thus is returning the same dictionary instance each time. At the expense of additional memory this "visual bug" can be easily fixed by making a deep copy of the return from grains.append. I'm not sure it's worth it.

    However, it is worth considering the fact that grains.append is returning an object that (I believe) is actually present in the grains. Accidental direct modifications to that object could be reflected in the grains without any of the safeguards of the grain functions. /shrug

  12. linked a pull request that will close this issuefix various bugs in list_present #68369on Oct 6, 2025
  13. added a commit that references this issue on Oct 21, 2025
    0e86012
  14. added 2 commits that reference this issue on Nov 4, 2025
    1905fc4
    e33eb5b
  15. added a commit that references this issue on Dec 1, 2025
    45b4700
  16. added a commit that references this issue on Dec 4, 2025
    375442c
  17. added a commit that references this issue on Dec 12, 2025
    9804725
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ConfirmedCoreGrainsbugmust-fixseverity-medium

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions