cz-sjm 发表于 2018-7-31 08:00:56

SaltStack配置管理--状态间的关系

(1)require使用  # pwd
  /srv/salt/prod/apache
  # systemctl stop httpd
  # 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
  # salt -S "192.168.56.11" state.highstate   #执行模块提示会有报错,此时httpd不会正常启动
  ......
  ----------
  ID: apache-config
  Function: file.managed
  Name: /etc/httpd/conf/httpd.conf
  Result: False
  Comment: Source file salt://apache/files/httpd1.conf not found
  Started: 09:48:33.459243
  Duration: 40.414 ms
  Changes:
  ----------
  ID: apache-service
  Function: service.running
  Name: httpd
  Result: False
  Comment: One or more requisite failed: apache.init.apache-config
  Changes:
  ----------
  ......
  Summary for linux-node1.example.com
  ------------
  Succeeded: 6
  Failed:    2
  ------------
  Total states run:   8
  Total run time:   1.110 s
  # systemctl status httpd
  ● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Active: inactive (dead) since Sat 2018-01-20 09:44:04 CST; 4min 59s ago
  Docs: man:httpd(8)
  man:apachectl(8)
  Process: 65439 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Main PID: 1025 (code=exited, status=0/SUCCESS)
  Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
  Jan 17 10:41:59 linux-node1 systemd: Starting The Apache HTTP Server...
  Jan 17 10:42:02 linux-node1 systemd: Started The Apache HTTP Server.

  Jan 18 03:49:02 linux-node1 systemd:>  Jan 20 09:43:53 linux-node1 systemd: Stopping The Apache HTTP Server...
  Jan 20 09:44:04 linux-node1 systemd: Stopped The Apache HTTP Server.
  (2)require_in使用
  # vim init_require_in.sls
  apache-install:
  pkg.installed:
  - name: httpd
  - require_in:------------------>被依赖
  - service: apache-service---->被依赖的模块是service,id为apache-service
  apache-config:
  file.managed:
  - name: /etc/httpd/conf/httpd.conf
  - source: salt://apache/files/httpd.conf
  - user: root
  - group: root
  - mode: 644
  - require_in:
  - service: apache-service
  apache-service:
  service.running:
  - name: httpd
  - enable: True
  解释说明:require和require_in都能实现依赖的功能,主动和被动的关系不同
页: [1]
查看完整版本: SaltStack配置管理--状态间的关系