Ansible – Modify and join a list
So you have a list with a number of item which are the /data* path where you want to put your hadoop hdfs data and you want to add after each item the end of the path mount point, here is a solution.
- hosts: localhost vars: hadoop_dir_hdfs_data: "/hadoop/hdfs/data" hadoop_dir: - "/data1" - "/data2" - "/data3" - "/data4" - "/data5" tasks: - set_fact: my_result: "{{ hadoop_dir | map('regex_replace', '(.*)', '\\1' + hadoop_dir_hdfs_data) | join(',') }}" - debug: var: my_result
This will give you this output
TASK [debug] **************************************************************************************************************************** ok: [localhost] => { "my_result": "/data1/hadoop/hdfs/data,/data2/hadoop/hdfs/data,/data3/hadoop/hdfs/data,/data4/hadoop/hdfs/data,/data5/hadoop/hdfs/data" }
Explanation :
- I apply a map filter on the hadoop_dir list : map(‘regex_replace’
- This map filter use regex_replace on the list searching anything and capture this anything : (.*)
- I concatenate the first matching member (there is only one member here) with the hadoop_dir_hdfs_data : ‘\\1’ + hadoop_dir_hdfs_data
- I join all the items from my list with a ‘,’ : join(‘,’)
Please follow and like us: