Book Image

Salt Cookbook

By : Anirban Saha
Book Image

Salt Cookbook

By: Anirban Saha

Overview of this book

Table of Contents (18 chapters)
Salt Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Understanding and writing the top file


In this recipe, we will understand and learn how to write the top file, the file which determines how the states are applied to the nodes or minions in Salt. We will apply the state that we configured in the previous recipe to minions.

How to do it...

The state configured in the previous recipe will now be applied to minions in the development environment.

  1. Create a file named top.sls in the base directory of the development environment:

    [root@salt-master ~]# touch \
    /opt/salt-cookbook/development/top.sls
    
  2. Edit the /opt/salt-cookbook/development/top.sls file and add the following contents:

    development:
      '*':
        - user
      'salt-minion':
        - match: list
        - hostconfig

How it works...

The top.sls file in Salt determines which state will be applied to which minions. It has an extension of .sls similar to all other files in Salt. It is written in the YAML format and takes minion matchers in the form of wildcards, nodegroups, lists, grains, and so on. We will look at all of the ways to match minions throughout the book when we go through each of these components.

First, we created the top.sls file in the base directory of the development environment, that is, /opt/salt-cookbook/development, and then we populated it with the required definitions.

The first line indicates the environment we are configuring the top file for, that is, development. Without this, the base environment will be used as default. The next line indicates a wildcard * meaning that it will match all minions and apply the listed state to all of them. Then, we listed the user state that we already configured:

development:
  '*':
    - user

We can also match minions when applying states in the top.sls file in the following manner:

development:
  '*':
    - user
  'salt-minion':
    - match: list
    - hostconfig

Here, we see a second block of node definition and state that introduces a new key called match. Instead of the wildcard in the preceding block, this block has the name of a minion node called salt-minion, which is the hostname of the minion node that will synchronize with the master and the second line in the block:

- match: list

This line indicates that the type of minion parameter to match is a list, which is nothing but the hostname of the minion. The line that has the name of the minion can be a comma-separated line of multiple hostnames:

'webserver,dbserver,appserver,proxyserver'

The list matcher will match the line as a list of minion names. Then, we mentioned a state called hostconfig, which is also the name of a state not configured in the book so far. This example means that the hostconfig state will be applied to all the minions in the list, which in this case is the minion named salt-minion.

There's more…

In Salt, minions can be matched in more ways than shown in this recipe, such as nodegroups, grains, IP/subnets, and so on. We will learn how to apply them in Chapter 3, Modules, Orchestration, and Scaling Salt.

See also

  • The Installing and configuring the Salt minion recipe, to learn how to install and configure minions