Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unless/ifonly params to module.run/wait + more granular unless/ifonly result evluation #5050

Closed
ryba-xek opened this issue May 16, 2013 · 22 comments
Assignees
Labels
Feature new functionality including changes to functionality and code refactors, etc.
Milestone

Comments

@ryba-xek
Copy link

NOTE (added by @basepi): All states now support unless and onlyif, this issue is still open for the additional request for more power in these statements than just cmd.retcode.

Salt state scripting will be much more useful if one can use module.run/wait states with unless and ifonly params.

The whole idea of salt is the aim not to use console commands in state.
So, the whole internal command stack was developed — called modules.
But why cmd.run/wait have unless/ifonly and module.run/wait doesn't?..

The second idea is to somehow to be able to control what unless/ifonly module commands are considered having positive result.

I mean for example I want smth like this:

debconf.set:
    module.run:
    - package: mysql-server-5.5
    - question: 'mysql-server/root_password'
    - type: 'password'
    - value: {{pillar['mysql.pass']}}
    - unless:
        command: pkg.version   
            - names:
                - mysql-server-5.5

Here pkg.version command returns empty string if not installed, this should be considered False. Any non-empty result should be string.

I will be happy if I will be ablo to use more complex logic like
- unless: pkg.available_version mysql-server < 5.5

@ryba-xek
Copy link
Author

Currently my mysql pr0n looks like this:

mysql-server:
  pkg:
    - installed
    - require:
      - pkg: python-mysqldb
      - cmd: 'pwd'
      - module: 'mysql-debconf1'
      - module: 'mysql-debconf2'

debconf-utils:
  pkg:
    - installed

python-mysqldb:
  pkg:
    - installed

pwd:
  cmd.run:
    - unless: "dpkg-query -W --showformat='${Status}\n' mysql-server-5.5 | grep 'install ok installed'"

mysql-debconf1:
  module.wait:
    - name: debconf.set
    - package: mysql-server-5.5
    - question: 'mysql-server/root_password'
    - type: 'password'
    - value: {{pillar['mysql.pass']}}
    - require:
      - pkg: debconf-utils
    - watch:
      - cmd: pwd

mysql-debconf2:
  module.wait:
    - name: debconf.set
    - package: mysql-server-5.5
    - question: 'mysql-server/root_password_again'
    - type: 'password'
    - value: {{pillar['mysql.pass']}}
    - require:
      - pkg: debconf-utils
    - watch:
      - cmd: pwd

I will be happy to replace dpkg-query -W --showformat='${Status}\n' mysql-server-5.5 | grep 'install ok installed' with more suitable module.run apt.version mysql-server-5.5.

@thatch45
Copy link
Contributor

I agree, I have approved this for the future, but it should have unless/onlyif for shell commands and executing other modules and checking the module retcode (which will take more work)

@ryba-xek
Copy link
Author

Thanks a lot!
Do you have some clear vision of how will it work and how will it look like in .sls file?

@thatch45
Copy link
Contributor

I don't know yet, probably the options like they are now and then the module_unless and module_onlyif options will be added.
Also keep in mind, we may not get to this for a while, there is a lot going on! :)

@ryba-xek
Copy link
Author

Okay, thanks. Just opened a new bug report for you... =) So you will have some work either way.
Anyway thanks for the great open-source product.

@pniederw
Copy link

Any progress on this? It's unfortunate that one has to drop back to cmd.run just because module.run has no unless/onlyif.

@galet
Copy link

galet commented Jan 2, 2014

Having this would be a huge help. I've run into this so many times. Is there a workaround that would allow a conditional execution for module.run?

@elithrar
Copy link

Adding my support for this. I'd like to run upstart.restart for a service ifonly upstart.status returns false, and therefore this feature would be extremely useful.

@leoluk
Copy link

leoluk commented Apr 3, 2014

It should be possible to use the templating system to emulate this, right?

@pidah
Copy link

pidah commented Jun 16, 2014

+1

@gtmanfred
Copy link
Contributor

I believe I implemented what is requested here.

Can someone verify?

https://github.com/saltstack/salt/blob/develop/salt/state.py#L601

@basepi
Copy link
Contributor

basepi commented Jun 16, 2014

Ah yes, another one that's closed because of your awesome global unless/onlyif additions! Thanks @gtmanfred!

@basepi basepi closed this as completed Jun 16, 2014
@khrisrichardson
Copy link

Correct me if I'm wrong (great work @gtmanfred by the way, I've already edited a ton of SLS files to use this), but I believe there's a meta request in the original issue that hasn't yet been addressed, that being the ability to use alternative functions to cmd.retcode for the conditionals. While the universal onlyif/unless if very useful, I've stumbled across a desire for the additional request myself a handful of times.

@basepi
Copy link
Contributor

basepi commented Jun 16, 2014

Ah, good point. Reopening.

@basepi basepi reopened this Jun 16, 2014
@basepi basepi added the Feature label Jun 16, 2014
@FunTimeCoding
Copy link

module.run with unless doesn't seem to work for me with the latest version
+1

@basepi
Copy link
Contributor

basepi commented Oct 8, 2014

@FunTimeCoding if it's not working at all, it's possible there's a bug. Please open a separate issue, include the states which are failing (simplified reproduction case if possible), and include the output of salt --versions-report.

@sivann
Copy link

sivann commented Dec 14, 2016

This still does not work.

@gtmanfred gtmanfred self-assigned this Jul 21, 2017
@Chupaka
Copy link

Chupaka commented Aug 8, 2018

Looks like it works now. The issue can be closed.

----------
          ID: /dev/sdb
    Function: module.run
        Name: xfs.mkfs
      Result: True
     Comment: unless condition is true
     Started: 11:18:19.735800
    Duration: 893.274 ms
     Changes:
----------

@gtmanfred
Copy link
Contributor

That is not what it is being talked about.

Passing modules to unless and onlyif, instead of just bash commands is the request.

@afletch
Copy link
Contributor

afletch commented Sep 13, 2018

How far did this get, can modules currently be passed to unless/onlyif? If so, what is the format for this?

@gtmanfred
Copy link
Contributor

no work has been done on this afaik

@gtmanfred
Copy link
Contributor

Work done here #51846

@dwoz dwoz closed this as completed in f88b721 Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature new functionality including changes to functionality and code refactors, etc.
Projects
None yet
Development

No branches or pull requests