Report this

What is the reason for this report?

Cannot attach volume to a new droplet using ansible

Posted on June 15, 2020

I’m trying to create and attach volumes and attach them to new droplets using Ansible.

    - name: what about the volumes?
      digital_ocean_block_storage:
        block_size: 8
        command: create
        region: 'nyc1'
        state: present
        volume_name: "{{ item }}"
      loop: "{{ droplet_tags }}"

    - name: create two droplets..
      digital_ocean_droplet:
        unique_name: yes
        region: 'nyc1'
        image: 'ubuntu-18-04-x64'
        wait_timeout: 100
        name: "{{ item }}"
        volumes: "{{ item }}"
        size: 's-1vcpu-1gb'
        state: present
      loop: "{{ droplet_tags }}"
      register: created_droplets

When I execute a playbook containing these two tasks above, I get the following error. (snippet from -vvv execution result)

failed: [localhost] (item=p6-droplet-1) => {
    "ansible_loop_var": "item", 
    "changed": false, 
    "invocation": {
        "module_args": {
            "backups": false, 
            "id": null, 
            "image": "ubuntu-18-04-x64", 
            "ipv6": false, 
            "monitoring": false, 
            "name": "p6-droplet-1", 
            "private_networking": false, 
            "region": "nyc1", 
            "size": "s-1vcpu-1gb", 
            "ssh_keys": null, 
            "tags": null, 
            "user_data": null, 
            "volumes": [
                "p6-droplet-1"
            ]
        }
    }, 
    "item": "p6-droplet-1", 
    "msg": "One or more of the provided volumes is not available in the selected region."
}
failed: [localhost] (item=p6-droplet-2) => {
    "ansible_loop_var": "item", 
    "changed": false, 
    "invocation": {
        "module_args": {
            "backups": false, 
            "id": null, 
            "image": "ubuntu-18-04-x64", 
            "ipv6": false, 
            "monitoring": false, 
            "name": "p6-droplet-2", 
            "private_networking": false, 
            "region": "nyc1", 
            "size": "s-1vcpu-1gb", 
            "ssh_keys": null, 
            "tags": null, 
            "user_data": null, 
            "volumes": [
                "p6-droplet-2"
            ]
        }
    }, 
    "item": "p6-droplet-2", 
    "msg": "One or more of the provided volumes is not available in the selected region."
}

The variable loops are working correctly, and the two volumes are already available in nyc1 region.

TASK [what about the volumes?] ********************************************************************************************************************************
ok: [localhost] => (item=p6-droplet-1) => {"ansible_loop_var": "item", "changed": false, "item": "p6-droplet-1"}
ok: [localhost] => (item=p6-droplet-2) => {"ansible_loop_var": "item", "changed": false, "item": "p6-droplet-2"}

TASK [create two droplets..] **********************************************************************************************************************************
failed: [localhost] (item=p6-droplet-1) => {"ansible_loop_var": "item", "changed": false, "item": "p6-droplet-1", "msg": "One or more of the provided volumes is not available in the selected region."}
failed: [localhost] (item=p6-droplet-2) => {"ansible_loop_var": "item", "changed": false, "item": "p6-droplet-2", "msg": "One or more of the provided volumes is not available in the selected region."}

Why am I not able to attach volumes that are in the same region? Do I have to go through extra steps? If so, what are them?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

The issue might be that the creation and attachment of the volumes are asynchronous operations that are still in progress when you try to create the droplets. This would make the volumes not yet available for attachment, resulting in the error message you’re seeing.

One way to handle this in your Ansible playbook is to introduce a delay or a wait_for task after the volume creation, giving the volumes enough time to be fully created and become available for attachment. The wait_for module can pause the playbook execution for a set amount of time, or until a certain condition is met.

Here’s an example of how to introduce a delay:

    - name: what about the volumes?
      digital_ocean_block_storage:
        block_size: 8
        command: create
        region: 'nyc1'
        state: present
        volume_name: "{{ item }}"
      loop: "{{ droplet_tags }}"
      
    - name: Wait for volume to be available
      wait_for:
        timeout: 60
        sleep: 5
      loop: "{{ droplet_tags }}"

    - name: create two droplets..
      digital_ocean_droplet:
        unique_name: yes
        region: 'nyc1'
        image: 'ubuntu-18-04-x64'
        wait_timeout: 100
        name: "{{ item }}"
        volumes: "{{ item }}"
        size: 's-1vcpu-1gb'
        state: present
      loop: "{{ droplet_tags }}"
      register: created_droplets

This introduces a delay of up to 60 seconds after each volume creation, waiting 5 seconds between each check. You can adjust the timeout and sleep values accordingly.

Best,

Bobby

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

It seems like the volume is not available for the droplet to attach to or its identifiers might not be correct. Keep in mind that a volume’s name is different from its ID, and it’s the ID that you should use to refer to a volume in your playbook.

Ensure that you are using the correct ID of the volume when attaching them to the droplets and that they are in an available state before the droplets are created.

Check in the DigitalOcean dashboard or by running doctl compute volume list to ensure the existence and state of the volume.

If the issue continues, please share your complete playbook and any defined variables or parameter files you might be using. Exclude any sensitive information such as token ids, passwords, etc.

For more information on using Ansible with DigitalOcean, check out this link - Ansible.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.