Cannot aggregate hosts after using ec2 module and with_items

I’m trying to figure out how to create multiple ec2 instances using with_items so that I can control the id etc. The example under works, but I can’t figure out how to add the new server with add_host.

  • name: provision instance
    local_action:
    module: ec2
    region: eu-west-1
    id: $item
    keypair: ${keypair}
    image: ${image}
    type: ${instance_type}
    group_id: ${security-group}
    vpc_subnet_id: ${subnet-id}
    register: instres
    with_items:
    [web-1, web-2, web-3]

I’m trying something like this, but it doesn’t work.

  • name: Register in inventory
    local_action: add_host name={{ $item.instances[0].private_ip }} groupname=${group-identifier}
    with_items:
    ${instres.results}

Could someone please help me with the add_host part?

Best regards

Erik