Parsing and creating new variables from inventory hostgroup

18 views
Skip to first unread message

Brent Weaver

unread,
Oct 23, 2018, 11:00:25 AM10/23/18
to Ansible Project
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://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?

Brian Coca

unread,
Oct 23, 2018, 11:18:14 AM10/23/18
to ansible...@googlegroups.com
for the first, the random_choice filter seems to be what you want:
```
anynode: "{{ groups['shc_hosts']|random_choice }}"
```
for the 2nd something like:
```
urllist: "{{ groups['shc_hosts']|map('regex_replace', '^(.*)$',
'https://'')|list|join(':8089,')}}:8089"
```

--
----------
Brian Coca

Brent Weaver

unread,
Oct 24, 2018, 1:20:25 PM10/24/18
to Ansible Project
This does not seem to work:

---
- hosts:
    - "*westus_azure_ops*"
  vars:
    X: "{{ groups['splunk_cluster_label_westus_azure_ops'] | random_choice }}"
  tasks:
    - debug: var=X

no filter named 'random_choice' is likely the error tripping us up

Kai Stian Olstad

unread,
Oct 24, 2018, 2:30:33 PM10/24/18
to ansible...@googlegroups.com
On Wednesday, 24 October 2018 19:20:25 CEST Brent Weaver wrote:
> This does not seem to work:
>
> ---
> - hosts:
> - "*westus_azure_ops*"
> vars:
> X: "{{ groups['splunk_cluster_label_westus_azure_ops'] | random_choice
> }}"
> tasks:
> - debug: var=X
>
> no filter named 'random_choice' is likely the error tripping us up

The filter is called random, random_choice is a lookup plugin.

--
Kai Stian Olstad


Reply all
Reply to author
Forward
0 new messages