Salt: digging in on SmartOS

The following working example demonstrates installing a package from pkgsrc, starting the SMF service and restarting the service when a managed file (under Salt control) is changed:

installing a pkg from pkgsrc

Let's install nginx:

mkdir -p /opt/salt/srv/salt/webserver
cat >/opt/salt/srv/salt/webserver/init.sls <<EOF
nginx:
pkg:
    - installed
EOF
[root@saltmaster /opt/salt/srv/salt]# salt minion1 state.sls webserver

Then I want nginx to be running on my minion so we append the following to webserver/init.sls (YAML):

cat >>/opt/salt/srv/salt/webserver/init.sls <<EOF
  service:
- running
- name: pkgsrc/nginx
- watch:
  - file: /opt/local/etc/nginx/nginx.conf
- require:
  - pkg: nginx

So I've chosen nginx's main config file to be watched by salt (nginx.conf).
Adding this section alone won't do much unless we declare that the nginx.conf file is a managed file and that it requires the nginx pkg, so let's set this up:

cat >>/opt/salt/srv/salt/webserver/init.sls <<EOF
/opt/local/etc/nginx/nginx.conf:
  file:
- managed
- source: salt://webserver/nginx.conf
- require:
  - pkg: nginx

I fired off an HTTP request to the IP address of my minion zone and I got the default 'welcome to nginx' page :)
To test if the nginx service actually is restarted once I change the watched file (nginx.conf) I added a simple index.htm with nothing but 'Salt rocks' and changed the index order to have nginx serve out the new index.htm instead of the default index.html page.
First we have to tell salt that there is a new index.htm page which should also be managed by salt so we add our final section to our webserver init state file:

cat >>/opt/salt/srv/salt/webserver/init.sls <<EOF
/opt/local/share/examples/nginx/html/index.htm:
  file:
- managed
- source: salt://webserver/index.htm
- require:
  - pkg: nginx

I'm happy with this state file so here's the full webserver/init.sls:

[root@saltmaster /opt/salt/srv/salt]# cat webserver/init.sls
nginx:
 pkg:
- installed
  service:
- running
- name: pkgsrc/nginx
- watch:
  - file: /opt/local/etc/nginx/nginx.conf
- require:
  - pkg: nginx

/opt/local/etc/nginx/nginx.conf:
  file:
- managed
- source: salt://webserver/nginx.conf
- require:
  - pkg: nginx

/opt/local/share/examples/nginx/html/index.htm:
  file:
- managed
- source: salt://webserver/index.htm
- require:
  - pkg: nginx

Let's finally test this to see if the nginx pkg was installed and the SMF service was started:

[root@saltmaster /opt/salt/srv/salt]# salt minion1 state.sls webserver
minion1:
----------
      ID: nginx
Function: pkg.installed
  Result: True
 Comment: Package nginx is already installed
 Changes:
----------
      ID: /opt/local/etc/nginx/nginx.conf
Function: file.managed
  Result: True
 Comment: The file /opt/local/etc/nginx/nginx.conf is in the correct state
 Changes:
----------
      ID: nginx
Function: service.running
    Name: pkgsrc/nginx
  Result: True
 Comment: The service pkgsrc/nginx is already running
 Changes:
----------
      ID: /opt/local/share/examples/nginx/html/index.htm
Function: file.managed
  Result: True
 Comment: The file /opt/local/share/examples/nginx/html/index.htm is in the correct state
 Changes:

Summary
------------
Succeeded: 4
Failed:    0
------------
Total:     4

Awesome :)

This concludes my little salt demo on SmartOS