Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are "states" when using SaltStack?

Tags:

salt-stack

I'm trying SaltStack after using Puppet for a while, but I can't understand their use of the word "state".

My understanding is that, for example, a light switch has 2 possible states - on or off. When I write my SLS configuration I am describing what state a server should be in. When I ask SaltStack to provision a server I issue the command salt '*' state.highstate. I understand that a server can be in a highstate (as described in my config) or not. All good so far.

But this page describes other states. It describes lowstate, highstate and overstate (amongst others) as layers. Does this mean a server passes through several states to get to a highstate? Or all states are maintained simultaneously as layers? Or can I configure multiple possible states in my SLS and have SaltStack switch between them? Or are they just layers to SaltStack that have 'state' in the name and I'm confused?

I'm probably missing something obvious, if anyone can nudge me in the right direction I think a lot of the documentation will become clear to me!

like image 239
NoChecksum Avatar asked Jan 08 '23 04:01

NoChecksum


2 Answers

Here, top.sls wihch contain,

# cat top.sls
base:
 '*':
  - httpd_require

and,

# cat httpd_require.sls
install_httpd:
 pkg.installed:
   - name: httpd
 service.running:
   - name: httpd
   - enable: True
   - require:
     - file: install_httpd
 file.managed:
   - name: /var/www/html/index.html
   - source: salt://index1.html
   - user: root
   - group: root
   - mode: 644
   - require:
   - pkg: install_httpd

High state:

We can see all the aspects of high state system while working with state files( .sls), There are three specific components.

  • High data:
  • SLS file:
  • High State

    • Each individual State represents a piece of high data(pkg.installed:'s block), Salt will compile all relevant SLS inside the top.sls, When these files are tied together using includes, and further glued together for use inside an environment using a top.sls file, they form a High State.

      # salt 'remote_minion' state.show_highstate --out yaml
      remote_minion:
        install_httpd:
          __env__: base
          __sls__: httpd_require
          file:
          - name: /var/www/html/index.html
          - source: salt://index1.html
          - user: root
          - group: root
          - mode: 644
          - require:
           - pkg: install_httpd
          - managed
          - order: 10002
          pkg:
          - name: httpd
          - installed
          - order: 10000
          service:
          - name: httpd
          - enable: true
          - require:
            - file: install_httpd
          - running
          - order: 10001
      

First, an order is declared, All States that are set to be first will have their order adjusted accordingly. Salt will then add 10000 to the last defined number (which is 0 by default), and add any States that are not explicitly ordered. Salt will also add some variables that it uses internally, to know which environment (__env__) to execute the State in, and which SLS file (__sls__) the State declaration came from, Remember that the order is still no more than a starting point; the actual High State will be executed based first on requisites, and then on order.

"In other words, "High" data refers generally to data as it is seen by the user."

Low States:

""Low" data refers generally to data as it is ingested and used by Salt."

Once the final High State has been generated, it will be sent to the State compiler. This will reformat the State data into a format that Salt uses internally to evaluate each declaration, and feed data into each State module (which will in turn call the execution modules, as necessary). As with high data, low data can be broken into individual components:

  • Low State
  • Low chunks
  • State module
  • Execution module(s)

     # salt 'remote_minion' state.show_lowstate --out yaml
     remote_minion:
     - __env__: base
       __id__: install_httpd
       __sls__: httpd_require
       fun: installed
       name: httpd
       order: 10000
       state: pkg
     - __env__: base
       __id__: install_httpd
       __sls__: httpd_require
       enable: true
       fun: running
       name: httpd
       order: 10001
       require:
       - file: install_httpd
       state: service
     - __env__: base
       __id__: install_httpd
       __sls__: httpd_require
       fun: managed
       group: root
       mode: 644
       name: /var/www/html/index.html
       order: 10002
       require:
       - pkg: install_httpd
       source: salt://index1.html
       state: file
       user: root
    

Together, all this comprises a Low State. Each individual item is a Low Chunk. The first Low Chunk on this list looks like this:

    - __env__: base
      __id__: install_httpd
      __sls__: httpd_require
      fun: installed
      name: http
      order: 10000
      state: pkg

Each low chunk maps to a State module (in this case, pkg) and a function inside that State module (in this case, installed). An ID is also provided at this level (__id__). Salt will map relationships (that is, requisites) between States using a combination of State and __id__. If a name has not been declared by the user, then Salt will automatically use the __id__ as the name.Once a function inside a State module has been called, it will usually map to one or more execution modules which actually do the work.

like image 157
Ranjithkumar T Avatar answered Mar 16 '23 10:03

Ranjithkumar T


salt '\*' state.highstate

  1. '*' refers to all the minions connected to the master.
  2. 'state.highstate' is used to run all modules / scripts mentioned in top.sls defined in master
  3. To invoke a specific module / script on all minions, use the following salt command where the state information is defined in state.sls for apache in the example given below.

salt '\*' state.sls apache

To invoke the above salt call only on a specific minion, use the below command.

salt 'minion-name' state.sls apache

like image 34
Pratik Anand Avatar answered Mar 16 '23 09:03

Pratik Anand