我們假設user01
有兩組定義:groupA
和groupB
(除了主組)。如何從Ansible中的指定組中刪除用戶?
我的帳戶使用添加到groupC
(確保user01
屬於groupC
):
- user: name=user01 groups=groupC append=yes
如何刪除user01
從groupB
(確保user01
不屬於groupB
)不指定所有組帳戶應該屬於?
我們假設user01
有兩組定義:groupA
和groupB
(除了主組)。如何從Ansible中的指定組中刪除用戶?
我的帳戶使用添加到groupC
(確保user01
屬於groupC
):
- user: name=user01 groups=groupC append=yes
如何刪除user01
從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
這功能是缺失的,這是開放的錯誤爲:
https://github.com/ansible/ansible/issues/11024
至於解決方法,使用這樣的事情。
- name: remove telegraf user from varnish group become: true shell: "/usr/sbin/delgroup telegraf varnish" when: ansible_hostname | lower not in groups['varnish'] | lower