<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: baffledmystic</title>
    <description>The latest articles on DEV Community by baffledmystic (@baffledmystic).</description>
    <link>https://dev.to/baffledmystic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F797832%2Fb9f2ed48-96e8-484b-a79f-4bb7940c9116.png</url>
      <title>DEV Community: baffledmystic</title>
      <link>https://dev.to/baffledmystic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/baffledmystic"/>
    <language>en</language>
    <item>
      <title>Detangling Ansible, loop and json_query
</title>
      <dc:creator>baffledmystic</dc:creator>
      <pubDate>Thu, 20 Jan 2022 22:11:35 +0000</pubDate>
      <link>https://dev.to/baffledmystic/detangling-ansible-loop-and-jsonquery-2fpd</link>
      <guid>https://dev.to/baffledmystic/detangling-ansible-loop-and-jsonquery-2fpd</guid>
      <description>&lt;p&gt;Ansible comes pretty handy when aiming to automate your infrastructure.&lt;br&gt;
However, in my experience over 2+ years, combining Ansible with loops can easily reach brain-wracking complexity.&lt;/p&gt;

&lt;p&gt;Following from my playbook demonstrates how &lt;a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#selecting-json-data-json-queries"&gt;json_query&lt;/a&gt; is essential to navigate a JSON jungle.&lt;br&gt;
Of course, there can be other approaches but, json_query simplifies it best in my opinion.&lt;/p&gt;

&lt;p&gt;The task at hand:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables"&gt;register&lt;/a&gt; the output of a &lt;a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html"&gt;loop&lt;/a&gt;ed module to a variable.&lt;/li&gt;
&lt;li&gt;check the 'results' in the output for 'failed' or 'changed' booleans to decide actions in the next module.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sounds simple right? Not quite ..&lt;/p&gt;

&lt;p&gt;Trial #1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  - name: PUT files in bucket
    amazon.aws.aws_s3:
      bucket: "{{ arch_bucket }}"
      object: "{{ item.path|basename }}"
      src: "{{ item.path }}"
      mode: put
      aws_ca_bundle: "/path/to/bundle.pem"
      aws_access_key: "{{ bucket_access_key }}"
      aws_secret_key: "{{ bucket_secret_key }}"
      s3_url: "{{ cloud_url }}"
      validate_certs: yes
    loop: "{{ files_to_archive }}"
    register: archive_files_op

  - name: Delete files successfully archived
    file:
      path: "{{ item.path }}"
      state: absent
    loop: "{{ archive_files_op | json_query(del_query) }}"
    vars:
        del_query: "results[?changed == 'True' &amp;amp;&amp;amp; failed == 'False'].item"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For some reason, the json_query doesn't like using standard comparison operators for the booleans and an error is reported:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fatal: [target.node.com]: FAILED! =&amp;gt; {"msg": "template error while templating string: expected token ',', got 'True'. String: {{ archive_files_op | json_query('results[?changed=='True' &amp;amp;&amp;amp; failed=='False'].item.path') }}"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After hours of trying, this is what worked to my satisfaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  - name: PUT files in bucket
    amazon.aws.aws_s3:
    ..&amp;lt;snip-snip&amp;gt;..
    loop: "{{ files_to_archive }}"
    register: archive_files_op

  - name: Delete files successfully archived
    file:
      path: "{{ item.path }}"
      state: absent
    loop: "{{ archive_files_op | json_query(del_query) }}"
    vars:
      del_query: "results[?!failed &amp;amp;&amp;amp; changed].item"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Happy automating!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ansible</category>
      <category>jsonquery</category>
      <category>jmespath</category>
      <category>automation</category>
    </item>
  </channel>
</rss>
