title: Merge 2 hashes recursively with jinja2/ansible
body: <p>I'm trying to merge 2 hashes in ansible, but I'm running into problems. The hash is mostly merged, but the lists inside are not.</p>
  <p>I'm using the combine filter for the merge:</p>
  <pre><code>config_hash: &quot;{{ inventory_config | combine(host_config, recursive=True, list_merge='append_rp') }}&quot;
  </code></pre>
  <p>Here are the two hashes/dicts I'd like to merge:</p>
  <pre><code>inventory_config:
    exoscale:
      inventory_name: test_inventory
      security_groups: 
        - name: rancher
          state: present
          rules:
            - port: 22
              cidr: &quot;1.2.3.4/32&quot;
              type: egress
            - port: 80
              cidr: &quot;0.0.0.0/32&quot;
              type: egress
  
  host_config:
    exoscale:
      name: host
      disk_size: 25 GiB
      security_groups: 
        - name: rancher
          state: added
          rules:
            - port: 21
              cidr: &quot;1.2.3.4/32&quot;
              type: ingress
            - port: 8080
              cidr: &quot;0.0.0.0/32&quot;
              type: ingress
  </code></pre>
  <p>The result:</p>
  <pre><code>TASK [debug] *******************************************************************
  ok: [instance] =&gt; {
      &quot;config_hash&quot;: {
          &quot;exoscale&quot;: {
              &quot;disk_size&quot;: &quot;25 GiB&quot;,
              &quot;inventory_name&quot;: &quot;test_inventory&quot;,
              &quot;name&quot;: &quot;host&quot;,
              &quot;security_groups&quot;: [
                  {
                      &quot;name&quot;: &quot;rancher&quot;,
                      &quot;rules&quot;: [
                          {
                              &quot;cidr&quot;: &quot;1.2.3.4/32&quot;,
                              &quot;port&quot;: 22,
                              &quot;type&quot;: &quot;egress&quot;
                          },
                          {
                              &quot;cidr&quot;: &quot;0.0.0.0/32&quot;,
                              &quot;port&quot;: 80,
                              &quot;type&quot;: &quot;egress&quot;
                          }
                      ],
                      &quot;state&quot;: &quot;present&quot;
                  },
                  {
                      &quot;name&quot;: &quot;rancher&quot;,
                      &quot;rules&quot;: [
                          {
                              &quot;cidr&quot;: &quot;1.2.3.4/32&quot;,
                              &quot;port&quot;: 21,
                              &quot;type&quot;: &quot;ingress&quot;
                          },
                          {
                              &quot;cidr&quot;: &quot;0.0.0.0/32&quot;,
                              &quot;port&quot;: 8080,
                              &quot;type&quot;: &quot;ingress&quot;
                          }
                      ],
                      &quot;state&quot;: &quot;added&quot;
                  }
              ]
          }
      }
  }
  </code></pre>
  <p>The result I wanted:</p>
  <pre><code>&quot;config_hash&quot;: {
      &quot;exoscale&quot;: {
          &quot;disk_size&quot;: &quot;25 GiB&quot;,
          &quot;inventory_name&quot;: &quot;test_inventory&quot;,
          &quot;name&quot;: &quot;host&quot;,
          &quot;security_groups&quot;: [
              {
                  &quot;name&quot;: &quot;rancher&quot;,
                  &quot;rules&quot;: [
                      {
                          &quot;cidr&quot;: &quot;1.2.3.4/32&quot;,
                          &quot;port&quot;: 22,
                          &quot;type&quot;: &quot;egress&quot;
                      },
                      {
                          &quot;cidr&quot;: &quot;0.0.0.0/32&quot;,
                          &quot;port&quot;: 80,
                          &quot;type&quot;: &quot;egress&quot;
                      },
                      {
                          &quot;cidr&quot;: &quot;1.2.3.4/32&quot;,
                          &quot;port&quot;: 21,
                          &quot;type&quot;: &quot;ingress&quot;
                      },
                      {
                          &quot;cidr&quot;: &quot;0.0.0.0/32&quot;,
                          &quot;port&quot;: 8080,
                          &quot;type&quot;: &quot;ingress&quot;
                      }
                  ],
                  &quot;state&quot;: &quot;added&quot;
              },
          ]
      }
  }
  </code></pre>
  <p>I tried some other options, and it seems that &quot;list_merge='append_rp'&quot; can combine lists of simple items but not hashes.</p>
  <p>Any ideas? Thanks for the help!</p>