我需要通过ansible工具批量修改ubuntu的普通账号密码。所有服务器的密码都是统一的,且需要sudo权限
时间: 2024-04-30 14:22:35 浏览: 157
1. 创建一个包含所有服务器IP地址的主机清单文件(inventory file),命名为hosts,格式如下:
```
[ubuntu_servers]
server1 ansible_host=192.168.1.101
server2 ansible_host=192.168.1.102
server3 ansible_host=192.168.1.103
```
2. 创建一个playbook文件,命名为change_password.yml,内容如下:
```
- name: Change password for all users
hosts: ubuntu_servers
become: yes
vars:
new_password: mynewpassword # 修改为你想要设置的密码
tasks:
- name: Change password for user
user:
name: "{{ item }}"
password: "{{ new_password | password_hash('sha512') }}"
update_password: always
with_items:
- user1
- user2
- user3
when: item != 'root' # 排除root用户
- name: Test login with new password
command: echo "Login successful"
become_user: "{{ item }}"
with_items:
- user1
- user2
- user3
```
3. 运行playbook文件:
```
ansible-playbook -i hosts change_password.yml
```
该playbook将会批量修改所有ubuntu服务器上除root用户以外的用户账号密码为mynewpassword。修改密码后,会测试是否可以用新密码登录。
阅读全文