salt 使用state的sls来批量管理用户

参考:http://www.saltstack.cn/kb/managing-user-with-salt/

       http://blog.csdn.net/hnhuangyiyang/article/details/50412502

使用sls的方法添加多用户

先配置需要添加的用户名,我使用pillar的信息来存储用户的名称

编写用户列表在pillar数据中
[root@cong-33 salt]# cat pillar/user.sls
user:
  - cong
  - qiang
  - lin
  - huang
[root@cong-33 salt]#
刷新pillar到minoin中
 
[root@cong-33 salt]# salt 'cong-55' saltutil.refresh_pillar

上面的sls文件使用了jinja的格式,

执行上面sls文件,添加用户
[root@cong-33 salt]# salt 'cong-55' state.sls useradd
可以在命令的后面添加测试的选项 test=True  这样就测试,而不是真正的执行
编写state的sls文件,添加用户
 
[root@cong-33 salt]# cat useradd.sls
{% for USERNAME in pillar['user']%}
{{ USERNAME }}:
  user.present:
  - fullname: {{ USERNAME}}
  - shell: /bin/bash
{% endfor %}
[root@cong-33 salt]#
编写删除用户的sls文件
[root@cong-33 salt]# cat useradd.sls
{% for USERNAME in pillar['user']%}
{{ USERNAME }}:
  user.absent:
  - purge: True
{% endfor %}
[root@cong-33 salt]#
 

然后在执行这个文件就可以删除用户了

参考学习了别人的例子,发现还是别人的方便,简单

{% set users = ['jim','hwg','hyxc'] %}        #采用jinja语言  
{% for user in users %}  
{{ user }}:  
  user.present:  
    - shell: /bin/bash  
    - password: '$6$hyxchyxc$r2R3uQcnhIl8QSg7WrmojiJqRhD0s6KVYGvXZCswGwW9GDeLF4mhdQJ/Pq8ccWkdzkyYSpxnGqxciMJ53WPI//'  
    - gid: 650  
    - groups:  
      - jim  
{% endfor %}  

密码的hash方法

salt '10.0.10.100' shadow.gen_password 'password'     默认为sha512  

 也可以使用openssl等工具来实现。 

  1. nt crypt.crypt('password', '$6$hyxchyxc')"  

其中$6代表sha512

哈希类型支持如下几种:

[html] view plain copy
 
  1.         Type    Length  
  2.         ======  ======  
  3. $6    sha512     128  
  4. $5    sha384      96  
  5. $4    sha256      64  
  6. $3    sha224      56  
  7. $2    sha1        40  
  8. $1    md5         32  

方法2.在命令行执行如下命令

[html] view plain copy
 
  1. salt '10.0.10.100' shadow.gen_password 'password'     默认为sha512  

2.批量创建用户

原文地址:https://www.cnblogs.com/LYCong/p/7889268.html