{"id":57,"date":"2018-10-04T20:56:42","date_gmt":"2018-10-04T18:56:42","guid":{"rendered":"http:\/\/deleforterie.com\/wordpress\/?p=57"},"modified":"2023-01-18T08:53:13","modified_gmt":"2023-01-18T07:53:13","slug":"ansible-modify-and-join-a-list","status":"publish","type":"post","link":"https:\/\/deleforterie.com\/wordpress\/index.php\/2018\/10\/04\/ansible-modify-and-join-a-list\/","title":{"rendered":"Ansible &#8211; Modify and join a list"},"content":{"rendered":"<p>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.<\/p>\n<p><!--more--><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\">- hosts: localhost\n  vars:\n    hadoop_dir_hdfs_data: \"\/hadoop\/hdfs\/data\"\n    hadoop_dir:\n      - \"\/data1\"\n      - \"\/data2\"\n      - \"\/data3\"\n      - \"\/data4\"\n      - \"\/data5\"\n  tasks:\n  - set_fact:\n      my_result: \"{{ hadoop_dir | map('regex_replace', '(.*)', '\\\\1' + hadoop_dir_hdfs_data) | join(',') }}\"\n  - debug:\n      var: my_result\n<\/pre>\n<p>This will give you this output<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\">TASK [debug] ****************************************************************************************************************************\nok: [localhost] =&gt; {\n    \"my_result\": \"\/data1\/hadoop\/hdfs\/data,\/data2\/hadoop\/hdfs\/data,\/data3\/hadoop\/hdfs\/data,\/data4\/hadoop\/hdfs\/data,\/data5\/hadoop\/hdfs\/data\"\n}\n<\/pre>\n<p>Explanation :<\/p>\n<ul>\n<li>I apply a map filter on the hadoop_dir list : map(&#8216;regex_replace&#8217;<\/li>\n<li>This map filter use regex_replace on the list searching anything and capture this anything : (.*)<\/li>\n<li>I concatenate the first matching member (there is only one member here) with the hadoop_dir_hdfs_data :&nbsp;&#8216;\\\\1&#8217; + hadoop_dir_hdfs_data<\/li>\n<li>I join all the items from my list with a &#8216;,&#8217; :&nbsp;join(&#8216;,&#8217;)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":2,"featured_media":155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[6,14],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","category-hadoop"],"_links":{"self":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":4,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/57\/revisions\/161"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/media\/155"}],"wp:attachment":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}