Using Host File Group Entries for NetApp Clusters

Hello All,

I’m curious. With my “hosts” file, I have the following entry:

[single]
192.168.10.10  shortname=NAS1-CLUSTER

If I have a playbook I was to run an action against, it will only work if I use the IP address of 192.168.10.10.

  vars:
    netapp_host: '192.168.10.10'
    netapp_username: "{{ netapp_username }}"
    netapp_password: "{{ netapp_password }}"

If I change the netapp_host field to either ‘single’ or ‘[single]’ I receive an error:

fatal: [localhost]: FAILED! => {“changed”: false, “msg”: “Error using REST for version, error: Failed to parse: https://[single]/api/cluster. Error using REST for version, status_code: None.”}

How do you get the playbook to use the group name instead of only IP? If I have larger groups (I do) I don’t want to be using single IPs.

If it matters at all, this playbook is being run from AWX. The file format is also INI. I feel like I need to stop using IN and go with a .yml file instead

The error you got indicates that you set netapp_host to the literal string “[single]”, which clearly isn’t what you want.

You could try this:

vars:
  netapp_host: '{{ groups["single"] | random }}'

Each {{ groups["X"] }} is a list of all the hosts in group “X”. The “| random” will pick one.

Be careful, though. Because of lazy Jinja expression evaluation, it may resolve differently each place you use '{{ netapp_host }}". If you want to pick a group member at random, but use that same group member throughout the play, then set netapp_host with set_fact. Values set through set_fact get their templates fully resolved at set_fact time in contrast to the var definition above that gets re-evaluated on each use.

- name: Play the netapp_host lottery
  ansible.builtin.set_fact:
    netapp_host: '{{ groups["single"] | random }}'

If you do that (↑) first, each host your play targets will be randomly matched to one of the members of the single host group.

However, set_fact has this interesting (and quite useful) feature. If you throw a “run_once: true” on that task, then (a) it only runs once rather than once per target host, and (b) all your target hosts get the same fact set for them. So in this case, yes you do get a netapp host randomly selected from the single host group, but all your target hosts get the same randomly selected netapp host.

So you can pick whichever level of “random” you want:

  • play level: all play hosts use the same randomly selected netapp (via set_fact with run_once: true);
  • host level: each play host gets a randomly selected netapp (via set_fact without run_once. Note: they could all be randomly matched to the same netapp, but not likely);
  • use level: each time your {{ netapp_host }} variable is evaluated, a netapp host is selected at random (via the var: example at the top of this post).

Your INI file is fine. Nothing you’ve shown us would be improved by switching formats.

@utoddl Thank you, Todd I will try this and get back to you probably tomorrow if it works.

@utoddl Thanks again, Todd, I tested and it worked flawlessly. I used this snippet here:

---
- name: Get NetApp cluster version and name
  hosts: localhost
  gather_facts: false
  collections:
    - netapp.ontap
  tasks:
    - name: Select a random host from the 'single' group
      ansible.builtin.set_fact:
        netapp_host: '{{ groups["single"] | random }}'
      run_once: true
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.