官方文档 https://docs.saltstack.com/en/latest/topics/states/index.html

配置管理之SLS

Salt  State  SLS描述文件(YAML)

名称ID声明  默认是name声明

备注: 一个ID声明下面。状态模块不能重复使用

例:

  1. apache-install:
  2. pkg.installed:
  3. - names:
  4. - httpd
  5. - httpd-devel
  6.  
  7. apache-service: # ID声明,高级状态,ID必须唯一。
  8. service.running: # State声明 状态声明
  9. - name: httpd # 选项声明
  10. - enable: True
  11.  
  12. php:
  13. pkg.installed

常用状态模块介绍

1)pkg  (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.pkg.html#module-salt.states.pkg

pkg.installed  # 安装
pkg.latest  # 确保最新版本
pkg.remove  # 卸载
pkg.purge  # 卸载并删除配置文件

# 同时安装多个包

  1. common_packages:
  2. pkg.installed:
  3. - pkgs:
  4. - unzip
  5. - dos2unix
  6. - salt-minion: 2015.8.5-1.el6

2)file (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html#module-salt.states.file

salt:// 表示当前环境的根目录。例如:

那么salt://lamp/files/httpd.conf  表示 /srv/salt/lamp/files/httpd.conf

3)service (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.service.html#module-salt.states.service

  1. redis:
  2. service.running:
  3. - enable: True # 开机自启动 
  4. - reload: True # 重载

LAMP架构slat实现安装、配置、启动

1.安装软件包 pkg

2.修改配置文件 file

3.启动服务 service

lamp.sls文件内容如下

  1. lamp-pkg:
  2. pkg.installed:
  3. - pkgs:
  4. - httpd
  5. - php
  6. - mariadb
  7. - mariadb-server
  8. - php-mysql
  9. - php-cli
  10. - php-mbstring
  11.  
  12. apache-config:
  13. file.managed:
  14. - name: /etc/httpd/conf/httpd.conf
  15. - source: salt://lamp/files/httpd.conf
  16. - user: root
  17. - group: root
  18. - mode: 644
  19.  
  20. php-config:
  21. file.managed:
  22. - name: /etc/php.ini
  23. - source: salt://lamp/files/php.ini
  24. - user: root
  25. - group: root
  26. - mode: 644
  27.  
  28. mysql-config:
  29. file.managed:
  30. - name: /etc/my.cnf
  31. - source: salt://lamp/files/my.cnf
  32. - user: root
  33. - group: root
  34. - mode: 644
  35.  
  36. apache-service:
  37. service.running:
  38. - name: httpd
  39. - enable: True
  40. - reload: True
  41.  
  42. mysql-service:
  43. service.running:
  44. - name: mariadb
  45. - enable: True
  46. - reload: True

命令: salt 'linux-node2*' state.sls lamp.lamp

执行结果

  1. linux-node2.example.com:
  2. ----------
  3. ID: lamp-pkg
  4. Function: pkg.installed
  5. Result: True
  6. Comment: targeted packages were installed/updated.
  7. The following packages were already installed: httpd, mariadb-server, mariadb
  8. Started: ::16.178765
  9. Duration: 194279.377 ms
  10. Changes:
  11. ----------
  12. libzip:
  13. ----------
  14. new:
  15. 0.10.-.el7
  16. old:
  17. php:
  18. ----------
  19. new:
  20. 5.4.-36.3.el7_2
  21. old:
  22. php-cli:
  23. ----------
  24. new:
  25. 5.4.-36.3.el7_2
  26. old:
  27. php-common:
  28. ----------
  29. new:
  30. 5.4.-36.3.el7_2
  31. old:
  32. php-mbstring:
  33. ----------
  34. new:
  35. 5.4.-36.3.el7_2
  36. old:
  37. php-mysql:
  38. ----------
  39. new:
  40. 5.4.-36.3.el7_2
  41. old:
  42. php-pdo:
  43. ----------
  44. new:
  45. 5.4.-36.3.el7_2
  46. old:
  47. ----------
  48. ID: apache-config
  49. Function: file.managed
  50. Name: /etc/httpd/conf/httpd.conf
  51. Result: True
  52. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  53. Started: ::30.519583
  54. Duration: 98.547 ms
  55. Changes:
  56. ----------
  57. ID: php-config
  58. Function: file.managed
  59. Name: /etc/php.ini
  60. Result: True
  61. Comment: File /etc/php.ini is in the correct state
  62. Started: ::30.620067
  63. Duration: 36.824 ms
  64. Changes:
  65. ----------
  66. ID: mysql-config
  67. Function: file.managed
  68. Name: /etc/my.cnf
  69. Result: True
  70. Comment: File /etc/my.cnf is in the correct state
  71. Started: ::30.657074
  72. Duration: 58.78 ms
  73. Changes:
  74. ----------
  75. ID: apache-service
  76. Function: service.running
  77. Name: httpd
  78. Result: True
  79. Comment: The service httpd is already running
  80. Started: ::30.853149
  81. Duration: 40.481 ms
  82. Changes:
  83. ----------
  84. ID: mysql-service
  85. Function: service.running
  86. Name: mariadb
  87. Result: True
  88. Comment: The service mariadb is already running
  89. Started: ::30.893939
  90. Duration: 33.928 ms
  91. Changes:
  92.  
  93. Summary for linux-node2.example.com
  94. ------------
  95. Succeeded: (changed=)
  96. Failed:
  97. ------------
  98. Total states run:
  99. Total run time: 194.548 s

第二种方式:

文件lamp2.sls 内容如下:

  1. apache-server:
  2. pkg.installed:
  3. - pkgs:
  4. - httpd
  5. - php
  6. file.managed:
  7. - name: /etc/httpd/conf/httpd.conf
  8. - source: salt://lamp/files/httpd.conf
  9. - user: root
  10. - group: root
  11. - mode: 644
  12. service.running:
  13. - name: httpd
  14. - enable: True
  15. - reload: True
  16.  
  17. mysql-server:
  18. pkg.installed:
  19. - pkgs:
  20. - mariadb
  21. - mariadb-server
  22. file.managed:
  23. - name: /etc/my.cnf
  24. - source: salt://lamp/files/my.cnf
  25. - user: root
  26. - group: root
  27. - mode: 644
  28. service.running:
  29. - name: mariadb
  30. - enable: True
  31. - reload: True
  32.  
  33. php-config:
  34. file.managed:
  35. - name: /etc/php.ini
  36. - source: salt://lamp/files/php.ini
  37. - user: root
  38. - group: root
  39. - mode: 644

命令: salt 'linux-node2*' state.sls lamp.lamp2

执行结果

  1. linux-node2.example.com:
  2. ----------
  3. ID: apache-server
  4. Function: pkg.installed
  5. Result: True
  6. Comment: All specified packages are already installed
  7. Started: ::53.886308
  8. Duration: 665.948 ms
  9. Changes:
  10. ----------
  11. ID: apache-server
  12. Function: file.managed
  13. Name: /etc/httpd/conf/httpd.conf
  14. Result: True
  15. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  16. Started: ::54.553919
  17. Duration: 19.867 ms
  18. Changes:
  19. ----------
  20. ID: apache-server
  21. Function: service.running
  22. Name: httpd
  23. Result: True
  24. Comment: The service httpd is already running
  25. Started: ::54.574411
  26. Duration: 29.927 ms
  27. Changes:
  28. ----------
  29. ID: mysql-server
  30. Function: pkg.installed
  31. Result: True
  32. Comment: All specified packages are already installed
  33. Started: ::54.604496
  34. Duration: 0.771 ms
  35. Changes:
  36. ----------
  37. ID: mysql-server
  38. Function: file.managed
  39. Name: /etc/my.cnf
  40. Result: True
  41. Comment: File /etc/my.cnf is in the correct state
  42. Started: ::54.605362
  43. Duration: 15.125 ms
  44. Changes:
  45. ----------
  46. ID: mysql-server
  47. Function: service.running
  48. Name: mariadb
  49. Result: True
  50. Comment: The service mariadb is already running
  51. Started: ::54.620592
  52. Duration: 29.75 ms
  53. Changes:
  54. ----------
  55. ID: php-config
  56. Function: file.managed
  57. Name: /etc/php.ini
  58. Result: True
  59. Comment: File /etc/php.ini is in the correct state
  60. Started: ::54.650496
  61. Duration: 17.036 ms
  62. Changes:
  63.  
  64. Summary for linux-node2.example.com
  65. ------------
  66. Succeeded:
  67. Failed:
  68. ------------
  69. Total states run:
  70. Total run time: 778.424 ms

配置管理之状态间关系

状态间关系:

1.我依赖谁 require

  1. apache-service:
  2. service.running:
  3. - name: httpd
  4. - enable: True
  5. - reload: True
  6. - require:
  7. - pkg: lamp-pkg # pkg ID
  8. - file: apache-config # file ID

2 我被谁依赖 require_in

  1. mysql-config:
  2. file.managed:
  3. - name: /etc/my.cnf
  4. - source: salt://lamp/files/my.cnf
  5. - user: root
  6. - group: root
  7. - mode: 644
  8. - require_in:
  9. - service: mysql-service

3 我监控谁 watch

  1. apache-service:
  2. service.running:
  3. - name: httpd
  4. - enable: True
  5. - reload: True
  6. - require:
  7. - pkg: lamp-pkg
  8. - watch:
  9. - file: apache-config
  10. 1. 若果apache-config这个id的状态发生变化就reload
  11. 2. 如果不加reload: True,那么就restart

4 我被谁监控 watch_in

5 我引用谁 include

例:lamp第一种方法中,将安装、配置、启动分别保存3个文件, 由一个总文件引用

init.sls文件内容

  1. include:
  2. - lamp.lamp_pkg
  3. - lamp.lamp_config
  4. - lamp.lamp_service

lamp_pkg.sls文件内容

  1. lamp-pkg:
  2. pkg.installed:
  3. - pkgs:
  4. - httpd
  5. - php
  6. - mariadb
  7. - mariadb-server
  8. - php-mysql
  9. - php-cli
  10. - php-mbstring

lamp_config.sls文件内容

  1. apache-config:
  2. file.managed:
  3. - name: /etc/httpd/conf/httpd.conf
  4. - source: salt://lamp/files/httpd.conf
  5. - user: root
  6. - group: root
  7. - mode: 644
  8.  
  9. php-config:
  10. file.managed:
  11. - name: /etc/php.ini
  12. - source: salt://lamp/files/php.ini
  13. - user: root
  14. - group: root
  15. - mode: 644
  16.  
  17. mysql-config:
  18. file.managed:
  19. - name: /etc/my.cnf
  20. - source: salt://lamp/files/my.cnf
  21. - user: root
  22. - group: root
  23. - mode: 644
  24. - require_in:
  25. - service: mysql-service

lamp_service.sls文件内容

  1. apache-service:
  2. service.running:
  3. - name: httpd
  4. - enable: True
  5. - reload: True
  6. - require:
  7. - pkg: lamp-pkg
  8. - watch:
  9. - file: apache-config
  10.  
  11. mysql-service:
  12. service.running:
  13. - name: mariadb
  14. - enable: True
  15. - reload: True

执行命令:salt 'linux-node2*' state.sls lamp.init

6 我扩展谁

如何编写SLS技巧:

1.按状态分类 如果单独使用,很清晰。

2.按服务分类 可以被其他的SLS include。例如LNMP include mysql的服务。

jinja2

文档:http://docs.jinkan.org/docs/jinja2/

模板包含 变量 或 表达式,两种分隔符: {% ... %} 和 {{ ... }} 。前者用于执行诸如 for 循环 或赋值的语句,后者把表达式的结果打印到模板上。

salt中如何使用jinja2:

文档:https://docs.saltstack.com/en/latest/topics/jinja/index.html

  1)告诉File模块,你要使用jinja

  1. apache-config:
  2. file.managed:
  3. - name: /etc/httpd/conf/httpd.conf
  4. - source: salt://lamp/files/httpd.conf
  5. - user: root
  6. - group: root
  7. - mode: 644
  8. - template: jinja

  2)列出参数列表

  1. apache-config:
  2. file.managed:
  3. - name: /etc/httpd/conf/httpd.conf
  4. - source: salt://lamp/files/httpd.conf
  5. - user: root
  6. - group: root
  7. - mode: 644
  8. - template: jinja
  9. - defaults:
  10. PORT: 8080

  3)模板引用

