[关闭]
@nalan90 2017-08-31T17:53:12.000000Z 字数 16050 阅读 883

SaltStack

自动化运维


基本概念与工作原理

SaltStack架构主要涉及到三个开源软件,分别是 Python 软件集,SaltStack 软件集和 ZeroMQ 消息队列软件。

SaltStack 客户端(Minion)在启动时,会自动生成一套密钥,包含私钥和公钥。之后将公钥发送给服务器端,服务器端验证并接受公钥,以此来建立可靠且加密的通信连接。同时通过消息队列 ZeroMQ 在客户端与服务端之间建立消息发布连接。具体通信原理图。

image_1boo9cl641j7819vp1gea1tbfirg9.png-49kB


专业术语说明:


image_1boo9f0hd16ts141a1nqh12741bemm.png-58.3kB

原理图说明:


架构设计

image_1boo9h74jmpfgig1ff3hm1qrh13.png-57.3kB

说明:


环境安装

预安装准备

  1. ## 开启防火墙
  2. [root@master zhangshuang]# firewall-cmd --permanent --zone=public --add-port=4505-4506/tcp
  3. success
  4. [root@master zhangshuang]# firewall-cmd --reload
  5. success
  6. ## 安装yum源
  7. [root@master zhangshuang]# yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm

Master 角色的安装

  1. ## 安装salt-master
  2. [root@master zhangshuang]# yum install -y salt-master
  3. ## 设置开机自启salt-master
  4. [root@master zhangshuang]# systemctl enable salt-master
  5. [root@master zhangshuang]# systemctl start salt-master
  6. ## 查看端口连接
  7. [root@master zhangshuang]# netstat -anltp
  8. Active Internet connections (servers and established)
  9. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  10. tcp 0 0 172.16.1.162:4505 172.16.1.165:58376 ESTABLISHED 22000/python
  11. tcp 0 0 172.16.1.162:4505 172.16.1.164:42518 ESTABLISHED 22000/python
  12. tcp 0 0 172.16.1.162:4506 172.16.1.165:41972 ESTABLISHED 22016/python
  13. tcp 0 0 172.16.1.162:4506 172.16.1.161:52710 ESTABLISHED 22016/python
  14. tcp 0 0 172.16.1.162:4506 172.16.1.163:39124 ESTABLISHED 22016/python
  15. tcp 0 0 172.16.1.162:4505 172.16.1.163:37832 ESTABLISHED 22000/python
  16. tcp 0 0 172.16.1.162:4506 172.16.1.164:60848 ESTABLISHED 22016/python
  17. tcp 0 0 172.16.1.162:4505 172.16.1.161:50256 ESTABLISHED 22000/python

Minion 角色的安装

  1. ## 安装salt-minion
  2. [root@slave1 zhangshuang]# yum install -y salt-minion
  3. ## 修改Master主机地址
  4. [root@slave1 zhangshuang]# vim /etc/salt/minion
  5. master: 172.16.1.162
  6. ## 设置开机自启salt-minion
  7. [root@slave1 zhangshuang]# systemctl enable salt-minion
  8. [root@slave1 zhangshuang]# systemctl start salt-minion
  9. [root@slave1 zhangshuang]# netstat -anltp
  10. Active Internet connections (servers and established)
  11. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  12. tcp 0 0 172.16.1.163:39124 172.16.1.162:4506 ESTABLISHED 20560/python
  13. tcp 0 0 172.16.1.163:37832 172.16.1.162:4505 ESTABLISHED 20560/python

授权认证
  1. ## 接受所有minions的认证请求
  2. [root@master zhangshuang]# salt-key -A
  3. ## 查看所有minions的认证列表
  4. [root@master zhangshuang]# salt-key -L
  5. Accepted Keys:
  6. dev-161
  7. slave2
  8. slave3
  9. slave1
  10. Denied Keys:
  11. Unaccepted Keys:
  12. Rejected Keys:

