English 中文(简体)
SaltStack - Working Example
  • 时间:2024-11-03

SaltStack - Working Example


Previous Page Next Page  

In this working example, we will create a Salt formula that will configure the apache web server along with the PHP software. Salt is a great way to execute ad-hoc commands, but you would not really want to continually configure your infrastructure this way. By creating a set of Salt formulas, you can repably reproduce any configuration over.

Salt Formulas are simple YAML text files and by default reside on your Salt Master in /srv/salt/*. Let us start by creating a Salt Formula to install the Apache web server and PHP at the same time.

Create a file named “websetup.sls” under /srv/salt/ directory and add the following code.

websetup.sls

websetup:
   pkg:
      - installed
      - pkgs:
         - apache2
         - php5
         - php5-mysql

In this example, notice the “- pkgs:” argument. Each item in the pst below “- pkgs:” will be passed together to OS s package manager to be installed together. Whenever you have a large pst of packages to install this is the most efficient way to install them.

Apply this Formula to Salt master using the following command.

root@saltmaster:/home/vagrant# salt  minion2  state.sls websetup

Now, you will see the following output

minion2:
----------
   ID: websetup
   Function: pkg.installed
   Result: True
   Comment: 3 targeted packages were installed/updated.
   Started: 01:50:53.978396
   Duration: 86738.132 ms
   Changes:
      ----------
         apache2:
            ----------
            new:
               2.4.7-1ubuntu4.13
            old:
         apache2-api-20120211:
            ----------
            new:
               1
            old:
         apache2-bin:
            ----------
            new:
               2.4.7-1ubuntu4.13
            old:
         apache2-data:
            ----------
            new:
               2.4.7-1ubuntu4.13
            old:
         pbapache2-mod-php5:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         pbapr1:
            ----------
            new:
               1.5.0-1
            old:
         pbaprutil1:
            ----------
            new:
               1.5.3-1
            old:
         pbaprutil1-dbd-sqpte3:
            ----------
            new:
               1.5.3-1
            old:
         pbaprutil1-ldap:
            ----------
            new:
               1.5.3-1
            old:
         php5:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         php5-cp:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         php5-common:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         php5-json:
            ----------
            new:
               1.3.2-2build1
            old:
         php5-mhash:
            ----------
            new:
               1
            old:
         php5-mysql:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         php5-readpne:
            ----------
            new:
               5.5.9+dfsg-1ubuntu4.21
            old:
         phpapi-20121212:
            ----------
            new:
               1
            old:
         ssl-cert:
            ----------
            new:
               1.0.33
            old:
Summary for minion2
------------
Succeeded: 1 (changed = 1)
Failed:    0
------------ 
Total states run:     1
Total run time:  86.738 s

Now, you have installed the packages in minion2.

Highstate

A “highstate” is a way for Salt to determine which of the Salt Formulas should be appped to a certain minion. Execute a “highstate” using the following command.

root@saltmaster:/home/vagrant# salt <targets> state.highstate

top.sls

When the minion request to execute a highstate, as mentioned before, the minion requests the top.sls from the Salt master and searches for formulas that it matches. By default, this file is located at /srv/salt/top.sls. Let us add our formula to the top.sls file and set minion2 as target.

base:
    * :
      - common
    minion2’:
      - websetup

Now, execute the highstate targeting minion2 as shown below.

root@saltmaster:/home/vagrant# salt  minion2  state.highstate

After applying this, you could see the following output

minion2:
----------
   ID: common_packages
   Function: pkg.installed
   Result: True
   Comment: All specified packages are already installed
   Started: 01:55:17.998824
   Duration: 461.615 ms
   Changes:

Summary for minion2
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 461.615 ms

Now, Apache web server and PHP is installed in the minion2. In this way, we have to target minions using both top.sls and highstate and install the required software with minimal work and maximum flexibipty.

Advertisements