Search code examples
mysqldevopssalt-project

Only execute a command in Salt if a directory is empty


I am trying to integrate the following command into a salt state:

mysql_install_db --user=mysql --basedir=/usr/ --ldata=/data/mysql/

but only when either /data/mysql is empty or mysql contains no databases.

Can anyone suggest a good way of doing this please?

Thanks in advance


Solution

  • Use unless inside your state file validating the existence of a path:

    mysql_install_db:
      cmd.run: 
        - name: mysql_install_db --user=mysql --basedir=/usr/ --ldata=/data/mysql/
        - unless: file.path_exists_glob('/data/mysql/*')
    

    Edit

    As stated by @Christophe the previous version of the state had some issues . Updated the state based on that and on what I believe will be the best fit.