安装passlib模块
pip install passlib
生成密码
from passlib.hash import sha512_crypt
print(sha512_crypt.using(rounds=5000).hash("MyPassWord"))
记下输出的字符串$6$jBbNgHw4kiSOPjmQ$Py3pOVYj2FzLNFsKRfw8fNXzz6kIsdvgRn4ABAvFExQhtnCZaX/kiV.CigQXxnroguBavY8P9K3kMdy.2D1hv.
编写playbook
替换YOUR-HASHED-PASSWORD为你刚获得的密码加密串
---
- hosts: "all"
gather_facts: no
vars:
sre_user_name: sre
sre_hashed_pwd: YOUR-HASHED-PASSWORD
tasks:
- name: add {{sre_user_name}} user
user:
name: "{{sre_user_name}}"
groups: sudo,{{sre_user_name}}
password: "{{sre_hashed_pwd}}"
- name: set authorized_key for {{sre_user_name}}
authorized_key:
user: "{{sre_user_name}}"
comment: "{{ item }}@github"
key: https://github.com/{{ item }}.keys
with_items:
- github_userid_of_sre1
- github_userid_of_sre2
- github_userid_of_sre3
以上playbook先创建一个用户并设置密码,把用户加入到sudo组(默认ubuntu下sudo组可以使用sudo执行命令),然后从github接口获取sre们的公钥,前提是这些人在github正确配置了他们的公钥