Grains收集系统信息
  1. ## 查看grains模块的所有functions
  2. [root@master zhangshuang]# salt dev-161 sys.list_functions grains
  3. dev-161:
  4. - grains.append
  5. - grains.delval
  6. - grains.filter_by
  7. - grains.get
  8. - grains.get_or_set_hash
  9. - grains.has_value
  10. - grains.item
  11. - grains.items
  12. - grains.ls
  13. - grains.remove
  14. - grains.setval
  15. - grains.setvals
  16. ----------------------------------------
  17. ## 查看grains可以收集的所有系统项
  18. [root@master zhangshuang]# salt dev-161 grains.ls
  19. dev-161:
  20. - SSDs
  21. - biosreleasedate
  22. - biosversion
  23. - cpu_flags
  24. .....
  25. - os
  26. - os_family
  27. - osarch
  28. - oscodename
  29. - osfinger
  30. - osfullname
  31. - osmajorrelease
  32. - osrelease
  33. - osrelease_info
  34. ----------------------------------------
  35. ## 查看所有项的具体内容
  36. [root@master zhangshuang]# salt dev-161 grains.items
  37. ----------------------------------------
  38. ## 查看提定项的信息
  39. [root@master zhangshuang]# salt dev-161 grains.item os
  40. dev-161:
  41. ----------
  42. os:
  43. CentOS
  44. ----------------------------------------
  45. ## 查看minions IP
  46. [root@master zhangshuang]# salt '*' grains.item ipv4
  47. slave2:
  48. ----------
  49. ipv4:
  50. - 127.0.0.1
  51. - 172.16.1.164
  52. - 172.20.0.1
  53. dev-161:
  54. ----------
  55. ipv4:
  56. - 127.0.0.1
  57. - 172.16.1.161
  58. - 172.20.0.1
  59. slave3:
  60. ----------
  61. ipv4:
  62. - 127.0.0.1
  63. - 172.16.1.165
  64. - 172.20.0.1
  65. slave1:
  66. ----------
  67. ipv4:
  68. - 127.0.0.1
  69. - 172.16.1.163
  70. - 172.20.0.1

minions名称匹配
  1. ## 全局匹配
  2. root@master zhangshuang]# salt '*' test.ping
  3. slave2:
  4. True
  5. slave1:
  6. True
  7. dev-161:
  8. True
  9. slave3:
  10. True
  11. [root@master zhangshuang]# salt dev-161 test.ping
  12. dev-161:
  13. True
  14. [root@master zhangshuang]# salt '*-161' test.ping
  15. dev-161:
  16. True
  17. [root@master zhangshuang]# salt 'dev-???' test.ping
  18. dev-161:
  19. True
  20. [root@master zhangshuang]# salt 'slave[0-9]' test.ping
  21. slave1:
  22. True
  23. slave2:
  24. True
  25. slave3:
  26. True
  27. ----------------------------------------
  28. ## 正则匹配
  29. [root@master zhangshuang]# salt -E 'dev' test.ping
  30. dev-161:
  31. True
  32. [root@master zhangshuang]# salt -E 'slave' test.ping
  33. slave1:
  34. True
  35. slave2:
  36. True
  37. slave3:
  38. True
  39. [root@master zhangshuang]# salt -E ".*" test.ping
  40. slave2:
  41. True
  42. slave1:
  43. True
  44. dev-161:
  45. True
  46. slave3:
  47. True
  48. [root@master zhangshuang]# salt -E 'dev-[0-9]{3}' test.ping
  49. dev-161:
  50. True
  51. ----------------------------------------
  52. ## 列表匹配
  53. [root@master zhangshuang]# salt -L 'slave1,slave2,slave3' test.ping
  54. slave1:
  55. True
  56. slave2:
  57. True
  58. slave3:
  59. True
  60. ## 配置/etc/salt/master nodegroups字段,暂未配置
  61. [root@master zhangshuang]# salt -N minions test.ping
  62. Node group minions unavailable in /etc/salt/master
