SaltStack中状态间关系unless、onlyif、require、require_in、watch、watch_in

1、unless

  检查的命令,仅当unless选项指向的命令返回值为false时才执行name定义的命令

cmd.run:
    {% if grains["osmajorrelease"]|string in ["5","6"] %}
    - name: 'nohup sh /alidata1/admin/esstats/tcp_stats.sh > /alidata1/admin/esstats/nohup.log 2>&1 &'
    {% else %}
    - name: 'systemd-run  --scope su - root -c "nohup sh /alidata1/admin/esstats/tcp_stats.sh > /alidata1/admin/esstats/nohup.log 2>&1 &"'
    {% endif %}
    - runas: root
    - unless: 'ps -ef |grep tcp_stats.sh|grep -v grep'
    - require:
      - file: tcp_stats_file
      - pkg: iftop_install
View Code

2、onlyif

 检查的命令,仅当unless选项指向的命令返回值为true时才执行name定义的命令

set_link_logs:
  file.symlink:
    - name: /alidata1/admin/logs
    - target: /alidata1/admin/esstats/logs/season_8_cluster_tcp/
    - force: True
    - user: admin
    - group: admin
    - onlyif: test -d /alidata1/admin/esstats/logs/season_8_cluster_tcp/
View Code

3、require

此状态依赖于另一个状态(我依赖某个状态,也就是某个状态失败了,我也就不执行name)

4、require_in

此状态另一个状态所依赖

5、watch

我的状态关注某一个状态

6、watch_in

我的状态被某一个状态所关注

[root@linux-node1 apache]# vim init_require.sls 
apache-install:
  pkg.installed:
    - name: httpd

apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://apache/files/httpd1.conf----->将此处的文件改错,模拟配置错误
    - user: root
    - group: root
    - mode: 644

apache-service:
  service.running:
    - name: httpd
    - enable: True
    - require:---------------------------->使用require,表示依赖
      - pkg: apache-install--------------->依赖的状态模块为pkg模块,id为apache-install 也就是说这个安装成功后执行
      - file: apache-config--------------->依赖的状态模块为file模块,id为apache-config 也就是说文件管理成功后执行

在执行service前 先pkg中的apache-install  然后在执行 fil中的 apache-config 模块执行都成功 才执行service.running模块

#注意一个ID下面一个模块只能只用使用一次
View Code

 

posted @ 2019-11-20 14:39  在半空頫視地球╰☆╮  阅读(633)  评论(0编辑  收藏  举报