Digital_ocean_droplet - can not target a specific project

I am playing with digital_ocean_droplet and can manage to create one, but fail doing this into any but the default project.

There is a default project named devops_bootcamp, but I want, for testing purposes, create the droplet into the initial first-project (which still exists, but is not the default any longer)

---
- hosts: localhost
  connection: local
  become: false
  vars_files:
    - ./do_api_token.yml
  tasks:
    - name: show variable values
      ansible.builtin.debug:
        msg: |
          "drop_name: {{ drop_name }}
           project_name: {{ project }}"

    - name: Create a new Droplet
      community.digitalocean.digital_ocean_droplet:
        state: "{{ state }}"
        oauth_token: "{{ DO_API_TOKEN }}"
        name: "{{ drop_name }}"
        size: s-1vcpu-512mb-10gb
        region: fra1
        image: "{{ distro_image | default('debian-12-x64') }}"
        wait_timeout: 500
        project_name: "{{ project }}"
        ssh_keys: [ 'a5:a3:b9:50:e6:c1:a8:0f:b9:70:6a:d9:56:78:91:d4' ]
      register: drop_var
      when: state == 'present'

    - name: Show Droplet info
      ansible.builtin.debug:
        var: drop_var      

… which I run like

ansible-playbook do_droplet.yml \
    -e '{ "state":"present" , "drop_name":"drop-40" , "project":"first-project" }' \
    --ask-vault-pass

so, the droplet gets created, but inside the default project "devops-bootcamp", not "first-project", as intended.

any idea what is wrong here?


I am getting confirmation the variable value is passed into the run correctly by that show variable values TASK

2023-12-01 13:51:57,618 p=116936 u=dulhaver n=ansible | ok: [127.0.0.1] => 
  msg: |-
    "drop_name: drop-40
     project_name: first-project"

Hi,

I don’t do much python, but looking at create() function in module definition, specifically the project assignment part, I fail to see why it shouldn’t work.
Here is the assign_to_project() function definition if you’d like to delve deeper.

Could you run you playbook in verbose mode (-vvv should be plenty), see if module returns an error somewhere on project assignment ?
Function should return an error if: project is not set, doesn’t have an id, can’t be assigned for whatever reason, or there is an issue with resource (droplet data), though I’m not sure a) it isn’t catched, and b) if it breaks.

Also, interestingly enough, there are similar issues on community.digitalocean repo: #255, #201 which states using project keyword (which is just an alias) in you task definition instead of project_name works. Perplexing. I’m thinking it has to do with how parameters are defined in module specs, and the alias is used in create() function instead of the param name, but as said earlier, I’m not a python nor Ansible module expert, so I’ll let someone else chime on that.

I know that’s not what you asked, but as a workaround, you could probably update your target project to set it as default (or from DO API) (assuming droplet module always follows default project), create your droplet and let it be assigned to that project, then rollback your projects statuses. Not very elegant, but should do the trick for the meantime. Or just directly assign your droplet to project using this DO API endpoint and ansible.builtin.uri module.

1 Like