ansible批量加用户
1、生成密码
pip install passlib
python -c "from passlib.hash import sha512_crypt; print sha512_crypt.encrypt('123456')"
2、文件一 hosts.yaml
all:
vars:
ansible_connection: ssh
ansible_user: zhangs
ansible_ssh_pass: zhangs@123456
ansible_sudo_pass: zhangs@123456
ansible_sudo: true
ansible_sudo_user: root
become: yes
children:
newhosts:
vars:
user_name: lis
user_passwd: "$6$rounds=656000$iK2VjWcO/smQYSZ3$rTr6sbEDRUlWM47Ak72oYqNl8LYrMhXEjFJI..f5gVpTIiRiWvcyd5kWxuDvdDe6LASVXU3cYJkd1NjZrxnBW1"
hosts:
192.168.0.0:
3、文件二 playbook.yaml
- name: add admin user
hosts: newhosts
become: yes
become_method: sudo
vars:
current_date: "{{ansible_date_time.date}}"
sudoer_path: /etc/sudoers.d
user_sudo_file: "{{sudoer_path}}/{{user_name}}"
tasks:
- name: add user
user:
name: "{{user_name}}"
password: "{{user_passwd}}"
home: "/home/{{user_name}}"
- name: create sudo file
shell: "touch {{user_sudo_file}}"
- name: add sudoers
shell: "echo '{{user_name}} ALL=(ALL) NOPASSWD:ALL' > {{user_sudo_file}}"
- name: chmod sudo file
shell: "chmod 400 {{user_sudo_file}}"
4、执行
ansible-playbook ./playbook.yaml -i hosts.yaml