Skip to content

Commit

Permalink
instance: add user_scheme param (#97)
Browse files Browse the repository at this point in the history
* instance: add user_scheme param

* add changelog
  • Loading branch information
resmo authored Dec 5, 2023
1 parent 9f2dfc5 commit 55154e1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/instance-user-scheme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- instance - Added a new param ``user_scheme`` to change user scheme to non-root on Linux while creating the instance (https://github.com/vultr/ansible-collection-vultr/issues/96).
17 changes: 16 additions & 1 deletion plugins/modules/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@
description:
- User data to be passed to the instance.
type: str
user_scheme:
description:
- The user scheme used as login user (Linux-only).
- By default, the I(root) user is configured.
- Only considered while creating the instance.
type: str
choices: [ root, limited ]
version_added: "1.11.0"
startup_script:
description:
- Name or ID of the startup script to execute on boot.
Expand Down Expand Up @@ -362,6 +370,12 @@
returned: success
type: str
sample: I2Nsb3VkLWNvbmZpZwpwYWNrYWdlczoKICAtIGh0b3AK
user_scheme:
description: The user scheme to login into this instance
returned: success
type: str
sample: root
version_added: "1.11.0"
backups:
description: Whether backups are enabled or disabled.
returned: success
Expand Down Expand Up @@ -416,7 +430,6 @@


class AnsibleVultrInstance(AnsibleVultrCommonInstance):

def handle_power_status(self, resource, state, action, power_status, force=False, wait_for_state=True):
if state == self.module.params["state"] and (resource["power_status"] != power_status or force):
self.result["changed"] = True
Expand Down Expand Up @@ -491,6 +504,7 @@ def main():
user_data=dict(type="str"),
ssh_keys=dict(type="list", elements="str", no_log=False),
region=dict(type="str", required=True),
user_scheme=dict(type="str", choices=["root", "limited"]),
state=dict(
choices=[
"present",
Expand Down Expand Up @@ -538,6 +552,7 @@ def main():
"sshkey_id",
"backups",
"attach_vpc",
"user_scheme",
],
resource_update_param_keys=[
"plan",
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/targets/instance/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ vultr_instances:
- three
- four
region: ams
os: Debian 11 x64 (bullseye)
os: Debian 12 x64 (bookworm)
backups: true
backups_update: false
ddos_protection: true
ddos_protection_update: false
enable_ipv6: false
enable_ipv6_update: true
user_scheme: limited
vpcs:
- "{{ vultr_resource_prefix }}_instance_vpc_1"
- "{{ vultr_resource_prefix }}_instance_vpc_2"
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/targets/instance/tasks/present.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
- name: verify test create instance
ansible.builtin.assert:
Expand All @@ -59,6 +60,7 @@
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_2') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')

- name: test create instance idempotence
vultr.cloud.instance:
Expand All @@ -78,6 +80,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
- name: verify test create instance idempotence
ansible.builtin.assert:
Expand All @@ -91,6 +94,7 @@
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_2') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')

- name: test update instance in check mode
vultr.cloud.instance:
Expand All @@ -110,6 +114,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs_update | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
check_mode: true
- name: verify test update instance in check mode
Expand All @@ -124,6 +129,7 @@
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_2') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')

- name: test update instance
vultr.cloud.instance:
Expand All @@ -143,7 +149,11 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs_update | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
until: result is not failed
retries: 10
delay: 3
- name: verify test update instance
ansible.builtin.assert:
that:
Expand All @@ -156,6 +166,7 @@
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_3') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')

- name: test update instance idempotence
vultr.cloud.instance:
Expand All @@ -175,6 +186,7 @@
image: "{{ instance.image | default(omit) }}"
snapshot: "{{ instance.snapshot | default(omit) }}"
vpcs: "{{ instance.vpcs_update | default(omit) }}"
user_scheme: "{{ instance.user_scheme | default(omit) }}"
register: result
- name: verify test update instance idempotence
ansible.builtin.assert:
Expand All @@ -188,3 +200,4 @@
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_1') | list | count == 1
- result.vultr_instance.vpcs | selectattr('description','equalto',vultr_resource_prefix ~ '_instance_vpc_3') | list | count == 1
- result.vultr_instance.vpcs | list | count == 2
- result.vultr_instance.user_scheme == instance.user_scheme | default('root')

0 comments on commit 55154e1

Please sign in to comment.