Junior — Middle
How to modify an existing Ansible playbook, designed to create a single user, so that it can automatically create dozens of users, including administrators?
sobes.tech AI
Answer from AI
To modify an Ansible playbook for creating multiple users, you can use a loop such as with_items or loop, passing a list of users with their parameters.
Example:
- name: Create multiple users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups | default(omit) }}"
shell: "/bin/bash"
loop:
- { name: 'user1', groups: 'users' }
- { name: 'admin1', groups: 'admins' }
- { name: 'user2' }
In this example, multiple users with different groups are created. For administrators, you can specify the admins group or other necessary parameters. This approach makes it easy to scale user creation by simply adding new items to the list.