2016-08-17 46 views
3

我们假设user01有两组定义:groupAgroupB(除了主组)。如何从Ansible中的指定组中删除用户?

我的帐户使用添加到groupC(确保user01属于groupC):

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

如何删除user01groupB(确保user01不属于groupB)不指定所有组帐户应该属于?

回答

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