# Parsing and creating new variables from inventory hostgroup

**URL:** <https://forum.ansible.com/t/parsing-and-creating-new-variables-from-inventory-hostgroup/28854>\
**Category:** Ansible Project\
**Created:** [October 23, 2018, 3:00pm UTC](https://forum.ansible.com/t/parsing-and-creating-new-variables-from-inventory-hostgroup/28854 "2018-10-23T15:00:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brent\_Weaver](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brent\_Weaver](https://forum.ansible.com/u/Brent_Weaver)\
**Post date:** [October 23, 2018, 3:00pm UTC](https://forum.ansible.com/t/parsing-and-creating-new-variables-from-inventory-hostgroup/28854/1 "2018-10-23T15:00:25Z")

</div>

I have a hostgroup that have 4 hosts in it:

Example:

[ {  
“shcdeployer”,  
“shcnode1”,  
“shcnode3”,  
“shcnode3”  
} ]

I need to get two things actually

1. A shcnode\* node picked out of the list, does not matter
2. A string list of the nodes like: [https://shcnode1:8089](https://shcnode1:8089),[https://shcnode2:8089](https://shcnode2:8089),https//shcnode3:8089

There has to to be some easy trick that I am missing. I am also looking into also creating a python script to parse it as well.

Thoughts?

---

<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:** [October 23, 2018, 3:17pm UTC](https://forum.ansible.com/t/parsing-and-creating-new-variables-from-inventory-hostgroup/28854/2 "2018-10-23T15:17:33Z")

</div>

for the first, the random\_choice filter seems to be what you want:

```auto
anynode: "{{ groups['shc_hosts']|random_choice }}"

```

for the 2nd something like:

```auto
urllist: "{{ groups['shc_hosts']|map('regex_replace', '^(.*)$',
'https://'')|list|join(':8089,')}}:8089"

```

---

<div class="post-metadata">

**Author:** ![Brent\_Weaver](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brent\_Weaver](https://forum.ansible.com/u/Brent_Weaver)\
**Post date:** [October 24, 2018, 5:20pm UTC](https://forum.ansible.com/t/parsing-and-creating-new-variables-from-inventory-hostgroup/28854/3 "2018-10-24T17:20:25Z")

</div>

This does not seem to work:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 24, 2018, 6:30pm UTC](https://forum.ansible.com/t/parsing-and-creating-new-variables-from-inventory-hostgroup/28854/4 "2018-10-24T18:30:21Z")

</div>

The filter is called random, random\_choice is a lookup plugin.
