There are sites that name all or most of their volumes with the same base name. (Share001, Share002, Share003 or Home001, Home002, Home003).  A common question I get asked all the time is , "How to use Ansible to auto create the next X number of volumes in the list?”  Well, there’s playbook for that.  For my example all of my volumes will follow the pattern (prefix)v(number).

- hosts: localhost
  gather_facts: false
  - netapp.ontap
    vols: []
    login: &login
      hostname: "{{ netapp_hostname }}"
      username: "{{ netapp_username }}"
      password: "{{ netapp_password }}"
      https: "{{ https }}"
      validate_certs: "{{ validate_certs }}"
  - name: "Gather volume info for {{ vol_base }}"
      gather_subset: volume_info
      query :
            name: "{{ vol_base }}*"
     <<: *login
    register: ontap
  - name: Create array of existing counts
      vols: "{{ vols }} + ['{{ (ontap.ontap_info.volume_info[item]'v' )[1]|int)}}']"
    loop: "{{ ontap.ontap_info.volume_info|list }}"
  - name: Determine highest created count
      start: "{{ vols|max }}"
  - name: Create volume(s)
      state: present
      name: "{{ vol_base }}v{{ '%04d'|format(item|int) }}"
      size: 10
      size_unit: gb
      space_guarantee: none
      aggregate_name: ansible_lab_01_aggr1
      vserver: ansible_vserver
      <<: *login
    loop: "{{ range(start|int + 1, start|int + count|int + 1, 1)|list }}"

Like with the Snapshot advanced topic , the first thing to notice is ‘vols: []’ in the vars section.  This is creating an empty list called vols that we will be adding information to in order to find out the number of the last created volume.

That list is made up of all the counts of the volumes with this line:

vols: "{{ vols }} + ['{{ (ontap.ontap_info.volume_info[item]'v')[1]|int)}}']"

The most important part of this line is ‘.split(‘v’)[1]|int)’. This says to split the volume name at the letter v and use the second part of the split to number each volume with an integer.  You don’t have to use v in your playbook, but make sure you use some unique character that you can use as your split point.  In my example, all my volumes are named sharev0001, sharev0002, etc.  Note, after the “v” all the 0001, 0002, parts of the volume name are converted to integers and added to my list.

Next I set a variable using the largest number from all these volumes.  This is better than just a count of how many volumes exist.  Maybe you have deleted 0232, 0234, 0265, but are up to 0567.  In that case 0568 should be the next volume created.

The last important bit is this line ‘"{{ vol_base }}v{{ '%04d'|format(item|int) }}" ’ where I name the volume.  This takes my volume base name ‘share’ adds the v and then adds the next number forcing a 4 character format to add leading zeros if needed.  If you want a 5 character code, change the %04d to %05d.

This loop creates volumes across a range starting  with the first available number and going up by however many volumes you have asked for with the variable ‘count’ .

Some important caveats to understant when using this method:

  1. The same Aggregate will be used for all created volumes per run
  2. The same vserver will be used for all created volumes per run.

This method will work better using more variables  and with doing an auto aggregate selection using the examples at

Play around with this and let me know how it goes over at our Slack Workspace thePub and the #configurationmgmt channel.  If you don’t have an invite to our Slack get one at

About David Blackwell

David is a twenty year IT veteran who has been an admin for just about every aspect of a DataCenter at one time or another. When not working, or tinkering with new software at home, David spends most of his free time with his six year old son and his lovely wife.

Pin It on Pinterest