This is an assignment h3 - Palvelinten hallinta ICT4TN022-5 in Haaga-Helia University of Applied Sciences

In this week assignment we had to do few different Salt states that utilizes different Jinja template engine methods.

Jinja in the Simplest Form

I start by making a working directory to my state:

(master)$ sudo mkdir jinjatest

Then I make file for the state, which I call init.sls so I can apply the state with only the directory’s name.

(master)$ sudo emacs /srv/salt/jinjatest/init.sls
{% set msg = 'hello, world' %}
{% set file = 'hello.txt' %}

/tmp/{{ file }}:
  file.managed:
    - source: salt://jinjatest/test.txt
    - template: jinja
    - context:
      msg: {{ msg }}

Then I make the test.txt file where the message is printed:

(master)$ sudo emacs /srv/salt/jinjatest/test.txt
{{ msg }}

Then I apply the state to see if it works:

(master)$ sudo salt tumble state.apply jinjatest
tumble:
----------
          ID: /tmp/hello.txt
    Function: file.managed
      Result: True
     Comment: File /tmp/hello.txt updated
     Started: 18:11:54.633263
    Duration: 499.676 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary for tumble
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time: 499.676 ms

Here you can see that the state made the file according to the variable I set. I make sure that msg I set was printed correctly by:

(master)$ sudo salt tumble cmd.run 'cat /tmp/hello.txt'
tumble:
    hello, world

Multiple Apache Virtual Hosts via Jinja

I chose to do this instead of the SSH-exercise since I had done that before

I start by removing Apache from my minion and test that there is no webserver running on my localhost:

(minion)$ sudo zypper rm apache2

(minion)$ $ curl localhost
curl: (7) Failed to connect to localhost port 80: Connection refused

Again I start by making a working directory for my state:

(master)$ sudo mkdir virtualhost

Then I start building my state:

(master)$ sudo emacs /srv/salt/virtualhost/init.sls
apache2:
  pkg.installed

{% for vhost in ['tumble.example.com', 'tumble2.example.com'] %}

/etc/apache2/vhosts.d/{{ vhost }} .conf:
  file.managed:
    - source: salt://virtualhost/default_vhost.conf
    - template: jinja
    - context:
      vhost: {{ vhost }}

/home/topi/public_html/{{ vhost }}/index.html:
  file.managed:
    - user: topi
    - group: users
    - mode: 644
    - makedirs: True
    - source: salt://virtualhost/default_index.html
    - template: jinja
    - context:
      vhost: {{ vhost }}

{% endfor %}

apache2.service:
  service.running:
    - watch:
      - file: /etc/apache2/vhosts.d/*.conf

Notice that apache configurations differ on openSUSE when compared to, e.g., Debian. Also notice that, since I make these virtualhost files to user’s home directory it is important to give it correct permissions. This state also restarts apache when any file ending to .conf is changed in /etc/apache2/vhosts.d/.

Then I make the default_vhost.conf and default_index.html files indicated in the state above:

(master)$ sudo emacs /srv/salt/virtualhost/default_vhost.conf
<VirtualHost *:80>
             ServerName {{ vhost }}
             ServerAlias {{ vhost }}

             DocumentRoot /home/topi/public_html/{{ vhost }}

             <Directory /home/topi/public_html/{{ vhost }}>
                        Require all granted
             </Directory>
</VirtualHost>
(master)$ sudo emacs /srv/salt/virtualhost/default_index.html
Welcome to {{ vhost }}!

Then I apply the state:

(master)$ sudo salt tumble state.apply virtualhost
tumble:
----------
          ID: apache2
    Function: pkg.installed
      Result: True
     Comment: The following packages were installed/updated: apache2
     Started: 19:04:34.563942
    Duration: 32164.082 ms
     Changes:   
              ----------
              apache2:
                  ----------
                  new:
                      2.4.29-7.1
                  old:
              apache2-mod_php7:
                  ----------
                  new:
                      7.2.4-1.1
                  old:
              apache2-prefork:
                  ----------
                  new:
                      2.4.29-7.1
                  old:
              gpg-pubkey:
                  ----------
                  new:
                      a0e46e11-5911fc35
                  old:
                      1abd1afb-54176598,307e3d54-4be01a65,3dbdc284-53674dd4,a0e46e11-5911fc35
              kernel-default:
                  ----------
                  new:
                      4.16.0-1.6
                  old:
                      4.15.13-1.7,4.16.0-1.6
              kernel-default-devel:
                  ----------
                  new:
                      4.16.0-1.6
                  old:
                      4.15.13-1.7,4.16.0-1.6
              kernel-devel:
                  ----------
                  new:
                      4.16.0-1.5
                  old:
                      4.15.13-1.7,4.16.0-1.6,4.16.0-1.5
              kernel-source:
                  ----------
                  new:
                      4.16.0-1.6
                  old:
                      4.15.13-1.7,4.16.0-1.5,4.16.0-1.6
              kernel-syms:
                  ----------
                  new:
                      4.16.0-1.6
                  old:
                      4.16.0-1.5,4.16.0-1.6
----------
          ID: /etc/apache2/vhosts.d/tumble.example.com.conf
    Function: file.managed
      Result: True
     Comment: File /etc/apache2/vhosts.d/tumble.example.com.conf updated
     Started: 19:05:06.732858
    Duration: 416.723 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: /home/topi/public_html/tumble.example.com/index.html
    Function: file.managed
      Result: True
     Comment: File /home/topi/public_html/tumble.example.com/index.html updated
     Started: 19:05:07.149782
    Duration: 211.947 ms
     Changes:   
              ----------
              diff:
                  New file
              group:
                  users
              mode:
                  0644
              user:
                  topi
----------
          ID: /etc/apache2/vhosts.d/tumble2.example.com.conf
    Function: file.managed
      Result: True
     Comment: File /etc/apache2/vhosts.d/tumble2.example.com.conf updated
     Started: 19:05:07.361936
    Duration: 256.259 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: /home/topi/public_html/tumble2.example.com/index.html
    Function: file.managed
      Result: True
     Comment: File /home/topi/public_html/tumble2.example.com/index.html updated
     Started: 19:05:07.618476
    Duration: 226.674 ms
     Changes:   
              ----------
              diff:
                  New file
              group:
                  users
              mode:
                  0644
              user:
                  topi
----------
          ID: apache2.service
    Function: service.running
      Result: True
     Comment: Started Service apache2.service
     Started: 19:05:12.433836
    Duration: 153.753 ms
     Changes:   
              ----------
              apache2.service:
                  True

Summary for tumble
------------
Succeeded: 6 (changed=6)
Failed:    0
------------
Total states run:     6
Total run time:  33.429 s

And it successfully installed apache2 and made files according to my state. Next I’ll test if it works as intended:

(minion)$ curl -H 'Host: tumble.example.com' localhost
Welcome to tumble.example.com!
(minion)$ curl -H 'Host: tumble2.example.com' localhost
Welcome to tumble2.example.com!

And seems to work correctly!

I have gathered various Salt states that I’ve wrote during the course to single repository in my GitHub. That can be found at https://github.com/topikettunen/salt-states

Resources