httpd.conf配置文件引用如下

执行命令:salt 'linux-node2*' state.sls lamp.init

执行结果:

  1. linux-node2.example.com:
  2. ----------
  3. ID: lamp-pkg
  4. Function: pkg.installed
  5. Result: True
  6. Comment: All specified packages are already installed
  7. Started: ::02.903236
  8. Duration: 4591.748 ms
  9. Changes:
  10. ----------
  11. ID: apache-config
  12. Function: file.managed
  13. Name: /etc/httpd/conf/httpd.conf
  14. Result: True
  15. Comment: File /etc/httpd/conf/httpd.conf updated
  16. Started: ::07.558365
  17. Duration: 90.859 ms
  18. Changes:
  19. ----------
  20. diff:
  21. ---
  22. +++
  23. @@ -, +, @@
  24. # prevent Apache from glomming onto all bound IP addresses.
  25. #
  26. #Listen 12.34.56.78:
  27. -Listen
  28. +Listen
  29.  
  30. #
  31. # Dynamic Shared Object (DSO) Support
  32. ----------
  33. ID: php-config
  34. Function: file.managed
  35. Name: /etc/php.ini
  36. Result: True
  37. Comment: File /etc/php.ini is in the correct state
  38. Started: ::07.649429
  39. Duration: 63.754 ms
  40. Changes:
  41. ----------
  42. ID: mysql-config
  43. Function: file.managed
  44. Name: /etc/my.cnf
  45. Result: True
  46. Comment: File /etc/my.cnf is in the correct state
  47. Started: ::07.713515
  48. Duration: 49.273 ms
  49. Changes:
  50. ----------
  51. ID: apache-service
  52. Function: service.running
  53. Name: httpd
  54. Result: True
  55. Comment: Service reloaded
  56. Started: ::07.800629
  57. Duration: 135.15 ms
  58. Changes:
  59. ----------
  60. httpd:
  61. True
  62. ----------
  63. ID: mysql-service
  64. Function: service.running
  65. Name: mariadb
  66. Result: True
  67. Comment: The service mariadb is already running
  68. Started: ::07.936165
  69. Duration: 95.71 ms
  70. Changes:
  71.  
  72. Summary for linux-node2.example.com
  73. ------------
  74. Succeeded: (changed=)
  75. Failed:
  76. ------------
  77. Total states run:
  78. Total run time: 5.026 s

 - 模板里面支持: salt执行模块 grinas 进行赋值

