Using ec2_vol with dynamic inventory

Hi,
I’m trying to create a playbook that will attach a volume to the specified instances and install casssandra as a cluster.

I have almost everything worked out but the ec2_vol part.

tasks file for cassandra

  • name: gather ec2 facts
    action: ec2_facts
    register: ec2_facts

  • debug: var=ec2_facts

  • name: Add volume for cassandra nodes
    local_action:
    module: ec2_vol
    instance: “{{ ansible_ec2_instance_id }}”
    volume_size: 50
    volume_type: gp2
    region: us-east-1
    device_name: /dev/xvdb

With that in mind I can see the ec2_facts of the tagged instances that I’m going to use in the cluster but when executed it doesn’t recognized the ansible_ec2_instance_id variable that I can actually see when running the ec2_facts.

Any idea why?

Thank you.

Hi Max,

Are you running this playbook with the ec2 instance as the remote target?

You are missing the reference to the registered fact, I think:

  • name: gather ec2 facts
    action: ec2_facts
    register: ec2_facts

  • debug: var=ec2_facts

  • name: Add volume for cassandra nodes
    local_action:
    module: ec2_vol
    instance: “{{ **ec2_facts.**ansible_ec2_instance_id }}”
    volume_size: 50
    volume_type: gp2
    region: us-east-1
    device_name: /dev/xvdb

I haven’t used the ec2_facts module yet myself, however, since I usually provision and register facts from the ec2 module, so I might be wrong.

Joanna

Hi Joanna,

Thanks for your post, I just tried your suggestion but still isn’t working. But you pointed me in the right direction. I was running ec2_facts as a local action which was incorrect and using a tripple debug flag I noticed that aws_access_key and aws_secret_key were null, seems kind of dump having to set them up as variables for ec2_vol to work, but after doing that but it worked, bellow how I end up doing it