{"id":67,"date":"2018-10-11T21:03:38","date_gmt":"2018-10-11T19:03:38","guid":{"rendered":"http:\/\/deleforterie.com\/wordpress\/?p=67"},"modified":"2023-01-18T08:53:12","modified_gmt":"2023-01-18T07:53:12","slug":"ansible-test-variables-types","status":"publish","type":"post","link":"https:\/\/deleforterie.com\/wordpress\/index.php\/2018\/10\/11\/ansible-test-variables-types\/","title":{"rendered":"Ansible &#8211; Test variables types"},"content":{"rendered":"<p>Here is a playbook for understanding the Ansible test with variables types<\/p>\n<p><!--more--><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\">---\n- hosts: localhost\n  gather_facts: no\n  vars:\n    string: \"string\"\n    list:\n      - item1\n      - item2\n    dict:\n      key1: value1\n      key2: value2\n    object:\n      k1: \"string\"\n      k2: [ \"item1\", \"item2\" ]\n      k3: { 'i1': 'v1', 'i2': 'v2' }\n  tasks:\n    - name: \"dict is mapping\"\n      debug: msg=\"dict is mapping\"\n      when: dict is mapping\n    - name: \"list is mapping\"\n      debug: msg=\"list is mapping\"\n      when: list is mapping\n    - name: \"string is mapping\"\n      debug: msg=\"string is mapping\"\n      when: string is mapping\n    - name: \"dict is sequence\"\n      debug: msg=\"dict is sequence\"\n      when: dict is sequence\n    - name: \"list is sequence\"\n      debug: msg=\"list is sequence\"\n      when: list is sequence\n    - name: \"string is sequence\"\n      debug: msg=\"string is sequence\"\n      when: string is sequence\n    - name: \"dict is iterable\"\n      debug: msg=\"dict is iterable\"\n      when: dict is iterable\n    - name: \"list is iterable\"\n      debug: msg=\"list is iterable\"\n      when: list is iterable\n    - name: \"string is iterable\"\n      debug: msg=\"string is iterable\"\n      when: string is iterable\n    - name: \"dict is string\"\n      debug: msg=\"dict is string\"\n      when: dict is string\n    - name: \"list is string\"\n      debug: msg=\"list is string\"\n      when: list is string\n    - name: \"string is string\"\n      debug: msg=\"string is string\"\n      when: string is string\n    - copy:\n        dest: .\/variable.txt\n        content: |\n          {% for k,v in object.iteritems() -%}\n            {% if v is string -%}\n            \"string: {{ v }}\"\n            {% elif v is mapping -%}\n              {% for j,l in v.iteritems() -%}\n                \"{{ j }}: {{ l }}\"\n              {% endfor -%}\n            {% elif v is sequence -%}\n              {% for n in v -%}\n                \"- {{ n }}\"\n              {% endfor -%}\n            {% else -%}\n              \"what?\"\n            {% endif -%}\n          {% endfor -%}\n<\/pre>\n<p>This will produce this output<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">PLAY [localhost] ***************************************************************************************************************************************************************************************\n\nTASK [dict is mapping] *********************************************************************************************************************************************************************************\nok: [localhost] =&gt; {\n    \"msg\": \"dict is mapping\"\n}\n\nTASK [list is mapping] *********************************************************************************************************************************************************************************\nskipping: [localhost]\n\nTASK [string is mapping] *******************************************************************************************************************************************************************************\nskipping: [localhost]\n\nTASK [dict is sequence] ********************************************************************************************************************************************************************************\nok: [localhost] =&gt; {\n    \"msg\": \"dict is sequence\"\n}\n\nTASK [list is sequence] ********************************************************************************************************************************************************************************\nok: [localhost] =&gt; {\n    \"msg\": \"list is sequence\"\n}\n\nTASK [string is sequence] ******************************************************************************************************************************************************************************\nok: [localhost] =&gt; {\n    \"msg\": \"string is sequence\"\n}\n\nTASK [dict is iterable] ********************************************************************************************************************************************************************************\nok: [localhost] =&gt; {\n    \"msg\": \"dict is iterable\"\n}\n\nTASK [list is iterable] ********************************************************************************************************************************************************************************\nok: [localhost] =&gt; {\n    \"msg\": \"list is iterable\"\n}\n\nTASK [string is iterable] ******************************************************************************************************************************************************************************\nok: [localhost] =&gt; {\n    \"msg\": \"string is iterable\"\n}\n\nTASK [dict is string] **********************************************************************************************************************************************************************************\nskipping: [localhost]\n\nTASK [list is string] **********************************************************************************************************************************************************************************\nskipping: [localhost]\n\nTASK [string is string] ********************************************************************************************************************************************************************************\nok: [localhost] =&gt; {\n    \"msg\": \"string is string\"\n}\n\nTASK [copy] ********************************************************************************************************************************************************************************************\nok: [localhost]\n\nPLAY RECAP *********************************************************************************************************************************************************************************************\nlocalhost                  : ok=9    changed=0    unreachable=0    failed=0   \n<\/pre>\n<p>And this file<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\"i1: v1\"\n\"i2: v2\"\n\"- item1\"\n\"- item2\"\n\"string: string\"\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a playbook for understanding the Ansible test with variables types<\/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-67","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\/67","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=67"}],"version-history":[{"count":2,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/67\/revisions\/89"}],"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=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}