我们假设user01
定义了两个组:groupA
and groupB
(除了主要组之外)。
我可以将帐户添加到groupC
(确保user01
属于groupC
) using:
- user: name=user01 groups=groupC append=yes
我怎样才能删除user01
from groupB
(确保user01
不属于groupB
)而不指定帐户应属于的所有组?
据我所知,你不能只使用普通的用户模块。
然而,对于一些相当疯狂的旋转,你可以在剧本中完成。
但我不确定我是否推荐这个;这只是一个有趣的练习。 (我确实测试过这个并且它有效。)
有趣的部分是“构建新组列表”任务,该任务删除列表条目。如果在 python 列表上调用 .remove() 返回新列表,那么这都是不必要的。
---
- hosts: target
gather_facts: no
vars:
group_to_remove: admins
new_groups_list: []
user_to_check: user1
tasks:
- user: name="{{ user_to_check }}" groups=testers,developers,admins
- name: get the current groups list
command: groups "{{ user_to_check }}"
register: current_groups
- debug: var=current_groups
# parse the output of the groups command into a python list
# of the current groups
- set_fact:
current_group_list: "{{ current_groups.stdout.replace( user_to_check+' : ','').split(' ') }}"
- name: show user_group_list
debug: var=current_group_list
- name: build the new groups list
set_fact:
new_groups_list: "{{ new_groups_list + [ item ] }}"
no_log: False
when: "not '{{ group_to_remove }}' == '{{ item }}'"
with_items: "{{ current_group_list }}"
# turn the list, into a comma-delimited string
- set_fact:
new_groups: "{{ ','.join(new_groups_list) }}"
- name: show new_groups_list
debug: var=new_groups
- name: set new user groups
user: name="{{ user_to_check }}" groups="{{ new_groups }}"
- name: get the new groups list
command: groups "{{ user_to_check }}"
register: new_groups
- debug: var=new_groups
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)