title: Using jq to append output generated from Ansible template
body: <p>I am writing Ansible play, one of the tasks is to append entry to a JSON document. E.g.</p>
  
  <p>JSON document <code>staff.json</code>:</p>
  
  <pre><code>{
     "staff":[
        {
           "john":[
              {
                 "position":"techwriter"
              },
              {
                 "sex":"male"
              }
           ]
        }
     ]
  }
  </code></pre>
  
  <p>I need to append this entry to <code>staff</code>:</p>
  
  <pre><code>{
     "staff":[
        {
           "john":[
              {
                 "position":"techwriter"
              },
              {
                 "sex":"male"
              }
           ]
        },
        {
           "jane":[
              {
                 "position":"admin"
              },
              {
                 "sex":"female"
              }
           ]
        }
     ]
  }
  </code></pre>
  
  <p>The entry would be generated from Ansible <a href="http://docs.ansible.com/ansible/template_module.html" rel="nofollow noreferrer"><code>template</code></a>, something like this:</p>
  
  <pre><code>      {
           "{{ staff_name }}":[
              {
                 "position":"{{ staff_position }}"
              },
              {
                 "sex":"{{ staff_sex }}"
              }
           ]
        }
  </code></pre>
  
  <p>I've learned to use <code>jq</code> to <a href="https://stedolan.github.io/jq/manual/#Assignment" rel="nofollow noreferrer">append entry</a> to the JSON document, as seen in "<a href="https://stackoverflow.com/questions/25414854/add-json-array-element-with-jq-cmdline">Add json array element with jq (cmdline)</a>". However, I do not know how I can implement this in Ansible, as template would output to a file.</p>
  
  <p>I need a solution of something like this:</p>
  
  <pre><code>cat staff.json | jq '.staff |= .+ ["OUTPUT_FROM_TEMPLATE"]'
  </code></pre>
  
  <p>Any ideas welcome.</p>