Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for vultr api v2 #14587

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.cfg
Expand Up @@ -215,7 +215,7 @@ cloud_providers:
disk: 10
vultr:
os: Ubuntu 20.04 LTS x64
size: 1024 MB RAM,25 GB SSD,1.00 TB BW
size: vc2-1c-1gb
linode:
type: g6-nanode-1
image: linode/ubuntu20.04
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,3 +1,3 @@
ansible==6.1.0
ansible==7.3.0
jinja2~=3.0.3
netaddr
57 changes: 36 additions & 21 deletions roles/cloud-vultr/tasks/main.yml
Expand Up @@ -4,50 +4,65 @@

- block:
- name: Creating a firewall group
vultr_firewall_group:
vultr.cloud.firewall_group:
api_key: "{{ vultr_api_key }}"
name: "{{ algo_server_name }}"

- name: Creating firewall rules
vultr_firewall_rule:
vultr.cloud.firewall_rule:
api_key: "{{ vultr_api_key }}"
group: "{{ algo_server_name }}"
protocol: "{{ item.protocol }}"
port: "{{ item.port }}"
ip_version: "{{ item.ip }}"
cidr: "{{ item.cidr }}"
with_items:
- { protocol: tcp, port: "{{ ssh_port }}", ip: v4, cidr: 0.0.0.0/0 }
- { protocol: tcp, port: "{{ ssh_port }}", ip: v6, cidr: "::/0" }
- { protocol: udp, port: 500, ip: v4, cidr: 0.0.0.0/0 }
- { protocol: udp, port: 500, ip: v6, cidr: "::/0" }
- { protocol: udp, port: 4500, ip: v4, cidr: 0.0.0.0/0 }
- { protocol: udp, port: 4500, ip: v6, cidr: "::/0" }
- { protocol: udp, port: "{{ wireguard_port }}", ip: v4, cidr: 0.0.0.0/0 }
- { protocol: udp, port: "{{ wireguard_port }}", ip: v6, cidr: "::/0" }
ip_type: "{{ item.ip }}"
subnet: "{{ item.subnet }}"
subnet_size: "0"
loop:
- { protocol: tcp, port: "{{ ssh_port }}", ip: v4, subnet: 0.0.0.0/0 }
- { protocol: tcp, port: "{{ ssh_port }}", ip: v6, subnet: "::/0" }
- { protocol: udp, port: 500, ip: v4, subnet: 0.0.0.0/0 }
- { protocol: udp, port: 500, ip: v6, subnet: "::/0" }
- { protocol: udp, port: 4500, ip: v4, subnet: 0.0.0.0/0 }
- { protocol: udp, port: 4500, ip: v6, subnet: "::/0" }
- {
protocol: udp,
port: "{{ wireguard_port }}",
ip: v4,
subnet: 0.0.0.0/0
}
- {
protocol: udp,
port: "{{ wireguard_port }}",
ip: v6,
subnet: "::/0"
}

- name: Upload the startup script
vultr_startup_script:
vultr.cloud.startup_script:
api_key: "{{ vultr_api_key }}"
name: algo-startup
script: |
{{ lookup('template', 'files/cloud-init/base.yml') }}

- name: Creating a server
vultr_server:
name: "{{ algo_server_name }}"
vultr.cloud.instance:
api_key: "{{ vultr_api_key }}"
label: "{{ algo_server_name }}"
startup_script: algo-startup
hostname: "{{ algo_server_name }}"
os: "{{ cloud_providers.vultr.os }}"
plan: "{{ cloud_providers.vultr.size }}"
region: "{{ algo_vultr_region }}"
firewall_group: "{{ algo_server_name }}"
state: started
tag: Environment:Algo
ipv6_enabled: true
auto_backup_enabled: false
notify_activate: false
tags: ["Environment:Algo"]
enable_ipv6: true
backups: false
activation_email: false
register: vultr_server

- set_fact:
cloud_instance_ip: "{{ vultr_server.vultr_server.v4_main_ip }}"
cloud_instance_ip: "{{ vultr_server.vultr_instance.main_ip }}"
ansible_ssh_user: algo
ansible_ssh_port: "{{ ssh_port }}"
cloudinit: true
Expand Down
19 changes: 10 additions & 9 deletions roles/cloud-vultr/tasks/prompts.yml
Expand Up @@ -12,17 +12,21 @@
set_fact:
algo_vultr_config: "{{ vultr_config | default(_vultr_config.user_input) | default(lookup('env','VULTR_API_CONFIG'), true) }}"

- name: Set the Vultr API Key as a fact
set_fact:
vultr_api_key: "{{ lookup('ansible.builtin.ini', 'key', section='default', file=algo_vultr_config) }}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I reached the "Enter the local path to your configuration INI file" prompt, I left it blank in hopes it would use ~/.vultr.ini by default, but I got an error:

"msg": "An unhandled exception occurred while running the lookup plugin 'ansible.builtin.ini'. Error was a <class 'ansible.errors.AnsibleParserError'>, original message: an error occurred while trying to read the file '/Users/max/dev/algo/roles/cloud-vultr/': [Errno 21] Is a directory:

I'm guessing it didn't like the empty input?

However, after entering ~/.vultr.ini directly, everything worked for me


- name: Get regions
uri:
url: https://api.vultr.com/v1/regions/list
url: https://api.vultr.com/v2/regions
method: GET
status_code: 200
register: _vultr_regions

- name: Format regions
set_fact:
regions: >-
[ {% for k, v in _vultr_regions.json.items() %}
[ {% for v in _vultr_regions.json['regions'] %}
{{ v }}{% if not loop.last %},{% endif %}
{% endfor %} ]

Expand All @@ -32,17 +36,14 @@

- name: Set default region
set_fact:
default_region: >-
{% for r in vultr_regions %}
{%- if r['DCID'] == "1" %}{{ loop.index }}{% endif %}
{%- endfor %}
default_region: 1

- pause:
prompt: |
What region should the server be located in?
(https://www.vultr.com/locations/):
{% for r in vultr_regions %}
{{ loop.index }}. {{ r['name'] }}
{{ loop.index }}. {{ r['city'] }}
{% endfor %}

Enter the number of your desired region
Expand All @@ -54,5 +55,5 @@
set_fact:
algo_vultr_region: >-
{% if region is defined %}{{ region }}
{%- elif _algo_region.user_input %}{{ vultr_regions[_algo_region.user_input | int -1 ]['name'] }}
{%- else %}{{ vultr_regions[default_region | int - 1]['name'] }}{% endif %}
{%- elif _algo_region.user_input %}{{ vultr_regions[_algo_region.user_input | int -1 ]['id'] }}
{%- else %}{{ vultr_regions[default_region | int - 1]['id'] }}{% endif %}