Introduction in Pure Storage Ansible modules

Introduction in Pure Storage Ansible modules

About this lab

vTeam Specialization Program

Recently I was nominated to join the Pure Storage vTeam Specialization Program for New Stack. The idea behind the program is to provide a community within Pure for Puritans to learn and develop skills and grow into a subject matter expert.

The program consists of training and lab exercises that are focussed on developing experience in the New Stack space (Kubernetes, Ansible, Open Stack and more). And since I think there are more people out there how want to learn more about New Stack, I though I’d blog my progress of the lab exercises.

Lab instructions

The purpose of this lab is to get started on Ansible and run some ad-hoc command.

Name:Use Pure modules to get facts from FA and FB
Description:Communicate with Pure devices using ansible
Objective:Be able to interrogate FA and FB using Ansible
Task #1:Obtain API tokens from FA and FB
Task #2:Create an Ansible playbook to get all FA information using upstream modules
Task #3:Create an Ansible playbook to get all FA information using upstream modules
Task #4:Execute playbooks and identify the FA and FB array names
Task #5:
Success Criteria:Successfully identify the name of the FA and FB used in the lab
Lab goals and tasks

Following my earlier blog on Ansible ad-hoc, we’re now introducing the Pure Storage Ansible modules for both FlashArray and FlashBlade.

Installing the Pure Storage Ansible collections

While in the past the Pure Storage modules were “in-tree” with Ansible, or in other words shipped together with Ansible, in the future all third party modules will be removed from Ansible and move to Ansible Collections. So if we’re starting with Ansible now, best is to start with collections immediately. Although moving from the in-tree modules to using collections is really simple, since all you need to do is install the collections and mention the collection in your playbook as we’ll see later on in this blog.

First we’ll be installing the Ansible collections to our host using the following:

ansible-galaxy collection install purestorage.flasharray
ansible-galaxy collection install purestorage.flashblade

Now the ansible collections are installed. However there are some requirements to be able to use the modules, that need to be satisfied first before we can get started.

You can find the prerequisites for the FlashArray collections here. And the requirements for FlashBlade can be found here.

To install the requirements execute the following commands.

pip install purestorage
pip install purity_fb
pip install netaddr pytz

The command on line 1 will install the FlashArray python SDK, the second line will install the FlashBlade python SDK.

The command on line 3 installs some required modules. While the prerequisites only mentions the netaddr module, I know (since I test the Ansible collections for Simon from Pure) that some modules also need the pytz module. While this is mentioned in the modules, installing this module right now will keep you out of trouble.

So with the collections and prerequisites installed, we are ready to go!

Our first playbook

So when we start writing our first playbook, it’s a good time to first read about playbooks in the Ansible website.

In this session, I will go through the sections of a sample playbook and explain the individual sections. In the end I’ll show the complete playbook so that we can run it and complete this lab.

Playbook information

The first part of the playbook, shown below, defines the name of the playbook, it specifies the hosts to run the playbook against and finally if we want to gatherfacts for this host, which we do not.

- name: My first Pure playbook
  hosts: localhost
  gather_facts: false

The name kind of makes sense I guess, but the other two might not. The hosts parameters is specified here as localhost. Normally ansible is run against some hosts, where remote commands are executed, as we have seen in my previous blog. Normally a inventory file is used to target one or more remote hosts. However to run commands against FlashArray or FlashBlade, the actual modules are executed from the host the playbook is executed from.

The gather_facts param is used to inventory the host we execute the playbook against. This could for example be used to retrieve the hostname of the remote hosts targeted by our inventory. For this playbook I do not need facts from our host, so I set it to false.

Using the Pure Storage collections

Next up is pointing to our Pure Storage collections for FlashArray and FlashBlade:

  collections:
    - purestorage.flasharray
    - purestorage.flashblade

By specifying the collections, the Ansible collections which we’ve installed earlier in this blog are now referenced and loaded for this playbook. Using these lines we can now access all modules that are available in the collections.

Our first Pure Storage task

Now, we can specify our tasks, the first will be a simple task using the purefb_info module, which returns information from a FlashBlade system.

  tasks:
    - name: Get FlashBlade information
      purefb_info:
        gather_subset:
          - minimum
        fb_url: "192.168.10.18"
        api_token: "T-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
      register: blade_info

The tasks: line specifies that the next line in the playbook are tasks to be executed for this playbook.

The first task starts at the name: Get FlashBlade information line, which names the task. This name will also be shown when executing the playbook, so chose a good name that makes sense to the users.

The line directly after the name, in this case purefb_info:, specifies the module to use. In this case re are referencing the FlashBlade info module.

Next we need to specify the parameters for the module to run correctly, where in this case the specify the gather_subnet, fb_url and api_token. The first (gather_subset) determines which info to return. The other two are required to authenticate to the Pure Storage FlashBlade system.

Finally we use the register: blade_info to register the results from the module in a variable called blade_info. By registering the results, we can reference the results of the module later in our playbook.

Dump the results

The playbook contains a second tasks, which basically dumps the results that had been registered as blade_info:

    - name: show default information
      debug:
        msg: "{{ blade_info }}"

The first line set’s the name for the task. The second line sets the module for the task called debug, which is a Ansible module to display (debug) messages. In the third line the msg: param is used to display the information contained in the variable blade_info. To make Ansible understand blade_info is a variable we need to reference it using double curly brackets as {{ blade_info }}. This will make Ansible dump the contents of the variable blade_info as a result of this task.

The complete playbook

That completes the first playbook, for reference, please see the full script below:

- name: My first Pure playbook
  hosts: localhost
  gather_facts: false
  collections:
    - purestorage.flasharray
    - purestorage.flashblade
  tasks:
    - name: Get FlashBlade information
      purefb_info:
        gather_subset:
          - minimum
        fb_url: "192.168.10.18"
        api_token: "T-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
      register: blade_info
    - name: show default information
      debug:
        msg: "{{ blade_info }}"

Conclusion

That completes this blog, introducing Ansible playbooks and using the Pure Storage Ansible modules. The Ansible modules for Pure are very complete and powerful, virtually everything that is available to configure on the array is available via de Ansible modules. This means that it allows you automate everything you want, adding your Pure Storage arrays to your infrastructure-as-code environment.

I hope this was helpful, thanks for reading and see you in the next blog!

One thought on “Introduction in Pure Storage Ansible modules

Leave a Reply

Your email address will not be published.