Running the same task on 2 different groups , each group have different variables

Goal: Running the same task on different inventory group, each group with different variables

Setup:
In my inventory hosts file, I have 2 groups. Web-Server and Manager servers.
I have created a playbook that format and mounting disks, each group have a different number of disks and different mount point name. Therefore I have created VARs per group - but the tasks should be the same for the 2 groups.

How can I define the tasks to use the variables that are relevant to each group? I have managed to run it a single group

Host file

[web_server]
130.1.1.1
130.1.1.2
130.1.1.3

[managr_server]
130.1.1.5
130.1.1.6
130.1.1.7

Playbook:

- hosts: all
  become: true
  vars:
    web_server:
      - {drive: /dev/sdb ,mount_path: /data1, partition_id: 1}
      - {drive: /dev/sdc ,mount_path: /data2, partition_id: 1}

Host file

[web_server]
130.1.1.1
130.1.1.2
130.1.1.3

[managr_server]
130.1.1.5
130.1.1.6
130.1.1.7

Systemic Ansible-way would be to create a role and use group_vars. For example

1) Keep the inventory (fix "manager_server" group label)
2) Create a role e.g. "manage_drives" with the tasks
   https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html
   
   a) Use ansible-galaxy to create an empty role
   $ ansible-galaxy init manage_drives

   b) Create tasks.

   tasks/main.yml

    - name: create directory
      file:
        path: "{{ item.mount_path }}"
        state: directory
      loop: "{{ my_drives }}"

    - name: Create a new primary partition
      parted:
        device: "{{item.drive}}"
        number: 1
        state: present
      loop: "{{ my_drives }}"

    - name: Create a ext4 filesystem
      filesystem:
        fstype: ext4
        dev: "{{ item.drive }}{{ item.partition_id }}"
      loop: "{{ my_drives }}"

    - name: Mount drives
      mount:
        path: "{{ item.mount_path }}"
        src: "{{ item.drive }}{{ item.partition_id }}"
        fstype: ext4
        state: present
      loop: "{{ my_drives }"

    c) Review other artefacts of the role that you don't need right now
       (defaults, files, handlers, meta, templates, tests, vars).

    d) You might want to create empty list my_drives by default

    defaults/main.yml
    my_drives:

3) Create group_vars

$ tree group_vars/
group_vars/
├── manager_server.yml
└── web_server.yml

$ cat group_vars/manager_server.yml
my_drives:
  - {drive: /dev/sdb ,mount_path: /manager1, partition_id: 1}
  - {drive: /dev/sdc ,mount_path: /manager2, partition_id: 1}

$ cat group_vars/web_server.yml
my_drives:
  - {drive: /dev/sdb ,mount_path: /data1, partition_id: 1}
  - {drive: /dev/sdc ,mount_path: /data2, partition_id: 1}
  - {drive: /dev/sdd ,mount_path: /data3, partition_id: 1}

4) Create a playbook. Run it first with "--syntax-check" then with
   "--check". Then try it sharp.

- hosts: manager_server
  become: true
  roles:
    - manage_drives

- hosts: web_server
  become: true
  roles:
    - manage_drives

HTH,

  -vlado