8、salt生产常用,状态文件include。修改minion的ID

如果一段配置文件会被多次使用,可以单独定义出来,然后其他文件引用进来。例如我们之前的lamp.sls中,可以把开始的安装单独拿出来

cd /srv/salt/base/web

[root@master-1 web]# vim httpd.sls

lamp-install: # 安装
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - php-pdo
      - php-mysql

 vim lamp.sls 

include:
  - web.httpd
#配置
apache-config:
  file.managed: # 文件管理
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://web/files/httpd.conf #这是个相对目录,相当于base目录下的web
    - user: root
    - group: root
    - mode: 644
    - template: jinja # 将httpd.conf变成模板文件
    - defaults: # 参数
      PORT: 80 #定义的两个变量
      IPADDR: {{ grains['fqdn_ip4'][0] }}
    - require: #依赖某个状态
      - pkg: lamp-install
apache-auth:
  pkg.installed:
    - name: httpd-tools
    - require_in:
      - cmd: apache-auth # 确保httpd-tools安装
  cmd.run:
    - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin # 调用htpasswd命令生成用户名密码,用户名和密码都是admin,保存到认证文件/etc/httpd/conf/htpasswd_file
    - unless: test -f /etc/httpd/conf/htpasswd_file # 判断命令返回值为真,那么cmd.run就不执行
apache-conf:
  file.recurse:
    - name: /etc/httpd/conf.d
    - source: salt://web/files/apache-conf.d

php-config:
  file.managed:
    - name: /etc/php.ini
    - source: salt://web/files/php.ini
    - user: root
    - group: root
    - mode: 644
lamp-service:
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - watch: # 关注某个状态,只要下边的配置文件有变更,就重载服务
      - file: apache-conf #监控的状态模块,后边是状态ID
      - file: apache-config

 

2、我们也可以把tomcat的安装过程分开,将jdk安装单独定义

vim jdk.sls

jdk-install:
  file.managed:
    - name: /usr/local/src/jdk-8u181-linux-x64.tar.gz
    - source: salt://web/files/jdk-8u181-linux-x64.tar.gz
  cmd.run:
    - name: cd /usr/local/src && tar zxf jdk-8u181-linux-x64.tar.gz -C /usr/local/
    - unless: test -f /usr/local/src/jdk-8u181-linux-x64.tar.gz && test -d /usr/local/jdk1.8.0_181
/etc/profile:
  file.append:
    - text:
      - "export JAVA_HOME=/usr/local/jdk1.8.0_181"
      - "export JRE_HOME=/usr/local/jdk1.8.0_181/jre"
      - "export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH"

vim tomcat.sls

include:
  - web.jdk
tomcat-install:
  file.managed:
    - name: /usr/local/src/apache-tomcat-8.0.46.tar.gz
    - source: salt://web/files/apache-tomcat-8.0.46.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src && tar zxf apache-tomcat-8.0.46.tar.gz -C /usr/local/ && ln -s /usr/local/apache-tomcat-8.0.46 /usr/local/tomcat
    - unless: test -L /usr/local/tomcat && test -d /usr/local/apache-tomcat-8.0.46

测试一下

salt master-2 state.sls web.tomcat test=True

salt master-2 state.sls web.tomcat

 

#3、改minion的ID过程

- 停止minion

- salt-key -d minionid #master操作

以下minion操作 

- rm -fr /etc/salt/minion_id

- rm -fr /etc/salt/pki

- 修改配置文件id:,或者用主机名作为id的,可以不改,把主机名改完即可,/etc/hostname,/etc/hosts两个文件

- 启动minion

- 重启master,否则master也会有缓存

- salt-key -A 将minion重新加入

 

posted @ 2021-02-23 14:38  莫莫学习  阅读(152)  评论(0编辑  收藏  举报