Question

How to have unique_name with Ansible and DO?

Trying to get my Ansible playbook to keep unique_names, but I can’t get this feature to work. Followed the tutorial on Ansible with DO 2.0 Api, but every time I run my playbook, totally new droplets are created with the SAME names!

Here’s my playbook, which otherwise works fine.

---
- hosts: digitalocean

  vars:
    do_token: xxxx
    droplets:
    - droplet-one
    - droplet-two

  tasks:
  - name: ensure key exists at DigitalOcean
    digital_ocean: >
      state=present
      command=ssh
      name=demo
      ssh_pub_key={{ lookup('file', '~/.ssh/id_rsa.pub') }}
      api_token={{ do_token }}
    register: demo


  - name: ensure droplets exist
    digital_ocean:
      state: present
      command: droplet
      name: "{{item}}"
      unique_name: yes
      size_id: 512mb
      region_id: sfo1
      image_id: 15764687
      private_networking: "yes"
      ssh_key_ids: "{{ demo.ssh_key.id }}"
      api_token: "{{ do_token }}"
    with_items: "{{droplets}}"
    register: droplet_details

  - debug: msg="IP is {{ item.droplet.ip_address }}"
    with_items: droplet_details.results
Show comments

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I had the same issue. Downgrading ansible to version 2.0 worked for me:

sudo pip install 'ansible>=2.0,<2.1'

If you then run your playbook you will see the following deprecation warning which points to the actual issue with latest ansible versions:

[DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the
 full variable syntax ('{{droplet_details.results}}').
This feature will be removed in a future release. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

Hope that helps somebody :)

Hi,

One thing I noticed that would cause this is that your unique_name: yes statement should instead be unique_name=yes to match the tutorial:

  • name: ensure droplet one exists digital_ocean: > state=present command=droplet name=droplet-one unique_name=yes size_id=512mb Additionally, it’s likely the dual name declarations will cause an issue - It looks like you will need to use either - name: ensure key exists at DigitalOcean or - name: ensure droplets exist for this to work as expected.