Lately I’ve been working on automatically provisioning servers while trying to remain sane as well as getting simple automatic deployments running. I’ve been bit by it before, but it wasn’t until I was trying to set up loadbalancers and DNS in Rackspace, that I took some time to look into the problem: with_subelements doesn’t work properly. At least not the way I thought it should work.

With this object…

{
  "object": {
    "subobject": [
      {
        "sublist": [  ]
      },
      {
        "sublist": [  ]
      },
      
    ]
  }
}

with_subelements can access the list in object.subobject as well as the list in sublist with the following:

- 
  with_subelements:
    - object.subobject
    - sublist

But if sublist is placed deeper in the object, like the virtual IPs are, when creating multiple Rackspace load balancers:

"lb.results": [
    {
        "balancer": {
            …,
            "virtual_ips": [
                {
                    …,
                    "ip_version": "IPV4",
                    "type": "PUBLIC"
                },
                {
                    …,
                    "ip_version": "IPV6",
                    "type": "PUBLIC"
                }
            ]
        },
        
    },
    
]

… and you try to access it with

- 
  with_subelements:
    - lb.results
    - balancer.virtual_ips

… Ansible just complains that the key balancer.virtual_ips wasn’t found or didn’t contain a list.

So I fixed that, making with_subelements properly descend the levels of an object. You can place the updated version of subelements.py in your Ansible project in the filter_plugins folder until my pull request hopefully is accepted.