常用命令
  1. ## 列出dev-161 minion的所有模块
  2. [root@master zhangshuang]# salt dev-161 sys.list_modules
  3. dev-161:
  4. - acl
  5. - aliases
  6. - apache
  7. - artifactory
  8. ......
  9. - xfs
  10. ## 列出dev-161 minion test module的所有function
  11. [root@master zhangshuang]# salt dev-161 sys.list_functions test
  12. dev-161:
  13. - test.arg
  14. - test.echo
  15. .....
  16. - test.version
  17. - test.versions_report
  18. ----------------------------------------
  19. ## 查看test.ping的使用文档
  20. [root@master zhangshuang]# salt dev-161 sys.doc test.ping
  21. 'test.ping:'
  22. Used to make sure the minion is up and responding. Not an ICMP ping.
  23. Returns ``True``.
  24. CLI Example:
  25. salt '*' test.ping
  26. ----------------------------------------
  27. ## 查看所有minions的系统版本信息
  28. [root@master zhangshuang]# salt '*' cmd.run 'uname -r'
  29. slave1:
  30. 3.10.0-514.el7.x86_64
  31. dev-161:
  32. 3.10.0-514.el7.x86_64
  33. slave2:
  34. 3.10.0-514.el7.x86_64
  35. slave3:
  36. 3.10.0-514.el7.x86_64
  37. ----------------------------------------
  38. ## 包管理
  39. [root@master zhangshuang]# salt dev-161 pkg.install httpd
  40. dev-161:
  41. ----------
  42. [root@master zhangshuang]# salt dev-161 pkg.version httpd
  43. dev-161:
  44. 2.4.6-45.el7.centos.4
  45. [root@master zhangshuang]# salt dev-161 service.status httpd
  46. dev-161:
  47. True
  48. [root@master zhangshuang]# salt dev-161 service.stop httpd
  49. dev-161:
  50. True
  51. [root@master zhangshuang]# salt dev-161 service.status httpd
  52. dev-161:
  53. False
  54. [root@master zhangshuang]# salt dev-161 pkg.remove httpd
  55. dev-161:
  56. ----------
  57. httpd:
  58. ----------
  59. new:
  60. old:
  61. 2.4.6-45.el7.centos.4
  62. ----------------------------------------
  63. ## 文件管理
  64. [root@master zhangshuang]# salt dev-161 file.stats /etc/yum.conf
  65. dev-161:
  66. ----------
  67. atime:
  68. 1503995808.45
  69. ctime:
  70. 1487821839.52
  71. gid:
  72. 0
  73. group:
  74. root
  75. inode:
  76. 67191933
  77. mode:
  78. 0644
  79. mtime:
  80. 1479223823.0
  81. size:
  82. 970
  83. target:
  84. /etc/yum.conf
  85. type:
  86. file
  87. uid:
  88. 0
  89. user:
  90. root
  91. [root@master zhangshuang]# salt dev-161 file.chown /etc/passwd root root
  92. dev-161:
  93. None
  94. ----------------------------------------
  95. ## 用户管理
  96. [root@master zhangshuang]# salt dev-161 user.add mysql
  97. dev-161:
  98. True
  99. [root@master zhangshuang]# salt dev-161 cmd.run 'ls -l /home'
  100. dev-161:
  101. total 0
  102. drwx------. 3 gitlab-runner gitlab-runner 74 Mar 8 08:27 gitlab-runner
  103. drwx------ 5 hadoop hadoop 137 Aug 25 09:40 hadoop
  104. drwx------. 3 1000 1000 74 Mar 8 08:27 jenkins
  105. drwx------ 2 mysql mysql 62 Aug 30 13:47 mysql
  106. drwx------. 4 shensi shensi 112 Mar 27 13:00 shensi
  107. drwx------. 4 2004 2004 112 Mar 27 13:00 shuran
  108. drwx------ 2 mysql mysql 62 Aug 29 17:57 test
  109. drwx------. 5 zhangshuang zhangshuang 147 Mar 27 13:09 zhangshuang
  110. drwx------. 4 2007 2007 112 Mar 27 13:00 zhanxin
  111. [root@master zhangshuang]# salt dev-161 user.info mysql
  112. dev-161:
  113. ----------
  114. fullname:
  115. gid:
  116. 2010
  117. groups:
  118. - mysql
  119. home:
  120. /home/mysql
  121. homephone:
  122. name:
  123. mysql
  124. passwd:
  125. x
  126. roomnumber:
  127. shell:
  128. /bin/bash
  129. uid:
  130. 2010
  131. workphone:
  132. [root@master zhangshuang]# salt dev-161 user.delete mysql
  133. dev-161:
  134. True

