请参考以下示例代码来实现Ansible识别UID<500的用户,并将其shell更改为/sbin/nologin。
- name: Identify users with UID < 500 and change their shell
hosts: all
become: true
gather_facts: false
tasks:
- name: Get list of users
command: getent passwd
register: users
- name: Change shell for users with UID < 500
lineinfile:
path: /etc/passwd
regexp: "^{{ item.split(':')[0] }}:"
line: "{{ item.split(':')[0] }}:x:{{ item.split(':')[2] }}:{{ item.split(':')[3] }}:{{ item.split(':')[4] }}:{{ item.split(':')[5] }}:{{ item.split(':')[6] }}:/sbin/nologin"
state: present
backup: yes
with_items: "{{ users.stdout_lines }}"
when: item.split(':')[2] | int < 500
这个例子中,我们首先使用getent passwd命令获取当前系统中的用户列表,并将结果存储在users变量中。然后,我们使用lineinfile模块来修改/etc/passwd文件中UID<500的用户的shell为/sbin/nologin。这里的with_items指令用于在每次迭代中针对每个用户运行lineinfile任务,只有当条件item.split(':')[2] | int < 500为真时才执行任务。
请注意,在运行此Ansible Playbook之前,请确保您有足够的权限来修改/etc/passwd文件。