0

Background: recently I discovered an issue on our servers where zookeeper wouldn't start on one of them. It was caused by the myid file being empty (it requires a number). Executing salt didn't fix it. It turns out that the issue was cause by a recent hostname change. I fixed the sls file, but any such tweak may cause it to break again.

So, my question is: can I use salt to validate that certain changes were correctly applied? In this case, I could check the size of the file or validate that the contents contained a single number. Ideally this would be run as part of the execution of a state, but if there's another way I'd love to hear it.

Just to be specific, I'm not looking to validate or lint the state file themselves. I just want to ensure that the end results are valid to some degree. E.g. the file contains a single number and isn't empty.

1
  • 1
    Have a look at serverspec.
    – jordanm
    Feb 2, 2017 at 20:54

2 Answers 2

2

To go along with Roald Nefs' answer, you can use module.run within a state to execute a module within a state. So, for my case, I ended up doing this:

myid_file_test:
    module.run:
        name: file.search
        path: /path/to/myid
        pattern: '\d'

If the pattern fails to match, then it will report a failure, which is exactly what I want.

1

With the salt.module.file module you would be able to manage information about regular files, directories, and special files on the minion, set/read user, group, mode, and data.

This modules includes a search() function which allows you to search for occurrences of a pattern in a file.

salt '*' file.search /path/to/the/file pattern='='

The pattern is a regular expression, to be matched using Python's search().

You could use the diskusage() function to recursively calculate disk usage of path and return it in bytes:

salt '*' file.diskusage /path/to/the/file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.