用ansible脚本给系统用户修改登录密码
时间: 2024-05-09 13:15:59 浏览: 50
ansible-create-users:从列表创建用户
可以使用 `user` 模块和 `password` 插件来实现。以下是一个示例 playbook:
```yaml
---
- name: Change password for user
hosts: all
become: yes
vars_prompt:
- name: "user_name"
prompt: "Enter the user name"
private: no
- name: "new_password"
prompt: "Enter the new password"
private: yes
tasks:
- name: Set new password for user
user:
name: "{{ user_name }}"
password: "{{ new_password | password_hash('sha512') }}"
```
在运行 playbook 时,会提示你输入要修改密码的用户名称和新密码。
如果你想要将密码直接写入 playbook 中,而不是在运行 playbook 时输入,可以将 `vars_prompt` 部分移除,并将 `new_password` 变量设置为你想要的密码,如下所示:
```yaml
---
- name: Change password for user
hosts: all
become: yes
vars:
user_name: "myuser"
new_password: "mypassword"
tasks:
- name: Set new password for user
user:
name: "{{ user_name }}"
password: "{{ new_password | password_hash('sha512') }}"
```
这个 playbook 将会修改名为 `myuser` 的用户的密码为 `mypassword`。
阅读全文