Salt Notes
Task: Copy a .zip file to a minion and unzip it into a different directory.
This script is in two parts, first copy file to minion and set permissions etc, then the archive “master.zip” is unzipped into a new directory called /opt/rabbit-c-0.5
/opt/xxx/master.zip: file: - managed - source: salt://common/mq/master.zip - mode: 664 - user: root - group: rootmaster.zip: module.run: - name: archive.unzip - zipfile: /opt/xxx/master.zip - dest: /opt/rabbitmq-c-0.5
Task: Untar a .tar archive
Like our zip file example above, but this time a .tar archive, note the “options” line.
/opt/master.tar: module.run: - name: archive.tar - options: xvf - tarfile: /opt/master.tar - dest: /opt
Task: Add a user to the Minion
In this example we create an account for a daemon that needs a user account but does not allow log in’s. The user account to create is “collectd”. More information on the user add command is available at http://docs.saltstack.com/en/latest/ref/states/all/salt.states.user.html
collectd: user.present: - fullname: 'Collectd Daemon' - createhome: false - shell: /bin/nologin
Create a MySQL Database, User and Set Permissions
This block of code creates a database if NOT present and defines the user with permissions. The script also sets the password via a variable which is hand if the variable is reused in other parts of the script later.
## create and setup the DB#keystone: mysql_database: - present mysql_grants.present: - grant: all privileges - database: keystone.* - user: keystone - host: localhost mysql_user.present: - host: localhost - password: {{ KEYSTONE_DBPASS }} - connection_charset: utf8 - saltenv: - LC_ALL: "en_US.utf8"
Copy over a shell script to minion, and execute it,
Store your script in salt filestore, copy to minion, cmd.run it
your_script.sls
/tmp/runtest.sh ## this will copy your shell script to /tmp
file.managed:
- source: salt://linux/scripts/runtest.sh
- makdirs: True ## create the target dir if not present
- user: root
- group: root
- mode: 755
runtest.sh ## actions for your script
cmd.run: ## run it!
- name: /tmp/runtest.sh ## name of script on the target
- requires
- file: salt://linux/scripts/runtest.sh