Skip to main content

Saltstack Refresh Course 2: Salt Mines

Cheung Chau, Hong Kong

salt.modules.mine: The function cache system allows for data to be stored on the master so it can be easily read by other minions

Salt Mine Setup

We can configure the value that we want to mine from our minion inside it's configuration file. For testing I want to read my minions IP address and make it available to my other minions through a salt mine. The salt command to get your minions LAN IP address is:

salt '*' network.ip_addrs 'enp*'

Where the globbing in the end takes care of the fact that the primary LAN port is now no longer called eth0 but can have the ID's enp2s0, enp3s0, etc. To mine this value we can now add the following configuration to our minion servers - make sure that you choose the right interface here by first checking ip a:

/etc/salt/minion.d/mine.conf

mine_interval: 60
mine_functions:
  network.ip_addrs ['enp2s0']

Now you have to restart your minion services:

pkill -9 salt-minion
salt-minion -d

You can test your mine now by asking one of your minions for the IPv4 address of all your other minions (that configured above):

salt salt-minion mine.get '*' network.ip_addrs
salt-minion:
    ----------
    salt-master_minion:
        - 172.17.0.1
        - 192.168.2.110
    salt-minion:
        - 172.17.0.1
        - 192.168.2.111

Using Mine Data with JinJa

I want to use the mined data inside my Apache web service using a Jinja template. For this I just need to modify my Apache state by adding the line template: jinja

install_apache:
  pkg.installed:
    - pkgs:
      - apache2

index_html:
  file.managed:
    - name: /var/www/html/index.html
    - source: salt://apache/templates/index.html
    - template: jinja
    - user: www-data
    - group: www-data
    - mode: 644

apache_service:
  service.running:
    - name: apache2
    - enable: True

Now I am able to use Jinja to get access to the mined data inside salt://apache/templates/index.html:

/srv/salt/states/base/apache/templates/index.html

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Salty Dayz, Sailor!</title>
  <meta name="description" content="Salt Apache Template">
</head>

<body>
  
  {% set minion_ips = salt['mine.get']('salt-master_minion', 'network.ip_addrs') %}
  <strong>Salt Master:</strong>{{ minion_ips['salt-master_minion'][-1] }}

  <br/>
  
  {% set minion_ips = salt['mine.get']('salt-minion', 'network.ip_addrs') %}
  <strong>Salt Minion:</strong> {{ minion_ips['salt-minion'][-1] }}

</body>
</html>

Jinja Script in words:

  1. Create a variable minion_ips
  2. Set this variable to the mined value of network.ip_addrs that you get from your minions.
  3. Call the value of the variable minion_ips and display the -1 index - which is the last value in the list - the IP that starts with 192.168.2* see above.

Now apply the updated state to your web server minion and check the Apache webpage output:

salt salt-minion state.apply

The output should read:

Salt Master:192.168.2.110
Salt Minion: 192.168.2.111