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

**URL:** <https://forum.ansible.com/t/running-the-same-task-on-2-different-groups-each-group-have-different-variables/32153>\
**Category:** Ansible Project\
**Created:** [February 6, 2020, 6:25am UTC](https://forum.ansible.com/t/running-the-same-task-on-2-different-groups-each-group-have-different-variables/32153 "2020-02-06T06:25:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [February 6, 2020, 6:25am UTC](https://forum.ansible.com/t/running-the-same-task-on-2-different-groups-each-group-have-different-variables/32153/1 "2020-02-06T06:25:56Z")

</div>

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:

```auto
- 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}

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 6, 2020, 9:05am UTC](https://forum.ansible.com/t/running-the-same-task-on-2-different-groups-each-group-have-different-variables/32153/2 "2020-02-06T09:05:49Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_reuse\_roles.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html)  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;a) Use ansible-galaxy to create an empty role  
&nbsp;&nbsp;&nbsp;$ ansible-galaxy init manage\_drives

&nbsp;&nbsp;&nbsp;b) Create tasks.

&nbsp;&nbsp;&nbsp;tasks/main.yml

&nbsp;&nbsp;&nbsp;&nbsp;- name: create directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item.mount\_path }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_drives }}"

&nbsp;&nbsp;&nbsp;&nbsp;- name: Create a new primary partition  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parted:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device: "{{item.drive}}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number: 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_drives }}"

&nbsp;&nbsp;&nbsp;&nbsp;- name: Create a ext4 filesystem  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filesystem:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fstype: ext4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dev: "{{ item.drive }}{{ item.partition\_id }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_drives }}"

&nbsp;&nbsp;&nbsp;&nbsp;- name: Mount drives  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mount:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item.mount\_path }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item.drive }}{{ item.partition\_id }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fstype: ext4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_drives }"

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

&nbsp;&nbsp;&nbsp;&nbsp;d) You might want to create empty list my\_drives by default

&nbsp;&nbsp;&nbsp;&nbsp;defaults/main.yml  
&nbsp;&nbsp;&nbsp;&nbsp;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:  
&nbsp;&nbsp;- {drive: /dev/sdb ,mount\_path: /manager1, partition\_id: 1}  
&nbsp;&nbsp;- {drive: /dev/sdc ,mount\_path: /manager2, partition\_id: 1}

$ cat group\_vars/web\_server.yml  
my\_drives:  
&nbsp;&nbsp;- {drive: /dev/sdb ,mount\_path: /data1, partition\_id: 1}  
&nbsp;&nbsp;- {drive: /dev/sdc ,mount\_path: /data2, partition\_id: 1}  
&nbsp;&nbsp;- {drive: /dev/sdd ,mount\_path: /data3, partition\_id: 1}

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

- hosts: manager\_server  
&nbsp;&nbsp;become: true  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- manage\_drives

- hosts: web\_server  
&nbsp;&nbsp;become: true  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- manage\_drives

HTH,

&nbsp;&nbsp;-vlado
