This tutorial builds on topics covered in part 1. It is recommended that you begin there.
In the last part of the Salt States tutorial we covered the
basics of installing a package. We will now modify our
to have requirements, and use even more Salt States.
1 2 3 4 5
apache: pkg.installed:  service.running: - require: - pkg: apache
Try stopping Apache before running
state.apply once again and observe the output.
For those running RedhatOS derivatives (Centos, AWS), you will want to specify the
service name to be httpd. More on state service here,
service state. With the example above, just add "- name: httpd"
above the require line and with the same spacing.
We now have a working installation of Apache so let's add an HTML file to
customize our website. It isn't exactly useful to have a website without a
webserver so we don't want Salt to install our HTML file until Apache is
installed and running. Include the following at the bottom of your
1 2 3 4 5 6 7 8 9 10 11 12
apache: pkg.installed:  service.running: - require: - pkg: apache /var/www/index.html: # ID declaration file: # state declaration - managed # function - source: salt://webserver/index.html # function arg - require: # requisite declaration - pkg: apache # requisite reference
line 7 is the ID declaration. In this example it is the location we
want to install our custom HTML file. (Note: the default location that
Apache serves may differ from the above on your OS or distro.
could also be a likely place to look.)
Line 11 is a Requisite declaration.
Line 12 is a Requisite reference which refers to a state and an ID.
In this example, it is referring to the
ID declaration from our example in
part 1. This declaration tells Salt not to install the HTML
file until Apache is installed.
Next, create the
index.html file and save it in the
<!DOCTYPE html> <html> <head><title>Salt rocks</title></head> <body> <h1>This file brought to you by Salt</h1> </body> </html>
salt '*' state.apply
Verify that Apache is now serving your custom HTML.
For example, if you use Salt to install an Apache virtual host configuration file and want to restart Apache whenever that file is changed you could modify our Apache example from earlier as follows:
/etc/httpd/extra/httpd-vhosts.conf: file.managed: - source: salt://webserver/httpd-vhosts.conf apache: pkg.installed:  service.running: - watch: - file: /etc/httpd/extra/httpd-vhosts.conf - require: - pkg: apache