配置管理-states
- states是SaltStack中的配置语言
- states sls文件是描述状态配置的文件
- states sls文件都是YAML语法
查看minion支持的所有states列表
salt '*salt*' sys.list_state_modules
查看模块详细用法
salt '*salt*' sys.state_doc file
YAML的规则:
- 缩进表示层级关系,默认缩进是2个空格、4个空格、6个空格
- 冒号后面有个空格,以冒号结尾可以有空格,可以无空格
- 短横线代表一个列表,短横线后面有个空格
[root@saltStack01 ~]# cat apache.sls apache-install: pkg.installed: # 模块pkg,方法installed,会匹配操作系统进行选择安装的方法 - name: httpd # 装的包的名称 apache-service: # id要唯一 service.running: # 状态模块service,running为模块的方法 - name: httpd # 管理服务的名称 - enable: True # 设置开机自动启动
常用状态模块介绍
1、pkg模块
查看pkg方法
[root@saltStack01 ~]# salt '*salt*' sys.list_state_functions pkg saltStack01: - pkg.installed # 安装 - pkg.latest # 确保最新版本 - pkg.mod_aggregate - pkg.mod_init - pkg.purged # 卸载并删除配置文件 - pkg.removed # 卸载 - pkg.uptodate [root@saltStack01 ~]#
例如:同时安装多个包
common_packages: pkg.installed: - pkgs: - unzip - dos2unix - salt-minion: 2015.8.5-1.el6
2、file模块
查看file方法
[root@saltStack01 ~]# salt '*salt*' sys.list_state_functions file saltStack01: - file.absent # 确保指定的文件或目录不存在。如果它存在,它将被删除。 - file.accumulated # 可用于文件内容替换 - file.append # 向文件末尾追加内容 - file.blockreplace - file.comment - file.copy # 从源目录拷贝文件到目标目录 - file.directory - file.exists - file.managed - file.missing - file.mknod - file.mod_run_check_cmd - file.patch - file.prepend - file.recurse - file.rename - file.replace - file.serialize - file.symlink - file.touch # 如果文件不存在创建文件,相当于touch file,如果存在就更新访问时间或者修改时间 - file.uncomment [root@saltStack01 ~]#
salt:// 表示当前环境的根目录。
例如:salt://lamp/files/httpd.conf 表示 /srv/salt/lamp/files/httpd.conf
[root@saltStack01 ~]# cat /etc/salt/minion |grep -A 9 file_roots # file_roots: # base: # - /srv/salt/ # dev: # - /srv/salt/dev/services # - /srv/salt/dev/states # prod: # - /srv/salt/prod/services # - /srv/salt/prod/states # file_roots: base: - /srv/salt
3、service模块
查看service方法
[root@saltStack01 ~]# salt '*salt*' sys.list_state_functions service saltStack01: - service.dead - service.disabled - service.enabled - service.mod_watch - service.running [root@saltStack01 ~]#
例如:
redis: service.running: - enable: True # 开机自启动 - reload: True # 重载
LAMP架构slat实现安装、配置、启动
- 安装软件包 --pkg
- 修改配置文件 --file
- 启动服务 --service
方法1:
lamp.sls文件内容如下
lamp-pkg: pkg.installed: - pkgs: - httpd - php - mariadb - mariadb-server - php-mysql - php-cli - php-mbstring apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 php-config: file.managed: - name: /etc/php.ini - source: salt://lamp/files/php.ini - user: root - group: root - mode: 644 mysql-config: file.managed: - name: /etc/my.cnf - source: salt://lamp/files/my.cnf - user: root - group: root - mode: 644 apache-service: service.running: - name: httpd - enable: True - reload: True mysql-service: service.running: - name: mariadb - enable: True - reload: True
运行sls文件
salt 'lamp1*' state.sls lamp.lamp
方法2:
文件lamp2.sls 内容如下:
apache-server: pkg.installed: - pkgs: - httpd - php file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 service.running: - name: httpd - enable: True - reload: True mysql-server: pkg.installed: - pkgs: - mariadb - mariadb-server file.managed: - name: /etc/my.cnf - source: salt://lamp/files/my.cnf - user: root - group: root - mode: 644 service.running: - name: mariadb - enable: True - reload: True php-config: file.managed: - name: /etc/php.ini - source: salt://lamp/files/php.ini - user: root - group: root - mode: 644
运行sls文件
salt 'lamp2*' state.sls lamp.lamp2
配置管理之状态间关系
1、require
- 执行apache-service时,先执行lamp-pkg和apache-config
- 如果执行成功才执行apache-service
apache-service: service.running: - name: httpd - enable: True - reload: True - require: - pkg: lamp-pkg - file: apache-config
2、require_in
- 如果mysql-config执行不成功,则不能执行mysql-service
mysql-config: file.managed: - name: /etc/my.cnf - source: salt://lamp/files/my.cnf - user: root - group: root - mode: 644 - require_in: - service: mysql-service
3、watch
- 如果apache-config这个id的状态发生变化就reload
- 如果不加reload: True,那么就restart
apache-service: service.running: - name: httpd - enable: True - reload: True - require: - pkg: lamp-pkg - watch: - file: apache-config
4、watch_in
- 如果mysql-service状态发生变化,则不能执行mysql-service
mysql-config: file.managed: - name: /etc/my.cnf - source: salt://lamp/files/my.cnf - user: root - group: root - mode: 644 - watch_in: - service: mysql-service
5、include
- lamp第一种方法中,将安装、配置、启动分别保存3个文件, 由一个总文件引用
init.sls文件内容
include: - lamp.lamp_pkg - lamp.lamp_config - lamp.lamp_service
lamp_pkg.sls文件内容
lamp-pkg: pkg.installed: - pkgs: - httpd - php - mariadb - mariadb-server - php-mysql - php-cli - php-mbstring
lamp_config.sls文件内容
apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 php-config: file.managed: - name: /etc/php.ini - source: salt://lamp/files/php.ini - user: root - group: root - mode: 644 mysql-config: file.managed: - name: /etc/my.cnf - source: salt://lamp/files/my.cnf - user: root - group: root - mode: 644 - require_in: - service: mysql-service
lamp_service.sls文件内容
apache-service: service.running: - name: httpd - enable: True - reload: True - require: - pkg: lamp-pkg - watch: - file: apache-config mysql-service: service.running: - name: mariadb - enable: True - reload: True
执行命令:
salt '*salt*' state.sls lamp.init
salt配置管理使用jinja2
1、配置使用jinja
apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 - template: jinja
2、列出参数列表
apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 - template: jinja - defaults: PORT: 8080
3、模板引用,修改配置文件httpd.conf
Listen {{ PORT }}
4、执行命令
salt '*salt*' state.sls lamp.init
模板里面支持
- salt执行模块 grinas 进行赋值
# 将IP地址指向本机IP Listen {{ grains['fqdn_ip4'][0] }}:{{ PORT }}
- salt远程执行模块
# mac is {{ salt['netwrok.hw_addr']('eth0') }}
- salt执行模块 pillar进行赋值
# pillar :{{ pillar['apache'] }}