LLlAMnYP
LLlAMnYP

Reputation: 243

What *is* a salt formula, really?

I am trying to work through the Salt Formulas documentation and seem to be having a fundamental misunderstanding of what a salt formula really is.

Understandably, this question may seem like a duplicate of these questions, but due to my failing to grasp the basic concepts I'm also struggling to make use of the answers to these questions.

I thought, that a salt formula is basically just a package that implements extra functions, a lot like

#include <string.h>

in C, or

import numpy as np

in Python. Thus, I thought, I could download the salt-formula-linux to /srv/formulas/salt-formula-linux/, add that to file_roots, restart the master (all as per the docs), and then write a file like swapoff.sls containing

disable_swap:
  linux:
    storage:
      swap:
        file:
          enabled: False

(the above is somewhat similar to the examples in the repo's root) in hope that the formula would then handle removing the swap entry from /etc/fstab and running swapoff -a for me. Needless to say, this didn't work, clearly because I'm not understanding what a salt formula is meant to be.

So, what is a salt formula and how do I use it? Can I make use of it as a library of functions too?

Upvotes: 3

Views: 683

Answers (1)

LLlAMnYP
LLlAMnYP

Reputation: 243

This answer might not be fully correct in all technicalities, but this is what solved my problem.

A salt formula is not a library of functions. It is, rather, a collection of state files. While often a state file can be very simple, such as some of my user defined

--> top.sls <--
base:
  '*':
    - docker

--> docker.sls <--
install_docker_1703:
  pkgrepo.managed:
    # stuff
  pkg.installed:
    - name: docker-ce

creating a state file like

--> swapoff.sls <--
disable_swap:
  linux.storage.swap: # and so on

is, perhaps, not the way to go. Well, at least, maybe not for a beginner with lacking knowledge.

Instead, add an item to top.sls:

    - linux.storage.swap

This is not enough, however. Most formulas (or the state files within them, if you will) are highly parametrizable, i.e. they're full of placeholders with variable names, such as {{ swap.device }}. If there's nothing to fill this gap, the state fill will not be able to do anything. These gaps are filled from pillars.

All that remains, is to create a file like swap.sls in /srv/pillar/ that would contain something like (as per the examples of that formula)

linux:
  storage:
    enabled: true
    swap:
      file:
        enabled: true
        engine: file
        device: /swapfile
        size: 1024

and also /srv/pillar/top.sls with

base:
  '*':
    - swap

Perhaps /srv/pillar should also be included in pillar_roots in /etc/salt/master.

So now /srv/salt/top.sls runs /srv/formulas/salt-formula-linux/linux/storage/swap.sls which using the guidance of /srv/pillar/top.sls pulls some parameters from /srv/pillar/swap.sls and enables a swapfile.

Upvotes: 1

Related Questions