Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
Improved with vbox_autostart_user variable
Browse files Browse the repository at this point in the history
  • Loading branch information
dmotte committed Jun 28, 2023
1 parent 49d2325 commit 4f0b1c0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ This role is inspired by the following article: [AutoStart VirtualBox VMs on Sys
1. Install this role using the `ansible-galaxy` CLI tool
2. You can then include it into the `tasks` section of your _Ansible Playbook_. See [`test/playbook.yml`](test/playbook.yml) for an example of how to do that. Remember to replace the role name with `dmotte.vbox_autostart`.

> **Note**: this role must be run as root (`ansible_become: true`).
> **Note**: if you want to automate also the _VirtualBox_ installation on the host, you can use the [oefenweb.virtualbox](https://galaxy.ansible.com/oefenweb/virtualbox) role.
### Role variables
Expand Down
5 changes: 4 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
# User to which the VirtualBox VMs belong
vbox_autostart_user: mainuser

# List of VirtualBox VMs that must be started at system boot
vbox_vms: []
vbox_autostart_vms: []
15 changes: 6 additions & 9 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
- name: Ensure the VBOXAUTOSTART_DB variable is set correctly
become: true
ansible.builtin.lineinfile:
path: /etc/default/virtualbox
regexp: "^VBOXAUTOSTART_DB="
Expand All @@ -9,7 +8,6 @@
mode: "0644"

- name: Ensure the VBOXAUTOSTART_CONFIG variable is set correctly
become: true
ansible.builtin.lineinfile:
path: /etc/default/virtualbox
regexp: "^VBOXAUTOSTART_CONFIG="
Expand All @@ -18,27 +16,24 @@
mode: "0644"

- name: Ensure the autostartvm.cfg file exists and has the right content
become: true
ansible.builtin.copy:
content: |
default_policy = deny
{{ ansible_user_id }} = {
{{ vbox_autostart_user }} = {
allow = true
startup_delay = 10
}
dest: /etc/vbox/autostartvm.cfg
mode: "0644"

- name: Add the user to the vboxusers group
# Equivalent to "usermod -aG vboxusers {{ ansible_user_id }}"
become: true
# Equivalent to "usermod -aG vboxusers {{ vbox_autostart_user }}"
ansible.builtin.user:
name: "{{ ansible_user_id }}"
name: "{{ vbox_autostart_user }}"
groups: [vboxusers] # It's important to use the "groups" parameter instead of "group"
append: true

- name: Ensure the owner group of the /etc/vbox dir is vboxusers and has write permissions
become: true
ansible.builtin.file:
path: /etc/vbox
group: vboxusers
Expand All @@ -48,14 +43,16 @@
ansible.builtin.meta: reset_connection

- name: Set the VirtualBox autostartdbpath property
become_user: "{{ vbox_autostart_user }}"
ansible.builtin.command:
cmd: VBoxManage setproperty autostartdbpath /etc/vbox/
register: result
changed_when: result.rc == 0

- name: Enable autostart on the virtual machines
become_user: "{{ vbox_autostart_user }}"
ansible.builtin.command:
cmd: VBoxManage modifyvm {{ item }} --autostart-enabled on
register: result
changed_when: result.rc == 0
loop: "{{ vbox_vms }}"
loop: "{{ vbox_autostart_vms }}"
4 changes: 3 additions & 1 deletion test/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
- name: Include the role from the parent directory
ansible.builtin.include_role: { name: "{{ playbook_dir | dirname }}" }
vars:
vbox_vms: [DebVM01]
ansible_become: true
vbox_autostart_user: "{{ ansible_user_id }}"
vbox_autostart_vms: [VMDeb01]

0 comments on commit 4f0b1c0

Please sign in to comment.