Sunday, January 3, 2016

Salt states - Quick Start

Set up the file server

  • As per the master configuration file /etc/salt/master, the  master will automatically include all config files from /etc/salt/master.d/*.conf
  • Create the below file in master
# cat /etc/salt/master.d/file-roots.conf
file_roots:
    base:
    - /srv/salt/file/base
  • Restart the Salt master
# service salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]
  • This sets up our Salt master with our file_roots directory.

SLS file

  • Create the below SLS (Salt State) file


# cat /srv/salt/file/base/user-wilma.sls
-----------------------------------------------
user_wilma:
    user.present:
    - name: wilma
    - fullname: Wilma Flintstone
    - uid: 2001
    - home: /home/wilma
-----------------------------------------------

verify & debug the salt state file with below command

# salt centos65-minion state.show_sls user-wilma

Execute the state file

# salt centos65-minion state.sls user-wilma

state.single 

  • We can call the state functions directly by using the state.single execution module. This can come in handy when you’re testing state functions
            # salt centos65-minion state.single  user.present name=wilma fullname='Wilma Flintstone' uid=2001 home=/home/wilma

state.low

# salt centos65-minion state.low '{state: user, fun: present, name: wilma}'
  • In this call, we specify the state (user) and the function (present) as two different parts of the data structure.

The Top File

  • The Top file combines the various state files and it normally named top.sls
-----------------------------------------------
# cat /srv/salt/file/base/top.sls
base:
    'centos65-minion':
    - user-wilma
-----------------------------------------------
  • We can view the effective top file for any minion using the state.show_top command
          # salt centos65-minion state.show_top
  • Add another state file
-----------------------------------------------
         # cat /srv/salt/file/base/default/package.sls
              packages_vim:
              pkg.installed:
                - name: vim
-----------------------------------------------
  • Modify top file and include the new SLS file
-----------------------------------------------
# cat /srv/salt/file/base/top.sls
base:
    '*':
      - default.packages

    'centos65-minion':
      - user-wilma
-----------------------------------------------
  • Directories are not denoted with slashes, but with dots. So, in the above example,  the state file is in the path default/packages.sls
  • The highstate layer is used to combine various states together
  • Execute the highstate with below command
             # salt * state.highstate
  • Top file example with 'Grains' 
-----------------------------------------------
# cat /srv/salt/file/base/top.sls
base:
    'os:CentOS':
        - match: grain
        - default.vim-enhanced

    'os:Ubuntu':
        - match: grain
        - default.vim


  'centos65-minion':
    - users.dba
    - users.qa

  'ubuntu-14.04-amd64-vbox':
    - users.all

-----------------------------------------------

-----------------------------------------------
# cat /srv/salt/file/base/default/vim.sls
packages_vim:
    pkg.installed:        - name: vim

-----------------------------------------------

-----------------------------------------------
# cat /srv/salt/file/base/default/vim-enhanced.sls
packages_vim:
    pkg.installed:
        - name: vim-enhanced

-----------------------------------------------

 State Ordering

  • require:  The require declaration will enforce that the named state executes before the current state.

----------------------------------------------- 

# cat start.sls
roles_webserver_start:
  service.running:
    - name: nginx
    - require:
      - pkg: nginx

-----------------------------------------------

  • watch: Run based on other changes.The watch statement will execute additional states if any change is detected.

-----------------------------------------------

# cat sites/init.sls
sites_first:
  file.managed:
    - name: /usr/share/nginix/html/first.html
    - source: salt://sites/src/first.html
    - user: www
    - mode: 0644
  service.running:
    - name: nginx
    - watch:
      - file: /usr/share/nginix/html/first.html

-----------------------------------------------

  • ordering with 'order' .  Specify order using either  "order: 1" or "order: last"

-----------------------------------------------

# cat /srv/salt/file/base/run_first.sls
run_first:
  cmd.run:
    - name: 'echo "I am run first."'
    - order: 1

-----------------------------------------------

  •  cmd.run is very powerful, and it is tempting to use it often. But cmd.run will always report a change because it will run a command.

-----------------------------------------------

# cat /srv/salt/file/base/top.sls

base:

  'centos65-minion':
    - users.all
    - run_first
    - roles.webserver
    - sites

-----------------------------------------------

# salt centos65-minion state.highstate

  •  failhard option:

You can add failhard: True to any state. If that state fails to run for any reason, then the entire state (which includes a highstate) will immediately stop. This can prove very useful if you have a service that is absolutely required for your infrastructure to work. You can also add failhard as a global option in the minion configuration.

Testing states with test=true flag

# salt centos65-minion state.sls sites test=true

or

# salt centos65-minion state.highstate test=true 

init.sls directory shortcut

  • If there is a file named init.sls in a directory, then you can simply reference the directory name without init.sls.

No comments: