0%

Ansible 批量创建用户

Ansible创建多个用户与密码

  • create_user.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
---
- hosts: local
gather_facts: no
become: yes
become_method: sudo
become_user: root

tasks:
- include_vars: users.yml
- name: create users
user:
name: "{{ item.username }}"
state: present
password: "{{ '123456'|password_hash('sha512') }}"
with_items: "{{ users }}"

- name: add users to /etc/sudoers
lineinfile:
dest: /etc/sudoers
#regexp: '^root ALL=(ALL) ALL'
insertafter: '^root ALL=(ALL) ALL'
line: '{{ item.username }} ALL=(ALL) NOPASSWD:ALL'
validate: 'visudo -cf %s'
with_items: "{{ users }}"
  • users.yml
1
2
3
4
users:
- username: hyman01
- username: hyman02
- username: hyman03