2016-08-17 2 views
3

user01에는 groupAgroupB (1 차 그룹에 추가)이라는 두 그룹이 정의되어 있다고 가정 해 보겠습니다.Ansible에서 지정한 그룹의 사용자를 삭제하는 방법은 무엇입니까?

내가 사용 ( user01groupC에 속하는 보장) groupC에 계정을 추가 할 수 있습니다

: groupB에서 user01 (groupB에 속하지 않는 user01을 위해) 모든 그룹에게 계정을 지정하지 않고

- user: name=user01 groups=groupC append=yes 

을 내가 제거 할 수있는 방법 에 속해야합니까?

답변

3

내가 알 수있는 한, 일반 사용자 모듈만으로는 할 수 없습니다.

그러나 꽤 복잡한 미션을 사용하면 게임 플레이에서 할 수 있습니다. 그래도 나는 이것을 추천하지 않는다. 그것은 단지 흥미로운 운동이었습니다. (나는 이것을 시험해 보았다.)

재미있는 부분은 "새 그룹 목록 작성"작업으로 목록 항목을 제거합니다. 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 
0

가 누락이 그에 대한 개방 버그 :

해결 방법으로

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

관련 문제