unman, thank you for your response!
I did not realize you could make an entire sls a “requires” (and that you could link such a thing with include); thus far I had only used “requires” for other states in the same sls.
Given my knowledge level, I did try something similar to this…basically I put the “exist” state in the same sls file as the command (which was to make Qube A the default dispvm of Qube B…i want to do this both where Qube A is created, and where Qube B is created, to ensure it gets done regardless of the order in which the two are created, but of course each sls must check for the existence of the qube created by the other sls before making the attempt).
The problem, at least with the single-file solution, is that when the state to run the command executes, it returns false because the prerequisite is missing, and I have an apparent failure of the entire sls file to run propertly. It’s not a failure, though; I simply want to run the command “onlyif” the other qube is already present.
And since your suggestion uses “requires” I suspect it would behave similarly…though I haven’t tried it yet.
I like to use “exists” to check preconditions for even starting to run the states in an sls file. For example, if I am going to delete sys-net (and its dvm template, and the dvm template’s template) then rebuild it, I damn well want to make sure the clone I made to temporarily handle sys-net duties actually exists! In that case, yes, I do want my sls file to report an error to me; using exists as a “requires” for the first actual command in the sls file is perfect for that.
But in my current case, both outcomes are correct; it’s not an error I must stop and “correct” if the other qube doesn’t exist (yet). A similar thing to what I am looking for is cmd.run having the onlyif qualifier, which I have seen used to check for the existence of a file before editing it. That allows the check to be made in the same state as the command, so either outcome is “valid” [provided there isn’t some other bona fide error that is] and not a reason to throw red messages on my screen and report error 20 at the end.
In fact if there were a way to check qube existence in an onlyif it would be nearly ideal; I can run qvm prefs on a command lilne. (In fact that’s what I did do, for some reason.) If the qvm.prefs state had onlyif, that would be even better–I could even get rid of a lot of ugly jinja in some places if that existed. However it doesn’t appear to be an option, according the the full list of qubes salt states on github.
In any case I did solve this…by running qvm prefs as a command line, inside an if block. So the command itself becomes my atomic “do this only if the qube exists” operation. As I reported elsewhere here, I ended up doing a cmd.run on qvm-check other-qube 2> /dev/null; then qvm-prefs ....; fi
I did have a bit of trouble hitting on that exact command–other things I tried had mysterious syntax errors but only in the salt file, or would leak the qvm-check error to the state which would report False, but it’s past me now.
It’s clunky but it seems to be working.
Thanks again for your response. I hope I’ve given some food for thought for improvements to some of the “qvm” states in salt.