例:修改配置文件httpd.conf,将IP地址指向本机IP,通过grains['fqdn_ip4'][0]可以获取本机IP地址

salt 'linux-node2*' grains.item fqdn_ip4

 

- 模板里面支持salt远程执行模块

例:修改配置文件httpd.conf,{{ salt['netwrok.hw_addr']('eth0') }}

salt 'linux-node2*' network.hw_addr eth0

执行命令:salt 'linux-node2*' state.sls lamp.init

执行结果

  1. linux-node2.example.com:
  2. ----------
  3. ID: lamp-pkg
  4. Function: pkg.installed
  5. Result: True
  6. Comment: All specified packages are already installed
  7. Started: ::57.213758
  8. Duration: 664.953 ms
  9. Changes:
  10. ----------
  11. ID: apache-config
  12. Function: file.managed
  13. Name: /etc/httpd/conf/httpd.conf
  14. Result: True
  15. Comment: File /etc/httpd/conf/httpd.conf updated
  16. Started: ::57.880642
  17. Duration: 82.912 ms
  18. Changes:
  19. ----------
  20. diff:
  21. ---
  22. +++
  23. @@ -, +, @@
  24. # prevent Apache from glomming onto all bound IP addresses.
  25. #
  26. #Listen 12.34.56.78:
  27. -Listen
  28. +Listen 192.168.137.12:
  29. +
  30. +# MAC IS: :0c::fd:dd:
  31.  
  32. #
  33. # Dynamic Shared Object (DSO) Support
  34. ----------
  35. ID: php-config
  36. Function: file.managed
  37. Name: /etc/php.ini
  38. Result: True
  39. Comment: File /etc/php.ini is in the correct state
  40. Started: ::57.963715
  41. Duration: 14.577 ms
  42. Changes:
  43. ----------
  44. ID: mysql-config
  45. Function: file.managed
  46. Name: /etc/my.cnf
  47. Result: True
  48. Comment: File /etc/my.cnf is in the correct state
  49. Started: ::57.978393
  50. Duration: 12.482 ms
  51. Changes:
  52. ----------
  53. ID: apache-service
  54. Function: service.running
  55. Name: httpd
  56. Result: True
  57. Comment: Service reloaded
  58. Started: ::58.021471
  59. Duration: 127.043 ms
  60. Changes:
  61. ----------
  62. httpd:
  63. True
  64. ----------
  65. ID: mysql-service
  66. Function: service.running
  67. Name: mariadb
  68. Result: True
  69. Comment: The service mariadb is already running
  70. Started: ::58.148913
  71. Duration: 58.592 ms
  72. Changes:
  73.  
  74. Summary for linux-node2.example.com
  75. ------------
  76. Succeeded: (changed=)
  77. Failed:
  78. ------------
  79. Total states run:
  80. Total run time: 960.559 ms

 - 模板里面支持 salt执行模块 pillar进行赋值

