6

I'm new to salt-stack.

I'm trying to make sure a symbolic link exists.

/etc/localtime:
  file.symlink:
    - target: /usr/share/zoneinfo/Europe/Paris
    - file.exists:
      - name: /usr/share/zoneinfo/Europe/Paris

The result is as excepted when there's no existing /etc/localtime, or when /etc/localtime is already a symlink. But it fails when /etc/localtime is a regular file :

----------
State: - file
Name:      /etc/localtime
Function:  symlink
    Result:    False
    Comment:   File exists where the symlink /etc/localtime should be
    Changes:   

I can't figure how to code the state so the file is deleted before the link creation. Any clue ?

Cheers, Pierre

Kyle Kelley
  • 13,804
  • 8
  • 49
  • 78
Pierre
  • 63
  • 1
  • 3

1 Answers1

13

Use force=True

/etc/locatime:
  file.symlink:
    - target: /usr/share/zoneinfo/Europe/Paris
    - force: True
    # Note: file.exists is not valid here and can be removed
    # file.exists:
    #  - name: /usr/share/zoneinfo/Europe/Paris

From the documentation:

If the target of the symlink exists and is not a symlink and force is set to False, the state will fail. If force is set to True, the file or directory in the way of the symlink file will be deleted to make room for the symlink, unless backupname is set, when it will be renamed

Dan Garthwaite
  • 3,436
  • 4
  • 22
  • 32
  • I don't think "file.exists" is doing anything in this example. It is just being ignored because it's not a valid option to "file.symlink" – Utah_Dave Dec 26 '13 at 17:21
  • Hrm. @Pierre I've removed the last two lines, can you confirm it still works? – Dan Garthwaite Dec 26 '13 at 17:57
  • @Dan-Garthwaite : Yes indeed. But the link is created even if the target does not exists ! So I guess something like this should be added elsewhere : /share/zoneinfo/Europe/Paris: file.exists – Pierre Dec 27 '13 at 17:49