模块管理
  1. ## 编写自定义的模块
  2. [root@master zhangshuang]# mkdir -p /srv/salt/_modules
  3. [root@master zhangshuang]# vim /srv/salt/_modules/hello.py
  4. [root@master zhangshuang]# cat /srv/salt/_modules/hello.py
  5. def world():
  6. """
  7. This is my first function.
  8. CLI Example::
  9. salt '*' hello.world
  10. """
  11. return 'Hello, world!'
  12. ## 推送模块至minions
  13. [root@master zhangshuang]# salt '*' saltutil.sync_modules
  14. slave1:
  15. - modules.hello
  16. salve2:
  17. - modules.hello
  18. dev-161:
  19. - modules.hello
  20. salve3:
  21. - modules.hello
  22. [root@master zhangshuang]# salt '*' hello.world
  23. salve3:
  24. Hello, world!
  25. dev-161:
  26. Hello, world!
  27. salve2:
  28. Hello, world!
  29. slave1:
  30. Hello, world!

state 模块使用

  1. [root@master salt]# salt dev-161 sys.list_functions state
  2. dev-161:
  3. - state.apply
  4. - state.check_request
  5. - state.clear_cache
  6. - state.clear_request
  7. - state.disable
  8. - state.enable
  9. - state.high
  10. - state.highstate
  11. - state.list_disabled
  12. - state.low
  13. - state.pkg
  14. - state.request
  15. - state.run_request
  16. - state.running
  17. - state.show_highstate
  18. - state.show_low_sls
  19. - state.show_lowstate
  20. - state.show_sls
  21. - state.show_top
  22. - state.single
  23. - state.sls
  24. - state.sls_id
  25. - state.template
  26. - state.template_str
  27. - state.top
  28. ----------------------------------------
  29. ## 编写SLS文件
  30. [root@master salt]# pwd
  31. /srv/salt
  32. [root@master salt]# vim apache.sls
  33. [root@master salt]# cat apache.sls
  34. install_httpd:
  35. pkg.installed:
  36. - name: httpd
  37. ## 安装httpd
  38. [root@master salt]# salt '*' state.sls apache
  39. salve2:
  40. ----------
  41. ID: install_httpd
  42. Function: pkg.installed
  43. Name: httpd
  44. Result: True
  45. Comment: The following packages were installed/updated: httpd
  46. Started: 14:43:44.602919
  47. Duration: 49646.909 ms
  48. Changes:
  49. ----------
  50. apr:
  51. ----------
  52. new:
  53. 1.4.8-3.el7
  54. old:
  55. apr-util:
  56. ----------
  57. new:
  58. 1.5.2-6.el7
  59. old:
  60. httpd:
  61. ----------
  62. new:
  63. 2.4.6-45.el7.centos.4
  64. old:
  65. httpd-tools:
  66. ----------
  67. new:
  68. 2.4.6-45.el7.centos.4
  69. old:
  70. mailcap:
  71. ----------
  72. new:
  73. 2.1.41-2.el7
  74. old:
  75. Summary
  76. ------------
  77. Succeeded: 1 (changed=1)
  78. Failed: 0
  79. ------------
  80. Total states run: 1
  81. ----------------------------------------
  82. ## 查看所有state模块
  83. [root@master salt]# salt dev-161 sys.list_state_modules
  84. dev-161:
  85. - acl
  86. - alias
  87. - alternatives
  88. - apache
  89. ......
  90. - winrepo
  91. [root@master salt]# salt dev-161 sys.list_state_functions pkg
  92. dev-161:
  93. - pkg.installed
  94. - pkg.latest
  95. - pkg.mod_aggregate
  96. - pkg.mod_init
  97. - pkg.purged
  98. - pkg.removed
  99. - pkg.uptodate
  100. ----------------------------------------
  101. [root@master ~]# tree /srv/salt/
  102. /srv/salt/
  103. ├── apache.sls
  104. ├── httpd.conf
  105. └── _modules
  106. ├── hello.py
  107. └── prank.py
  108. 1 directory, 4 files
  109. [root@master ~]# tree /srv/salt/
  110. /srv/salt/
  111. ├── apache.sls
  112. ├── httpd.conf
  113. └── _modules
  114. ├── hello.py
  115. └── prank.py
  116. 1 directory, 4 files
  117. ## apache.sls内容
  118. [root@master ~]# cat /srv/salt/apache.sls
  119. install_httpd:
  120. pkg.installed:
  121. - name: httpd
  122. httpd_running:
  123. service.running:
  124. - name: httpd
  125. - enable: True
  126. - require:
  127. - pkg: install_httpd
  128. - watch:
  129. - file: httpd_conf
  130. httpd_conf:
  131. file.managed:
  132. - name: /etc/httpd/conf/httpd.conf
  133. - source: salt://httpd.conf
  134. - user: root
  135. - group: root
  136. - mode: 600
  137. ----------------------------------------
  138. ## 执行SLS文件命令
  139. [root@master ~]# salt dev-161 state.sls apache
  140. dev-161:
  141. ----------
  142. ID: install_httpd
  143. Function: pkg.installed
  144. Name: httpd
  145. Result: True
  146. Comment: Package httpd is already installed.
  147. Started: 15:03:07.779195
  148. Duration: 547.207 ms
  149. Changes:
  150. ----------
  151. ID: httpd_conf
  152. Function: file.managed
  153. Name: /etc/httpd/conf/httpd.conf
  154. Result: True
  155. Comment: File /etc/httpd/conf/httpd.conf updated
  156. Started: 15:03:08.328355
  157. Duration: 3.764 ms
  158. Changes:
  159. ----------
  160. mode:
  161. 0600
  162. ----------
  163. ID: httpd_running
  164. Function: service.running
  165. Name: httpd
  166. Result: True
  167. Comment: Service httpd has been enabled, and is running
  168. Started: 15:03:08.332237
  169. Duration: 527.233 ms
  170. Changes:
  171. ----------
  172. httpd:
  173. True
  174. Summary
  175. ------------
  176. Succeeded: 3 (changed=2)
  177. Failed: 0
  178. ------------
  179. Total states run: 3