例:修改配置文件httpd.conf,{{ pillar['apache'] }}

salt 'linux-node2*' pillar.item apache

执行命令:salt 'linux-node2*' state.sls lamp.init

执行结果:

  1. linux-node2.example.com:
  2. ----------
  3. ID: lamp-pkg
  4. Function: pkg.installed
  5. Result: True
  6. Comment: All specified packages are already installed
  7. Started: ::16.490143
  8. Duration: 712.121 ms
  9. Changes:
  10. ----------
  11. ID: apache-config
  12. Function: file.managed
  13. Name: /etc/httpd/conf/httpd.conf
  14. Result: True
  15. Comment: File /etc/httpd/conf/httpd.conf updated
  16. Started: ::17.204369
  17. Duration: 93.136 ms
  18. Changes:
  19. ----------
  20. diff:
  21. ---
  22. +++
  23. @@ -, +, @@
  24. Listen 192.168.137.12:
  25.  
  26. # MAC IS: :0c::fd:dd:
  27. +# pillar: httpd
  28.  
  29. #
  30. # Dynamic Shared Object (DSO) Support
  31. ----------
  32. ID: php-config
  33. Function: file.managed
  34. Name: /etc/php.ini
  35. Result: True
  36. Comment: File /etc/php.ini is in the correct state
  37. Started: ::17.297764
  38. Duration: 17.209 ms
  39. Changes:
  40. ----------
  41. ID: mysql-config
  42. Function: file.managed
  43. Name: /etc/my.cnf
  44. Result: True
  45. Comment: File /etc/my.cnf is in the correct state
  46. Started: ::17.315170
  47. Duration: 15.217 ms
  48. Changes:
  49. ----------
  50. ID: apache-service
  51. Function: service.running
  52. Name: httpd
  53. Result: True
  54. Comment: Service httpd is already enabled, and is running
  55. Started: ::17.331369
  56. Duration: 184.591 ms
  57. Changes:
  58. ----------
  59. httpd:
  60. True
  61. ----------
  62. ID: mysql-service
  63. Function: service.running
  64. Name: mariadb
  65. Result: True
  66. Comment: The service mariadb is already running
  67. Started: ::17.516431
  68. Duration: 32.057 ms
  69. Changes:
  70.  
  71. Summary for linux-node2.example.com
  72. ------------
  73. Succeeded: (changed=)
  74. Failed:
  75. ------------
  76. Total states run:
  77. Total run time: 1.054 s

