# Using Host File Group Entries for NetApp Clusters

**URL:** <https://forum.ansible.com/t/using-host-file-group-entries-for-netapp-clusters/6113>\
**Category:** Get Help\
**Tags:** playbook, awx\
**Created:** [May 31, 2024, 9:55pm UTC](https://forum.ansible.com/t/using-host-file-group-entries-for-netapp-clusters/6113 "2024-05-31T21:55:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marshit](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@marshit](https://forum.ansible.com/u/marshit)\
**Post date:** [May 31, 2024, 9:55pm UTC](https://forum.ansible.com/t/using-host-file-group-entries-for-netapp-clusters/6113/1 "2024-05-31T21:55:41Z")

</div>

Hello All,

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

```auto
[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.

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

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 1, 2024, 8:44pm UTC](https://forum.ansible.com/t/using-host-file-group-entries-for-netapp-clusters/6113/2 "2024-06-01T20:44:47Z")

</div>

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:

```yaml
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.

```yaml
- 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.

---

<div class="post-metadata">

**Author:** ![marshit](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@marshit](https://forum.ansible.com/u/marshit)\
**Post date:** [June 2, 2024, 11:40pm UTC](https://forum.ansible.com/t/using-host-file-group-entries-for-netapp-clusters/6113/3 "2024-06-02T23:40:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marshit](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@marshit](https://forum.ansible.com/u/marshit)\
**Post date:** [June 3, 2024, 1:18am UTC](https://forum.ansible.com/t/using-host-file-group-entries-for-netapp-clusters/6113/4 "2024-06-03T01:18:12Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 3, 2024, 1:18am UTC](https://forum.ansible.com/t/using-host-file-group-entries-for-netapp-clusters/6113/5 "2024-07-03T01:18:24Z")

</div>

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