{"id":38400,"date":"2025-05-23T12:32:09","date_gmt":"2025-05-23T10:32:09","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=38400"},"modified":"2025-05-23T12:32:12","modified_gmt":"2025-05-23T10:32:12","slug":"ansible-reduce-output-in-loops-or-replace-loops","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/","title":{"rendered":"Ansible: reduce output in loops &#8211; or replace loops"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-reduce-the-output\">Reduce the output<\/h2>\n\n\n\n<p>If you do a <a href=\"https:\/\/ansible-doc.readthedocs.io\/en\/latest\/rst\/playbooks_loops.html\">loop<\/a> in Ansible, it always prints the <code>item<\/code> variable for each loop. &#8220;item&#8221; is the current list element. If you are looping over a list with small values, then the output is OK.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n    - set_fact:\n        myList: &#x5B; &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot; ]\n\n   - name: simple loop\n      debug:\n        msg: &quot;value of item is {{ item }}.&quot;\n      with_items: &quot;{{myList}}&quot;\n      register: loop_result\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\nTASK &#x5B;simple loop] ***********************************************\nok: &#x5B;localhost] =&gt; (item=a) =&gt; {\n    &quot;msg&quot;: &quot;value of item is a.&quot;\n}\nok: &#x5B;localhost] =&gt; (item=b) =&gt; {\n    &quot;msg&quot;: &quot;value of item is b.&quot;\n}\nok: &#x5B;localhost] =&gt; (item=c) =&gt; {\n    &quot;msg&quot;: &quot;value of item is c.&quot;\n}\nok: &#x5B;localhost] =&gt; (item=d) =&gt; {\n    &quot;msg&quot;: &quot;value of item is d.&quot;\n}\n<\/pre><\/div>\n\n\n<p>But if the loop variable is a list of dict, especially with many or big values, then the log-output of Ansible gets very unreadable.<\/p>\n\n\n\n<p>Example: you have to deploy more than one trusted certificate to an <a href=\"https:\/\/www.dbi-services.com\/blog\/different-types-of-oracle-wallets\/\">Oracle wallet<\/a> (a <a href=\"https:\/\/en.wikipedia.org\/wiki\/PKCS_12\">PKCS#12 container file<\/a>). The size of a certificate is usually 1-4 KB. That means, if you deploy 4 certificates \u00e0 3KB, your screen is spammed by approx. 12&#8217;000 characters. And I think you are not interested in the certificate content. Isn&#8217;t it?<\/p>\n\n\n\n<p>Variable trusted_certs (certificate shortened for better readability)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n       trusted_certs:\n          - { issuer: &quot;letsencrypt&quot;,\n              certificate: &quot;-----BEGIN CERTIFICATE-----\\nMIIFBjCCAu6gAwIBAgI....MEZSa8DA\\n-----END CERTIFICATE-----&quot; }\n          - { issuer: &quot;verisign&quot;,\n              certificate: &quot;-----BEGIN CERTIFICATE-----\\nMIICkDCCAXgCAQIwHDq....kMJVW2X=\\n-----END CERTIFICATE-----&quot; }\n<\/pre><\/div>\n\n\n<p>With the following task we add the certificate to the Oracle wallet<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n    - name: Add trusted certificates to wallet\n      ansible.builtin.shell:  |\n        echo &quot;{{ item.certificate }}&quot; &gt; {{ item.issuer }}.cert.pem\n        orapki wallet add -wallet . -cert {{ item.issuer }}.cert.pem -trusted_cert -pwd {{ walletpw }}\n      with_items: &quot;{{trusted_certs}}&quot;\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"566\" height=\"617\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png\" alt=\"\" class=\"wp-image-38484\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png 566w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output-275x300.png 275w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/figure>\n\n\n\n<p>But how we can get rid of this annoying output? <\/p>\n\n\n\n<p>One possibility is to use the <code>no_log: true<\/code> option. Then, item will be replaced by &#8220;None&#8221; in the output.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n- name: Add trusted certificates to wallet\n  ansible.builtin.shell:  |\n    echo &quot;{{ item.certificate }}&quot; &amp;gt; {{ item.issuer }}.cert.pem\n    orapki wallet add -wallet . -cert {{ item.issuer }}.cert.pem -trusted_cert -pwd {{ walletpw }}\n  with_items: &quot;{{trusted_certs}}&quot;\n  no_log: true\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"63\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/nolog_cert_output.png\" alt=\"\" class=\"wp-image-38912\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/nolog_cert_output.png 559w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/nolog_cert_output-300x34.png 300w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/figure>\n\n\n\n<p>Nice \ud83d\ude42 But in case of errors, we have a problem to see what went wrong \ud83d\ude41<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"115\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/nolog_errors.png\" alt=\"\" class=\"wp-image-38913\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/nolog_errors.png 559w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/nolog_errors-300x62.png 300w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/figure>\n\n\n\n<p>Another approach is: Instead of looping over the certificate list, create a list with the index numbers of your certificate list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n    - name: Add trusted certificates to wallet\n      ansible.builtin.shell:  |\n        echo &quot;{{ trusted_certs&#x5B;item|int].certificate }}&quot; &gt; {{ trusted_certs&#x5B;item|int].issuer }}.cert.pem\n        orapki wallet add -wallet . -cert {{ trusted_certs&#x5B;item|int].issuer }}.cert.pem -trusted_cert -pwd {{ walletpw }}\n      with_sequence: start=0 end={{ trusted_certs|length-1 }}\n<\/pre><\/div>\n\n\n<p>We will create a sequence to identify the list elements using <code>with_sequence:<\/code> The 1st list element is [0] (<code>start=0<\/code>) and the last one is the number of elements -1 (`end={{ trusted_certs|length-1 }}`).<\/p>\n\n\n\n<p>For example, to access the 1st certificate (index number 0) we can use <code>{{ trusted_certs[0].certificate }}<\/code>. In the loop we replace the [0] by [index|int] to access all elements. Caution! Ansible is not able to recognize the sequence as numbers and treats it as string, so you have to explicitly convert it to int.<\/p>\n\n\n\n<p>And now, the output is very compact and readable \ud83d\ude42<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"48\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/short_cert_output-1.png\" alt=\"\" class=\"wp-image-38486\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/short_cert_output-1.png 570w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/short_cert_output-1-300x25.png 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-replace-loops\">Replace loops<\/h2>\n\n\n\n<p>Loops are easy to implement and to understand. It is nice for a few list elements, but if you have thousands of elements, then you get a big amount of output, and it is slow. <\/p>\n\n\n\n<p>If you want to see what certificates are added (only the issuer field), you can do it similar to the task to add certificates:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n    - name: Show trusted certificates to add\n      ansible.builtin.debug:  var=trusted_certs&#x5B;item|int].certificate\n      with_sequence: start=0 end={{ trusted_certs|length-1 }}\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"417\" height=\"168\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/debug_loop.png\" alt=\"\" class=\"wp-image-38629\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/debug_loop.png 417w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/debug_loop-300x121.png 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/figure>\n\n\n\n<p>Or you can use <code>json_query<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n    - name: Show trusted certificates to add (with json_query)\n      ansible.builtin.debug:  var=trusted_certs|json_query(&#039;&#x5B;].issuer&#039;)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"417\" height=\"117\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/debug_json.png\" alt=\"\" class=\"wp-image-38630\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/debug_json.png 417w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/debug_json-300x84.png 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/figure>\n\n\n\n<p>json_query: <code>[]<\/code> is the filter criteria. In this case, we will not filter the list elements. And <code>.issuer<\/code> means, to only return the issuer field and not the certificate field.<\/p>\n\n\n\n<p>If you run a playbook for many servers (e.g. <code>hosts: [dbservers]<\/code>, a group with all database hosts) and you need information for the hosts from an ldap directory (or a REST web-service), then it is often faster to get once the whole data and then to extract the information for the hosts with json_query, instead of querying each host individually in ldap.<\/p>\n\n\n\n<p>Example: If you have to know all databases on a server and you have stored all the connect strings (orclNetDescString) in an ldap directory, then you can get the information from there. For simplicity, we assume that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>orclNetDescString<\/code>: contains no spaces, all keywords are in uppercase and <code>HOST=...<\/code>contains the <code>{{inventory_hostname}}<\/code><\/li>\n\n\n\n<li><code>cn<\/code>: contains the database\/instance name<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n    - name: &quot;get databases on the server&quot;\n      community.general.ldap_search:\n        server_uri: &quot;{{ ldap_server }}&quot;\n        bind_dn: &quot;&quot;\n        dn: &quot;cn=OracleContext,dc=domain,dc=com&quot;\n        filter: &quot;(&amp;(orclNetDescString=*HOST*{{inventory_hostname}})(objectclass=orclNetService))&quot;\n        scope: &quot;children&quot;\n        attrs:\n          - cn\n          - orclNetDescString\n        register: ldap1\n    - debug: var=ldap1.results\n<\/pre><\/div>\n\n\n<p>It will return all databases for this host, in this example exactly one:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\nldap1.results: &#x5B;\n  {\n    &quot;cn&quot;: &quot;DB1&quot;\n    &quot;dn&quot;: &quot;cn=DB1,cn=oracleContext,dc=domain,dc=com&quot;,\n    &quot;orclNetDescString&quot;: &quot;(DESCRIPTION =(ADDRESS=(PROTOCOL=TCP)(HOST=srv01)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=DB01.domain.com)))&quot;\n  }\n]\n<\/pre><\/div>\n\n\n<p>You have to do that for the <code>[dbservers]<\/code> hosts. If you have 1000 database servers, you will run this task 1000 times.<\/p>\n\n\n\n<p>Instead of querying LDAP for each hostname, we can query it for all hosts, and then extract the required data from the result for the current host with json_query.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n    - name: &quot;get databases of all servers&quot;\n      run_once: true\n      community.general.ldap_search:\n        server_uri: &quot;{{ ldap_server }}&quot;\n        bind_dn: &quot;&quot;\n        dn: &quot;cn=OracleContext,dc=domain,dc=com&quot;\n        filter: &quot;(objectclass=orclNetService)&quot;\n        scope: &quot;children&quot;\n        attrs:\n          - cn\n          - orclNetDescString\n      register: ldap2\n<\/pre><\/div>\n\n\n<p>With <code>run_once<\/code> it will only be executed once on the 1st host, but the result is available for all hosts. All the hosts then can query this result to get the records of the own host:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n- name: show all databases on this host\n  debug: var=ldap2.results|json_query(jquery)\n  vars:\n    jquery: &quot;&#x5B;? contains(orclNetDescString, `HOST={{inventory_hostname}}`)].cn&quot;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-summary\">Summary<\/h2>\n\n\n\n<p>The output of a loop (loop, with_items, &#8230;) is annoying if the list item contains many or big values. To reduce the output you can:<\/p>\n\n\n\n<p>use the <code>no_log: true<\/code> option. Then the item-values will no longer be printed. But in case of an error, you will no longer get the error-message.<\/p>\n\n\n\n<p>Use the index of the list-elements. In the output you will see the index-number, and in case of errors, you will also see the error-message.<\/p>\n\n\n\n<p>For filtering data, it is more efficient to use json_query instead of looping over all list items.<\/p>\n\n\n\n<p>If you get data from an LDAP directory or a Webservice, try to fetch once all data and then to extract from this result the host-specific data with json_query.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reduce the output If you do a loop in Ansible, it always prints the item variable for each loop. &#8220;item&#8221; is the current list element. If you are looping over a list with small values, then the output is OK. But if the loop variable is a list of dict, especially with many or big [&hellip;]<\/p>\n","protected":false},"author":123,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1321],"tags":[2600,3619,3302],"type_dbi":[],"class_list":["post-38400","post","type-post","status-publish","format-standard","hentry","category-ansible","tag-ansible-2","tag-json_query","tag-loop"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Ansible: reduce output in loops - or replace loops - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ansible: reduce output in loops - or replace loops\" \/>\n<meta property=\"og:description\" content=\"Reduce the output If you do a loop in Ansible, it always prints the item variable for each loop. &#8220;item&#8221; is the current list element. If you are looping over a list with small values, then the output is OK. But if the loop variable is a list of dict, especially with many or big [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-23T10:32:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-23T10:32:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png\" \/>\n\t<meta property=\"og:image:width\" content=\"566\" \/>\n\t<meta property=\"og:image:height\" content=\"617\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Martin Bracher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Martin Bracher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/\"},\"author\":{\"name\":\"Martin Bracher\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e\"},\"headline\":\"Ansible: reduce output in loops &#8211; or replace loops\",\"datePublished\":\"2025-05-23T10:32:09+00:00\",\"dateModified\":\"2025-05-23T10:32:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/\"},\"wordCount\":731,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png\",\"keywords\":[\"Ansible\",\"json_query\",\"loop\"],\"articleSection\":[\"Ansible\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/\",\"name\":\"Ansible: reduce output in loops - or replace loops - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png\",\"datePublished\":\"2025-05-23T10:32:09+00:00\",\"dateModified\":\"2025-05-23T10:32:12+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#primaryimage\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png\",\"contentUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png\",\"width\":566,\"height\":617},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ansible: reduce output in loops &#8211; or replace loops\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e\",\"name\":\"Martin Bracher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g\",\"caption\":\"Martin Bracher\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/martinbracher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Ansible: reduce output in loops - or replace loops - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/","og_locale":"en_US","og_type":"article","og_title":"Ansible: reduce output in loops - or replace loops","og_description":"Reduce the output If you do a loop in Ansible, it always prints the item variable for each loop. &#8220;item&#8221; is the current list element. If you are looping over a list with small values, then the output is OK. But if the loop variable is a list of dict, especially with many or big [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/","og_site_name":"dbi Blog","article_published_time":"2025-05-23T10:32:09+00:00","article_modified_time":"2025-05-23T10:32:12+00:00","og_image":[{"width":566,"height":617,"url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png","type":"image\/png"}],"author":"Martin Bracher","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Martin Bracher","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/"},"author":{"name":"Martin Bracher","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e"},"headline":"Ansible: reduce output in loops &#8211; or replace loops","datePublished":"2025-05-23T10:32:09+00:00","dateModified":"2025-05-23T10:32:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/"},"wordCount":731,"commentCount":0,"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png","keywords":["Ansible","json_query","loop"],"articleSection":["Ansible"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/","url":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/","name":"Ansible: reduce output in loops - or replace loops - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#primaryimage"},"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png","datePublished":"2025-05-23T10:32:09+00:00","dateModified":"2025-05-23T10:32:12+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#primaryimage","url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png","contentUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2025\/05\/long_cert_output.png","width":566,"height":617},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/ansible-reduce-output-in-loops-or-replace-loops\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Ansible: reduce output in loops &#8211; or replace loops"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/86cb065eea74ac30961c4cc45ce56c9e","name":"Martin Bracher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/864a93d77bcd4cd44bab880a88f25fb5173ffbfac8e6e8775f0b4e056a4fbb56?s=96&d=mm&r=g","caption":"Martin Bracher"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/martinbracher\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/38400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/123"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=38400"}],"version-history":[{"count":32,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/38400\/revisions"}],"predecessor-version":[{"id":38919,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/38400\/revisions\/38919"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=38400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=38400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=38400"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=38400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}