SaltStack配置管理之状态模块和jinja2(五)的更多相关文章

  1. SaltStack配置管理-LAMP状态设计

    上一篇:SaltStack之Salt-ssh 配置文件模板 apache: pkg.installed: - name: httpd service.running: - name: httpd /e ...

  2. SaltStack配置管理-状态间关系

    上一篇:SaltStack配置管理-LAMP状态设计 include包含 上篇安装LAMP环境是一个个环境安装,可以通过include模块全部安装 lamp.sls include: - apache ...

  3. 架构师成长之路5.3-Saltstack配置管理(State状态模块)

    点击架构师成长之路 架构师成长之路5.3-Saltstack配置管理(State状态模块) 配置管理工具: Pupper:1. 采用ruby编程语言:2. 安装环境相对较复杂:3.不支持远程执行,需要 ...

  4. 009(1)-saltstack之salt-ssh的使用及配置管理LAMP状态的实现

    1 salt-ssh的使用 1. 安装salt-ssh[root@slave1 .ssh]# yum install -y salt-ssh 2. 配置salt-ssh # Sample salt-s ...

  5. SaltStack的salt-ssh使用及LAMP状态设计部署(五)

    一.salt-ssh的使用 官方文档:https://docs.saltstack.com/en/2016.11/topics/ssh/index.html (1)安装salt-ssh [root@l ...

  6. SaltStack配置管理--状态间的关系(六)

    一.include的引用 需求场景:用于含有多个SLS的状态,使用include可以进行多个状态的组合,将安装apache,php,mysql集合在一个sls中 [root@7mini-node1 p ...

  7. Saltstack_使用指南10_配置管理-状态模块

    1. 主机规划 salt 版本 [root@salt100 ~]# salt --version salt (Oxygen) [root@salt100 ~]# salt-minion --versi ...

  8. saltStack 状态模块(状态间的关系)

    unless onlyif:状态间的条件判断,主要用于cmd状态模块 常用方法:    onlyif:检查的命令,仅当'onlyif'  选项指向的命令返回true时才执行name 定义的命 unle ...

  9. Saltstack 安装与常用模块

    一.介绍 saltstack是基于C/S服务模式,在该架构中,服务器端叫做Master,客户端叫做Minion.传统的C/S模式我们这样理解,客户端发送请求给服务器端,服务器端接受到来自客户端的请求并 ...

随机推荐

  1. Firefox about

    在firefox的地址栏输入about:about,然后看一下各个链接.有的链接有具体的用途,有的链接疯言疯语,并无软用. about:about集中了火狐浏览器的全部用户界面,平时常见的prefer ...

  2. 【Zeyphr】保存json到数据库

    方法一: public int SaveJob(JObject data) { var formWrapper = RequestWrapper.Instance().LoadSettingXmlSt ...

  3. 【JavaEE企业应用实战学习记录】servlet3.0上传文件

    <%-- Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 14:20 To change this tem ...

  4. hello Cookie

    Cookie 是什么? Cookie在浏览器中的表现为请求头域和响应头域的字段,也就是伴随着请求和响应的一组键值对的文本.Cookie来源于服务器,第一次请求无Cookie参数,增加Cookie通过服 ...

  5. Android怎么使用字体图标 自定义FontTextView字体图标控件-- 使用方法

    首先我想说明一下字体图标的好处,最大的好处就是自适应了,而且是使用TextView 不用去切图,是矢量图 灵活调用 第一步我要说明一下一般字体图标的来源,我这里使用的是  --阿里巴巴矢量图标库 -网 ...

  6. iOS开发中的错误整理,IOS9中canOpenURL调用失败分析

    由于IOS加入对用户隐私以及禁止扫描系统信息的控制,目前通过canOpenURL的方法来判断用户是否安装特定app,则会出现-canOpenURL: failed for URL: "ABC ...

  7. epon e8-c HG220GS超级密码破解

    网上找了很多管理电信e8-c的破解资料,大多都是明文密码,而hg220gs则为加密的密码,找来找去最后终于找到加密方式了base64,真心不容易 下面从其他博文中转载过来留着记录 低端hack.主要是 ...

  8. 16 IO操作文件读写

    IO的分类 第一种分法: 1.输入流 2.输出流 第二种分法: 1.字节流 2.字符流 第三种分法: 1.节点流 2.处理流 I/O当中的核心类: InputStream  <--------F ...

  9. Vi命令:如何删除全部内容?

    在命令模式下,输入:.,$d 一回车就全没了. 表示从当前行到末行全部删除掉. 用gg表示移动到首行.

  10. DFS经典题,reachable or not in a 2D maze

    [[0, 0, 0, 0, 0, 1], [1, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0], [0, 1, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0], ...