To work with files, Salt includes a stateless ZeroMQ file server that lets us send files to the minions from the master.
Files should be saved in our
/srv/salt/), generally within the directory of the formula they are being used for. However, files not associated with any formulas or states can still be saved and shared.
For CentOS servers, the MariaDB configuration files are located within the
/etc/my.cnf.d directory, with a primary configuration file located at
/etc/my.cnf. Since we're working on setting up MariaDB as a server, not a client, we'll be working to make sure our
/etc/my.cnf.d/server/server.cnf file is managed by Salt.
mysql directory, create a folder to store our configuration files:
$ cd /srv/salt/mysql $ mkdir files $ cd files
Pull in the configuration file:
$ curl -O https://raw.githubusercontent.com/linuxacademy/content-ssce-files/master/server.cnf
Open the file. When managing a file in Salt, it is best practice to place a comment at the top of the file informing anyone using the server that the file is managed by Salt, and should not be edited outside of configuration management:
$ $EDITOR server.cnf # The file is managed by Salt.
If needed, you can get more specific in this comment. You can make a note not to edit the file at all if you have any eager people working on the database or otherwise need to provide more information.
Save and exit the file.
We can now create another state, this time for adding this file to our CentOS 7 database minion. Let's drop back down into our MySQL formula folder, then create a
$ cd .. $ $EDITOR config.sls
From here, we want to set our state's name declaration. Remember, we want this to be specific so that anyone running these states later can read the output:
Next, to work with files, we need to use the
file state module. Specifically, we want to use the
file.managed function, which allows us to define a file, its source, its location on the minion, and more:
We now need to set our parameters for our managed file. The two parameters we need to include are the
name parameter, which will define the location of the file on the minion, and the
source parameter, which defines where on the master the file is located. Additional arguments, such as setting the user or group for the file and setting a source hash are also available. All arguments can be found via the
Let's add our name and source:
mysql_server_config: file.managed: - name: /etc/my.conf.d/server.cnf - source: salt://mysql/files/server.cnf
Save and exit.
Finally, let's ensure our state doesn't contain any errors by running a test state run:
$ sudo salt 'minion2' state.sls mysql.config test=true