创建state文件1

作者: 老夫刘某 | 来源:发表于2017-06-30 18:01 被阅读0次

    root@ubuntu:/etc/salt# vim master

    找到下面的内容并解除注释:

    file_roots:  base:
        - /srv/salt
    

    然后杀死salt-master,并重启:salt-master -d.

    root@ubuntu:/etc/salt# mkdir  /srv/salt
    root@ubuntu:/etc/salt#cd /srv/salt
    root@ubuntu:/srv/salt# vim top.sls
    

    写入下面的代码:

    base:  'os:ubuntu':    - match: grain    - webserver
    

    创建sls文件
    也是在这个目录下创建webserver.sls,包含下面内容:

    apache2:                # ID declaration
      pkg:                  # state declaration
        - installed          #  function declaration
    

    注意: The package name for the Apache httpd web server may differ depending on OS or distro — for example, on Fedora it ishttpd
    but on Debian/Ubuntu it isapache2
    .
    第一行的apache这是任意定义的,只是定义要被安装的包的名字
    第二行定义的是我们要用的stalt states,我们用了 pkg state来确保包被安装
    第三行称为函数声明,定义了pkg状态模块中调用的函数

    在所有的minions上安装apache,执行以下命令:

    salt '*' state.apply
    

    由于我的salt里面没有state.apply,我使用了state.highstate代替,我将上面的webserver.sls中的apache2改成了nginx:

    nginx:           
      pkg:                 
        - installed
    
    **Install the package**
    

    root@ubuntu:/srv/salt# salt ubuntu1 state.highstate

    ----------
        State: - pkg
        Name:      nginx
        Function:  installed
            Result:    True
            Comment:   The following packages were installed/updated: nginx.
            Changes:   libgd3: { new : 2.1.0-3ubuntu0.6
    old :
    }
                       nginx-core: { new : 1.4.6-1ubuntu3.7
    old :
    }
                       libxpm4: { new : 1:3.5.10-1ubuntu0.1
    old :
    }
                       nginx-common: { new : 1.4.6-1ubuntu3.7
    old :
    }
                       libvpx1: { new : 1.3.0-2
    old :
    }
                       nginx: { new : 1.4.6-1ubuntu3.7
    old :
    }
                       libtiff5: { new : 4.0.3-7ubuntu0.7
    old :
    }
                       libjpeg-turbo8: { new : 1.3.0-0ubuntu2
    old :
    }
                       libjpeg8: { new : 8c-2ubuntu8
    old :
    }
                       libjbig0: { new : 2.0-2ubuntu4.1
    old :
    }
    
    Summary
    ------------
    Succeeded: 1
    Failed:    0
    ------------
    Total:     1
    

    要是出错了,minions可以用下面的命令排查错误:

    root@ubuntu1:~# salt-minion  -l debug
    [DEBUG   ] Reading configuration from /etc/salt/minion
    [DEBUG   ] loading log_handlers in ['/var/cache/salt/minion/extmods/log_handlers', '/usr/lib/python2.7/dist-packages/salt/log/handlers']
    [DEBUG   ] Skipping /var/cache/salt/minion/extmods/log_handlers, it is not a directory
    [DEBUG   ] Configuration file path: /etc/salt/minion
    [INFO    ] Setting up the Salt Minion "ubuntu1"
    [DEBUG   ] Created pidfile: /var/run/salt-minion.pid
    [DEBUG   ] Chowned pidfile: /var/run/salt-minion.pid to user: root
    .....
    

    总结下:
    首先vim /etc/salt/master,找到file_roots:行 并打开与之相关的注释,如:file_roots:
    base: #这个是全局的

    - /srv/salt           
       #还可以继续往下面加,如下:
    

    dev:
    - /srv/salt/dev
    然后重启master.

    第二,cd /srv/salt,创建top.sls文件,内容包括要调用的state模块,如下:

      'ubuntu1':
        - foobarcom
    

    第三,创建webserver.sls状态文件,我们以安装apache为例:

    apache2:
      pkg.installed: []
      service.running:
        - require:
          - pkg: apache2
    

    执行命令:salt ubuntu1 state.highstate

    ubuntu1:
    ----------
        State: - pkg
        Name:      apache2
        Function:  installed
            Result:    True
            Comment:   Package apache2 is already installed
            Changes:   
    ----------
        State: - service
        Name:      apache2
        Function:  running
            Result:    True
            Comment:   The service apache2 is already running
            Changes:   
    
    Summary
    ------------
    Succeeded: 2
    Failed:    0
    ------------
    Total:     2
    

    如果有dev下有多个状态文件,可以base下面的top.sls文件中加载状态文件。

    相关文章

      网友评论

        本文标题:创建state文件1

        本文链接:https://www.haomeiwen.com/subject/ynjicxtx.html