state 实例
  1. ## 安装Nginx并启动
  2. ## SLS文件
  3. [root@master ~]# cat /srv/salt/nginx/init.sls
  4. [root@master ~]# cat /srv/salt/nginx/init.sls
  5. nginx:
  6. pkg:
  7. - installed
  8. service:
  9. - running
  10. - enable: True
  11. - user: nginx
  12. - require:
  13. - user: nginx
  14. - watch:
  15. - file: /etc/nginx/nginx.conf
  16. - file: /etc/nginx/sites-enabled/default
  17. user.present:
  18. - home: /home/nginx
  19. - shell: /bin/bash
  20. - gid: nginx
  21. - require:
  22. - group: nginx
  23. group.present:
  24. - require:
  25. - pkg: nginx
  26. configure_nginx:
  27. file.managed:
  28. - name: /etc/nginx/nginx.conf
  29. - source: salt://nginx/files/nginx.conf
  30. - user: nginx
  31. - group: nginx
  32. /etc/nginx/sites-enabled/default:
  33. file.absent
  34. ----------------------------------------
  35. ## 文件结构
  36. [root@master ~]# tree /srv/salt/nginx/
  37. /srv/salt/nginx/
  38. ├── files
  39.    └── nginx.conf
  40. └── init.sls
  41. 1 directory, 2 files
  42. ----------------------------------------
  43. ## 执行结果
  44. [root@master ~]# salt dev-161 state.sls nginx
  45. dev-161:
  46. ## 1、安装nginx
  47. ----------
  48. ID: nginx
  49. Function: pkg.installed
  50. Result: True
  51. Comment: Package nginx is already installed.
  52. Started: 17:02:02.257036
  53. Duration: 669.776 ms
  54. Changes:
  55. ## 2、创建nginx组
  56. ----------
  57. ID: nginx
  58. Function: group.present
  59. Result: True
  60. Comment: Group nginx is present and up to date
  61. Started: 17:02:02.928639
  62. Duration: 0.36 ms
  63. Changes:
  64. ## 3、创建nginx用户
  65. ----------
  66. ID: nginx
  67. Function: user.present
  68. Result: True
  69. Comment: User nginx is present and up to date
  70. Started: 17:02:02.929107
  71. Duration: 0.741 ms
  72. Changes:
  73. ## 4、复制本地nginx.conf到指定服务器
  74. ----------
  75. ID: configure_nginx
  76. Function: file.managed
  77. Name: /etc/nginx/nginx.conf
  78. Result: True
  79. Comment: File /etc/nginx/nginx.conf is in the correct state
  80. Started: 17:02:02.931000
  81. Duration: 3.591 ms
  82. Changes:
  83. ## 5、删除/etc/nginx/sites-enabled/default文件
  84. ----------
  85. ID: /etc/nginx/sites-enabled/default
  86. Function: file.absent
  87. Result: True
  88. Comment: File /etc/nginx/sites-enabled/default is not present
  89. Started: 17:02:02.934678
  90. Duration: 0.205 ms
  91. Changes:
  92. ## 6、启动nginx服务
  93. ----------
  94. ID: nginx
  95. Function: service.running
  96. Result: True
  97. Comment: Service nginx is already enabled, and is running
  98. Started: 17:02:02.935036
  99. Duration: 336.938 ms
  100. Changes:
  101. ----------
  102. nginx:
  103. True
  104. Summary
  105. ------------
  106. Succeeded: 6 (changed=1)
  107. Failed: 0
  108. ------------
  109. Total states run: 6

  1. ## 创建wilson用户并拷贝公钥
  2. ## SLS文件
  3. [root@master ~]# cat /srv/salt/useradd/init.sls
  4. wilson:
  5. user.present:
  6. - home: /home/wilson
  7. - shell: /bin/bash
  8. - gid: wilson
  9. - require:
  10. - group: wilson
  11. group.present:
  12. - name: wilson
  13. /home/wilson/.ssh/authorized_keys:
  14. file.managed:
  15. - source: salt://useradd/authorized_keys
  16. - user: wilson
  17. - group: wilson
  18. - mode: 600
  19. - require:
  20. - user: wilson
  21. - file: /home/wilson/.ssh
  22. /home/wilson/.ssh:
  23. file.directory:
  24. - user: wilson
  25. - group: wilson
  26. - mode: 700
  27. - require:
  28. - user: wilson
  29. ----------------------------------------
  30. ## 文件结构
  31. [root@master ~]# tree /srv/salt/useradd/
  32. /srv/salt/useradd/
  33. ├── authorized_keys
  34. └── init.sls
  35. 0 directories, 2 files
  36. ----------------------------------------
  37. ## 执行结果
  38. [root@master ~]# salt dev-161 state.sls useradd
  39. dev-161:
  40. ----------
  41. ID: wilson
  42. Function: group.present
  43. Result: True
  44. Comment: Group wilson is present and up to date
  45. Started: 17:37:47.683562
  46. Duration: 1.205 ms
  47. Changes:
  48. ----------
  49. ID: wilson
  50. Function: user.present
  51. Result: True
  52. Comment: User wilson is present and up to date
  53. Started: 17:37:47.684946
  54. Duration: 37.578 ms
  55. Changes:
  56. ----------
  57. ID: /home/wilson/.ssh
  58. Function: file.directory
  59. Result: True
  60. Comment: Directory /home/wilson/.ssh updated
  61. Started: 17:37:47.726595
  62. Duration: 1.553 ms
  63. Changes:
  64. ----------
  65. /home/wilson/.ssh:
  66. New Dir
  67. ----------
  68. ID: /home/wilson/.ssh/authorized_keys
  69. Function: file.managed
  70. Result: True
  71. Comment: File /home/wilson/.ssh/authorized_keys updated
  72. Started: 17:37:47.728318
  73. Duration: 8.479 ms
  74. Changes:
  75. ----------
  76. diff:
  77. New file
  78. group:
  79. wilson
  80. user:
  81. wilson
  82. Summary
  83. ------------
  84. Succeeded: 4 (changed=2)
  85. Failed: 0
  86. ------------
  87. Total states run: 4
  88. ----------------------------------------
  89. ## SLS使用变量
  90. [root@master useradd]# cat init.sls
  91. useradd:
  92. user.present:
  93. {% set name = pillar['name'] %}
  94. - name: {{ name }}
  95. - home: /home/{{ name }}
  96. - shell: /bin/bash
  97. - gid: {{ name }}
  98. - groups:
  99. - docker
  100. - {{ name }}
  101. - require:
  102. - group: {{ name }}
  103. group.present:
  104. - name: {{ name }}
  105. /home/{{ name }}/.ssh/authorized_keys:
  106. file.managed:
  107. - source: salt://useradd/authorized_keys
  108. - user: {{ name }}
  109. - group: {{ name }}
  110. - mode: 600
  111. - require:
  112. - user: {{ name }}
  113. - file: /home/{{ name }}/.ssh
  114. /home/{{ name }}/.ssh:
  115. file.directory:
  116. - user: {{ name }}
  117. - group: {{ name }}
  118. - mode: 700
  119. - require:
  120. - user: {{ name }}
  121. ## 执行结果
  122. [root@master useradd]# salt dev-161 state.sls useradd pillar='{"name":"jack"}'
  123. dev-161:
  124. ----------
  125. ID: useradd
  126. Function: group.present
  127. Name: jack
  128. Result: True
  129. Comment: New group jack created
  130. Started: 18:02:01.274312
  131. Duration: 60.186 ms
  132. Changes:
  133. ----------
  134. gid:
  135. 2011
  136. members:
  137. name:
  138. jack
  139. passwd:
  140. x
  141. ----------
  142. ID: useradd
  143. Function: user.present
  144. Name: jack
  145. Result: True
  146. Comment: New user jack created
  147. Started: 18:02:01.334737
  148. Duration: 46.215 ms
  149. Changes:
  150. ----------
  151. fullname:
  152. gid:
  153. 2011
  154. groups:
  155. - jack
  156. home:
  157. /home/jack
  158. homephone:
  159. name:
  160. jack
  161. passwd:
  162. x
  163. roomnumber:
  164. shell:
  165. /bin/bash
  166. uid:
  167. 2011
  168. workphone:
  169. ----------
  170. ID: /home/jack/.ssh
  171. Function: file.directory
  172. Result: True
  173. Comment: Directory /home/jack/.ssh updated
  174. Started: 18:02:01.384423
  175. Duration: 8.189 ms
  176. Changes:
  177. ----------
  178. /home/jack/.ssh:
  179. New Dir
  180. ----------
  181. ID: /home/jack/.ssh/authorized_keys
  182. Function: file.managed
  183. Result: True
  184. Comment: File /home/jack/.ssh/authorized_keys updated
  185. Started: 18:02:01.392797
  186. Duration: 7.8 ms
  187. Changes:
  188. ----------
  189. diff:
  190. New file
  191. group:
  192. jack
  193. user:
  194. jack
  195. Summary
  196. ------------
  197. Succeeded: 4 (changed=4)
  198. Failed: 0
  199. ------------
  200. Total states run: 4
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注