Reputation: 842
I have an Ansible playbook that sets up Splunk cluster, one of the commands adds a Server List of cluster members,
- name: search head init & set CAPTAIN
block:
- command: splunk init shcluster-config -auth admin:changeme \
-mgmt_uri "https://{{ ansible_default_ipv4.address }}:8089" \
-replication_port 8090 -conf_deploy_fetch_url https://{{ deployer_ip }}:8089 -secret {{ cluster['secret'] }}
# set captain
- debug: msg='setting captain'
- command: splunk bootstrap shcluster-captain \
-servers_list "10.185.20.156:8089, 10.185.20.160:8089, 10.185.20.161:8089" -auth admin:changeme
I have all my config vars set inside a file, and thats where Im getting them from (for example, cluster['secret']
), the config.yaml
varfile looks like this,
# Cluster Config
cluster:
secret: abracadabra # secret key
app_name: cluster # adds a custom Splunk App for Cluster config
members:
mrxsplunksh01: 10.185.20.156
mrxsplunksh02: 10.185.20.160
mrxsplunksh03: 10.185.20.161
I cant figure out how to parse/loop the cluster['members'] dictionary to get the IP of each cluster member, so I dont have to do this part manually,
- command: splunk bootstrap shcluster-captain \
-servers_list "10.185.20.156:8089, 10.185.20.160:8089, 10.185.20.161:8089" -auth admin:changeme
I tried with_items
and with_dict
and I can't get it to parse each element correctly.
Upvotes: 0
Views: 398
Reputation: 68459
You don't need any loops to achieve this.
Firstly, you can get a list of dictionary values with Python values()
method:
cluster.members.values()
Then you need to modify individual elements of the list like in this answer:
map('regex_replace', '(.*)', '\\1:8089')
Finally, you need to convert the list to a string, splitting the elements with ,
:
join(', ')
Combined:
- servers_list "{{ cluster.members.values() | map('regex_replace', '(.*)', '\\1:8089') | join(', ') }}" -auth admin:changeme
I’m answering from phone now, so I have not verified the exact outcome, or typos yet.
And you should quote the whole argument to the command
if it was written in one line. I’m not sure if YAML requires it with \
, or if splitting this way is correct at all.
Upvotes: 2