ansible 添加用户

ansible 添加用户:

---

- name: ansible playbook
  hosts: all
  remote_user: root
  become: yes
  tasks:
	- name: 添加用户
	  user:
		name: test
		state: present
		groups: wheel,sudo
		append: yes
	  tags:
		- user

测试:

ansible-playbook play.yml -t user

查看:

ansible all -a 'groups test'

如果想删除用户,可以将 state 设置成 absent, 如果想同时删除目录,则设置 remove 为 yes

	---

- name: ansible playbook
  hosts: all
  remote_user: root
  become: yes
  tasks:
	- name: 添加用户
	  user:
		name: test
		state: absent
		remove: yes
		groups: wheel,sudo
		append: yes
	  tags:
		- user

设置密码

---

- name: ansible playbook
  hosts: all
  remote_user: root
  become: yes
  tasks:
	- name: 添加用户
	  user:
		name: test
		state: present
		groups: wheel,sudo
		append: yes
		password: '加密后的密码'
	  tags:
		- user