Creating Multi-Part Ansible Playbook With Variables – NetApp and VMware

Please follow & like us :)

If you followed my last post where I created an Ansible Playbook to complete a full setup of a NetApp simulator, you would have seen that it was one gigantic playbook.

Working with one gigantic playbook can be a little overwhelming, a little bit messy and as a result, makes it hard to reuse certain tasks in other playbooks.

I have been working this week on creating a multipart Ansible playbook, whereby I have one main playbook, that calls upon multiple smaller playbooks. On top of this, I have also created a separate variables file that gets referenced within the YAML files.

As a bonus, there is a new part of this automation whereby the NFS datastores we created get automatically mounted to each ESXi host within my VMware vSphere environment.

Multi-Part Ansible Playbook Files

Below is a list of files that I have created along with a description of what each one of them does. Later on, we’ll take a look inside and list the changes that have occurred since my previous post:

  • netapp_full_install_multi-part.yml (the main playbook that calls each of the other playbooks, 01 – 06)
  • variables.yml (contains all the variables that are called upon within each file below)
  • 01_install_licenses_setup_ntp.yml (NetApp Simulator: install NetApp licenses, set NTP and Timezone)
  • 02_create_aggregate.yml (NetApp Simulator: rename root aggregate, create and online data aggregate)
  • 03_create_svm.yml (NetApp Simulator: create SVM, start NFS, create NFS export rule, add DNS, create first NFS volume
  • 04_network_setup.yml (Netapp Simulator: setup VLAN, create broadcast domain, create subnet, create NFS lif)
  • 05_create_volume.yml (NetApp Simulator: create additional NFS volume)
  • 06_mount_nfs_datastore.yml (VMware vSphere: Mount the 2 NetApp NFS volumes to ESXi)

Ansible Playbook Imports

Let’s take a look into the main file ‘netapp_full_install_multi-part.yml’. This file is used to call upon all playbooks labelled from 01 – 06

The way that it calls each playbook is via a command called ‘import_playbook’. You can see from the code below that this approach is much neater and structured than having all your commands in 1 big playbook. It also allows you to re-use some of these playbooks such as 05_create_volume.yml for any subsequent volume creation, simply by changing the relevant components in the variables file.

Ansible Playbook Variables File

If you remember in my previous post, we had a huge amount of variables at the beginning of the file, if you don’t remember it looked like this:

I have now created a variables.yml file, that each sub-playbook refers too. This approach is much neater, more user friendly, and makes it easy to re-use parts of the code.

This is what the new variables.yml file looks like:

Ansible Playbooks 01 – 06

I have taken all the tasks out of my previous gigantic playbook and created 6 smaller playbooks. Within each of these 6 playbooks you will notice that nearly all values are variables, as opposed to hardcoded values. These variables are all controlled via the variables.yml file. The benefits of creating a variables.yml file is that there is no need to edit any of the playbooks labelled 01-  – 06 and that it makes the playbooks more portable.

Below is the code for playbook ’01_install_licenses_setup_ntp.yml’. Nearly all the settings have a variable set as the value. To reference the variables.yml file, we need to add the following code to our playbooks:

I have added a debug msg at the end of each playbook for your reference. This debug message appears once the playbook has completed its run:

Ansible Playbook Code 01_install_licenses_setup_ntp.yml

Ansible Playbook 06_mount_nfs_datastore.yml

This playbook is a new addition to my build. It will take the 2 NFS volumes that we created on our NetApp simulator and mount them into my vSphere 6.7 environment.

Within the ‘netapp_full_install_multi-part.yml’ file you can see that I call this playbook 4 times. Each run sets the ‘esxihost’ variable to a different VMware ESXi host.

Back to the ’06_mount_nfs_datastore.yml’ playbook. The loop below mounts the 2 NFS volumes to the VMware ESXi host set within the ‘esxihost’ variable

I tried to figure out a way to put all the ESXi hosts into a variable as well as the number of datastores that I wish to mount and attempt to do 2 loops. One loop being the ESXi hosts and the second being the volumes. However, I’m yet to get that part working. When I do figure it out, I will update this post.

Ansible Playbooks on GitHub

I have copied all these Playbooks up to my GitHub repository. If want to copy this code, change some variables and try it out, just make sure all the files are in the same directory on your Ansible host.

If you have any questions or suggestion please let me know by leaving a comment below.

Ansible Playbook Netapp VMware

Be the first to comment

Leave a Reply

Your email address will not be published.



This site uses Akismet to reduce spam. Learn how your comment data is processed.