Ansible - 如果已经在其他组中,则从组 x 中删除用户

Ansible - remove user from group x if already in other group

我正在寻找从组 x 中删除用户的最简单方法,当他们已经在组 google-sudo 中时。我将用户存储在这种列表和字典组合的组变量中:

user_account:
  - name: jenny
    authorized_keys:
      - jenny_01
    groups:
      - "{% if not googlesudo.stat.exists %}sudo{% else %}google-sudo{% endif %}"

  - name: jerry
    authorized_keys:
      - jerry_01
    groups: 
      - "{% if not googlesudo.stat.exists %}sudo{% else %}google-sudo{% endif %}"
...

这些是我已经创建的任务:

- name: Check if google-sudo file exists
  stat:
    path: /etc/sudoers.d/google_sudo
  register: googlesudo
  tags:
    - add_user_group
    - remove_from_x

- debug: var=googlesudo verbosity=2
  tags:
    - add_user_group
    - remove_from_x

- debug:
    msg: "User account to create: {{ item.name }}"
  with_items: "{{ user_account }}"

- name: "Creating user"
  user:
    name: "{{ item.name }}"
    group: users
    shell: /bin/bash
  with_items: "{{ user_account }}"

- name: Add user to additional groups
  user:
    name: "{{ item.0.name }}"
    groups: "{{ item.1 }}"
    append: yes
  with_subelements:
    - "{{ user_account }}"
    - groups
  tags:
    - remove_from_x

- name: Check if user already in google-sudo
  command: "groups {{ item.name }}"
  with_items:
    - "{{ user_account }}"
  register: root_users
  tags:
    - remove_from_x

- name: View root users
  debug:
    msg: "{{ item }}"
    verbosity: 2
  with_items:
    - "{{ root_users }}"
  tags:
    - remove_from_x

- name: Save state
  set_fact:
    is_in_googlesudo: "{{ root_users.results.0.stdout_lines }}"
  tags:
     - remove_from_x

- name: List
  debug: msg='{{ user_account |json_query("[?groups==`google-sudo`]")}}'
  tags:
    - remove_from_x

- name: Remove from x group
  shell: "deluser {{ item.name }} x"
  with_items:
    - "{{ user_account }}"
  when: "'x in is_in_googlesudo' and 'google-sudo in is_in_googlesudo'"
  tags:
    - remove_from_x

我正在测试 json_query 是否在 google-sudox 组中提取用户名,但没有成功。尝试在定义组时列出用户,但是我得到空输出,使用这个:

msg='{{ user_account |json_query("[?groups==`google-sudo`]")}}'

我想知道在检查服务器(使用例如 groups 命令)或在 user_account -> [=19 中是否有任何更短的方法从组 x 中删除用户=] 如果他已经在 google-sudo.

可能有一些漂亮而优雅的方式来编写这段代码,我将不胜感激如何处理它的任何想法。

让我们简化数据,例如

  vars:
    my_group: "{{ googlesudo.stat.exists|ternary('google-sudo', 'sudo') }}"
    user_account:
      - name: jenny
        groups: "{{ my_group }}"
      - name: jerry
        groups: "{{ my_group }}"

代码按预期工作,例如

    - stat:
        path: /tmp/google_sudo
      register: googlesudo
    - debug:
        var: user_account
    - debug:
        msg: '{{ user_account|json_query("[?groups==`google-sudo`].name") }}'

如果 /tmp/google_sudo 存在则给出

  user_account:
  - groups: google-sudo
    name: jenny
  - groups: google-sudo
    name: jerry

  msg:
  - jenny
  - jerry

否则

  user_account:
  - groups: sudo
    name: jenny
  - groups: sudo
    name: jerry

  msg: []

问:"如果已经在另一个组中,则将用户从组 x 中删除。"

A:使用getent获取群组中的用户列表。例如,要列出组 sudo

中的用户
    - getent:
        database: group
    - debug:
        var: getent_group.sudo.2

如果用户是组 sudo 的成员,则测试操作,例如

    - debug:
        msg: "Remove user {{ item.name }} from group x"
      loop: "{{ user_account }}"
      when: item.name in getent_group.sudo.2

问:"将组作为列表。(可能我应该使用 contains 函数。)"

答:是的。使用 contains,例如给定列表

  vars:
    user_account:
      - name: jenny
        groups: [google-sudo, group2]
      - name: jerry
        groups: [google-sudo, group3]

任务

    - debug:
        msg: "{{ user_account|json_query('[?contains(groups, `group2`)].name') }}"

给予

  msg:
  - jenny