{"id":38,"date":"2018-09-29T18:03:25","date_gmt":"2018-09-29T16:03:25","guid":{"rendered":"http:\/\/deleforterie.com\/wordpress\/?p=38"},"modified":"2023-01-18T08:59:59","modified_gmt":"2023-01-18T07:59:59","slug":"38","status":"publish","type":"post","link":"https:\/\/deleforterie.com\/wordpress\/index.php\/2018\/09\/29\/38\/","title":{"rendered":"Ansible &#8211; How to use a variable in a variable name"},"content":{"rendered":"<p>Some times you have to construct the name of a variable to use it.<\/p>\n<p>In previous versions of ansible there was a syntax like this one&nbsp;<code class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vars[ ansible_hostname + 'string' ]<\/code><\/p>\n<p>Now, since the Ansible 2.5 you have another syntax very useful<\/p>\n<p><!--more--><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\">- name: Show value of 'variablename'\n  debug: msg=\"{{ lookup('vars', 'variabl' + myvar)}}\"\n  vars:\n    variablename: hello\n    myvar: ename\n\n- name: Show default empty since i dont have 'variablnotename'\n  debug: msg=\"{{ lookup('vars', 'variabl' + myvar, default='')}}\"\n  vars:\n    variablename: hello\n    myvar: notename\n\n- name: Produce an error since i dont have 'variablnotename'\n  debug: msg=\"{{ lookup('vars', 'variabl' + myvar)}}\"\n  ignore_errors: True\n  vars:\n    variablename: hello\n    myvar: notename\n\n- name: find several related variables\n  debug: msg=\"{{ lookup('vars', 'ansible_play_hosts', 'ansible_play_batch', 'ansible_play_hosts_all') }}\"\n\n- name: alternate way to find some 'prefixed vars' in loop\n  debug: msg=\"{{ lookup('vars', 'ansible_play_' + item) }}\"\n  loop:\n    - hosts\n    - batch\n    - hosts_all<\/pre>\n<p>Find the documentaiton <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/plugins\/lookup\/vars.html\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some times you have to construct the name of a variable to use it. In previous versions of ansible there was a syntax like this one&nbsp;vars[ ansible_hostname + &#8216;string&#8217; ] Now, since the Ansible 2.5 you have another syntax very useful<\/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],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible"],"_links":{"self":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/38","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=38"}],"version-history":[{"count":6,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":162,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/38\/revisions\/162"}],"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=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}