{"id":42,"date":"2018-09-29T18:21:57","date_gmt":"2018-09-29T16:21:57","guid":{"rendered":"http:\/\/deleforterie.com\/wordpress\/?p=42"},"modified":"2023-01-18T08:53:13","modified_gmt":"2023-01-18T07:53:13","slug":"ansible-using-key-value-of-a-dict","status":"publish","type":"post","link":"https:\/\/deleforterie.com\/wordpress\/index.php\/2018\/09\/29\/ansible-using-key-value-of-a-dict\/","title":{"rendered":"Ansible &#8211; Using key value of a dict"},"content":{"rendered":"<p>When you do some things that needs complex data structure, it could be great to acces to the key and value of the complex data structure.<\/p>\n<p>You can do it with the lookup magic word.<\/p>\n<p><!--more--><\/p>\n<p>Here is that complex data structure<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\">pkiaas_csr:\n  host1:\n    subject_alt_name:\n    - host1.domainA.com\n    - host1.domainB.com\n  host2:\n    subject_alt_name:\n    - host2.domainA.com\n    - host2.domainB.com\n<\/pre>\n<p>So you want to loop on that structure for creating a certificate signing request with this playbook<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\">- name: Creating the CSR\n  include: create_csr.yml\n  loop: \"{{ lookup('dict', pkiaas_csr, wantlist=True) }}\"\n<\/pre>\n<p>The <code class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">wantlist=True<\/code> is mandatory for looping when you have only one element in your data structure.<\/p>\n<p>The use of this in your yaml<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\">- name: Create a csr\n  command: \"\/usr\/bin\/openssl req -config csr_config.conf -new -keyout my_private_key_{{ item.key }}.key -out my_certificate_{{ item.key }}.csr<\/pre>\n<p>and in your template file used for creating the csr_config.conf<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\">subjectAltName = {% for alias in item.value.subject_alt_name %}DNS:{{ alias }}{% if not loop last %},{% endif %}{% endfor %}\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you do some things that needs complex data structure, it could be great to acces to the key and value of the complex data structure. You can do it with the lookup magic word.<\/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-42","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\/42","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=42"}],"version-history":[{"count":3,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/42\/revisions\/91"}],"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=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}