how to test if an unmanaged file exits

1,825 views
Skip to first unread message

Donovan Graham

unread,
Nov 8, 2013, 10:02:05 AM11/8/13
to salt-...@googlegroups.com
Hi 

I would like to test for the existence of a file which is independently installed by another package.  If I uncomment the line in example below, i get a " The following requisites were not found:" error. I have also tried to move the logic into the unless statement, but didn't have luck. Please could you advise.

/root/.bashrc:
  file:
    - append
    - text:
      - export WORKON_HOME=/usr/local/virtualenv
      - source /usr/local/bin/virtualenvwrapper.sh
    - unless: 
      - "sudo grep '^export WORKON_HOME' /root/.bashrc"
    - require:
      - pip.installed: virtualenvwrapper
      - file.directory: /usr/local/virtualenv
      # - file.exists: /usr/local/bin/virtualenvwrapper.sh  ## this line doesn't work


Many thanks, again:)
Donovan

David Anderson

unread,
Nov 8, 2013, 11:41:13 AM11/8/13
to salt-...@googlegroups.com
Do you have a state that checks file.exists on
/usr/local/bin/virtualenvwrapper.sh ?

E.g.:


/usr/local/bin/virtualenvwrapper.sh:
file.exists:

--
Dave
> --
> You received this message because you are subscribed to the Google
> Groups "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to salt-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

David Anderson

unread,
Nov 8, 2013, 11:42:16 AM11/8/13
to salt-...@googlegroups.com
Rather:

/usr/local/bin/virtualenvwrapper.sh:
file.exists

(no colon after file.exists)
--
Dave

Donovan Graham

unread,
Nov 8, 2013, 11:55:49 AM11/8/13
to salt-...@googlegroups.com
Hi

No I don't have another state, as I'm not currently managing the file.
I'll try to see if your suggestion helps.

Thanks
Donovan

Markus Kramer

unread,
Jul 18, 2014, 8:12:36 AM7/18/14
to salt-...@googlegroups.com
Hi Donavan,
have you solved your problem?
I am stuck with the same one :-)

Best regards, Markus

Markus Kramer

unread,
Jul 18, 2014, 9:08:44 AM7/18/14
to salt-...@googlegroups.com
Hi Dave,
it does work with an extra state (that requires a colon).

write_Yes:
  cmd:
    - run
    - cwd: /salt
    - name: WriteYes
    - require:
      - file: writeYesTrigger
writeYesTrigger:
  file.exists:
    - name: /salt/writeYesTrigger.txt

The extra state introduces one level on indirektion without any value.
How can it be done without an extra state?

Markus

Ryan Lane

unread,
Jul 18, 2014, 12:29:11 PM7/18/14
to salt-...@googlegroups.com
On Fri, Jul 18, 2014 at 6:08 AM, Markus Kramer <markus...@dhl.com> wrote:
Hi Dave,
it does work with an extra state (that requires a colon).

write_Yes:
  cmd:
    - run
    - cwd: /salt
    - name: WriteYes
    - require:
      - file: writeYesTrigger

write_Yes:
  cmd:
    - run
    - cwd: /salt
    - name: WriteYes
    - onlyif:
      - cmd: test -f /salt/writeYesTrigger.txt

If you're trying to run a command based on the existence of a file, the above works.

Sorry if this isn't what you're after, this thread is missing context.

- Ryan
Message has been deleted

Markus Kramer

unread,
Jul 22, 2014, 12:27:45 PM7/22/14
to salt-...@googlegroups.com
Hi Ryan,
thank you - this is exactly what I was after :-)

But I found a new problem:
On Windows       test -f /salt/file.txt        always return TRUE, also if the file does not exist.
therefore the only-if guarded command (WriteYes) is always executed.

Should I report this as a bug?

Also, I can leave out "cmd: " in "cmd: test", as below, and it seems to be legal Salt:
    - onlyif:
      - test -f /salt/writeYesTrigger.txt

Under Windows, this has the same result as with "cmd: ", but does "cmd: " make a difference?

Best regards,
Markus

Colton Myers

unread,
Aug 7, 2014, 5:39:09 PM8/7/14
to salt-...@googlegroups.com
If you remove the `cmd: ` does it still always return True?  I'm not sure that `cmd: ` was ever needed there, I think it runs the command by default, and for all I know, adding that `cmd: ` there could be the cause of your issues.

--
Colton Myers


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Markus Kramer

unread,
Aug 18, 2014, 8:46:15 AM8/18/14
to salt-...@googlegroups.com
Hi Colton,
yes, sadly "test -f" always returns True.

When I execute the code below, two notepads open, regardless if file /plan/salt/writeYesTrigger.txt exists or not.
This means: I does not matter if "cmd:" stands in front of "test -f". (form 1) or not (form 2)

I also tested a third form: "onlyif" and "test" in one row always return False!

Can you please decide if this is this a bug or wrong SLS code?

Tested with release 2014.1.10 on Windows 7, 64bit.
Best regards,
Markus

always_Yes1:

  cmd:
    - run
    - cwd: /
    - name: notepad
    - onlyif:
      - cmd: test
-f /plan/salt/writeYesTrigger.txt


always_Yes2:

  cmd:
    - run
    - cwd: /
    - name: notepad
    - onlyif:
      - test -f
/plan/salt/writeYesTrigger.txt


never_Yes3:
  cmd:
    - run
    - cwd: /plan/salt/
    - name: notepad YES.txt
    - onlyif: test -f /plan/salt/writeYesTrigger.txt


Daniel Jagszent

unread,
Aug 18, 2014, 8:56:13 AM8/18/14
to salt-...@googlegroups.com
Hi Markus,

the third option is the right syntax.

Are you sure there is a "test" executable in windows? Doesn't look like it when I e.g. read https://superuser.com/questions/219050/how-to-check-if-a-directory-exists-in-windows

Markus Kramer

unread,
Aug 18, 2014, 9:45:36 AM8/18/14
to salt-...@googlegroups.com
Hi Daniel,
well, if form 1 and 2 are "wrong syntax" why doesn't Salt tells or warns me at compile time?
Are there options to generate compiler warnings?
Also: what is wrong?

Interesting: there is an ERROR in the log, which tells me that " 'test' is not a recognized command."
Salt needs to halt execution on this kind of ERROR!

This is the old discussion of how to calculate with 1/0 (or 'undefinded').
If 'test' does not exist, then the result of test is undefined. The program must not "assume" that it's False.
Calculation with undefined (or null) must halt the program or throw an exeption.
Writing a log entry is, I regret to say it, not acceptable.

Seth House

unread,
Aug 18, 2014, 11:31:04 AM8/18/14
to salt-...@googlegroups.com
On Mon, Aug 18, 2014 at 7:45 AM, Markus Kramer <markus...@dhl.com> wrote:
> Interesting: there is an ERROR in the log, which tells me that " 'test' is
> not a recognized command."
> Salt needs to halt execution on this kind of ERROR!

That is indeed the normal behavior. This sounds like a Windows-related
bug. Will you please file an issue on GitHub? We'll get that fixed.
Reply all
Reply to author
Forward
0 new messages