fix: rename roles to use underscores instead of hyphens which was causing the roles not to be found
This commit is contained in:
15
roles/proxmox_lxc_provision/tasks/check-exists.yml
Normal file
15
roles/proxmox_lxc_provision/tasks/check-exists.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
---
|
||||
- name: Query Proxmox for existing LXCs
|
||||
community.proxmox.proxmox_lxc_info:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
register: proxmox_lxcs
|
||||
|
||||
- name: Check if LXC with hostname already exists
|
||||
ansible.builtin.set_fact:
|
||||
lxc_exists: >-
|
||||
{{
|
||||
proxmox_lxcs.proxmox_lxcs
|
||||
| selectattr('name', 'equalto', lxc_hostname)
|
||||
| list
|
||||
| length > 0
|
||||
}}
|
||||
29
roles/proxmox_lxc_provision/tasks/clone.yml
Executable file
29
roles/proxmox_lxc_provision/tasks/clone.yml
Executable file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
- name: Create a full clone of the container
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_vmid | default(0) }}"
|
||||
clone: "{{ lxc_clone_from }}"
|
||||
clone_type: "{{ lxc_clone_type }}"
|
||||
hostname: "{{ lxc_hostname }}"
|
||||
storage: "{{ lxc_storage }}"
|
||||
register: clone_result
|
||||
|
||||
- name: Add bind mounts via pct
|
||||
become: yes
|
||||
ansible.builtin.shell: |
|
||||
pct set {{ clone_result.vmid | default(lxc_vmid) }} {% for key, value in lxc_mounts.items() %}-{{ key }} {{ value }} {% endfor %}
|
||||
delegate_to: "{{ proxmox_api_host }}"
|
||||
when: lxc_mounts is defined
|
||||
|
||||
- name: Resize rootfs after clone
|
||||
ansible.builtin.command:
|
||||
cmd: "pct resize {{ clone_result.vmid }} rootfs {{ lxc_size }}G"
|
||||
delegate_to: "{{ proxmox_api_host }}"
|
||||
become: yes
|
||||
register: resize_result
|
||||
changed_when: resize_result.rc == 0 and 'already at specified size' not in resize_result.stderr
|
||||
failed_when:
|
||||
- resize_result.rc != 0
|
||||
- "'already at specified size' not in resize_result.stderr"
|
||||
when: lxc_size is defined
|
||||
8
roles/proxmox_lxc_provision/tasks/convert.yml
Executable file
8
roles/proxmox_lxc_provision/tasks/convert.yml
Executable file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
- ansible.builtin.include_tasks: stop.yaml
|
||||
- name: Convert container to template
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
|
||||
state: template
|
||||
hostname: "{{ lxc_hostname }}"
|
||||
25
roles/proxmox_lxc_provision/tasks/create.yml
Executable file
25
roles/proxmox_lxc_provision/tasks/create.yml
Executable file
@@ -0,0 +1,25 @@
|
||||
---
|
||||
- name: Create an LXC container
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_vmid | default(omit) }}"
|
||||
hostname: "{{ lxc_hostname }}"
|
||||
password: "{{ lxc_password }}"
|
||||
ostemplate: "{{ lxc_template }}"
|
||||
cores: "{{ lxc_cores }}"
|
||||
memory: "{{ lxc_memory }}"
|
||||
swap: "{{ lxc_swap }}"
|
||||
disk: "{{ lxc_disk }}"
|
||||
mounts: "{{ lxc_mounts | default(omit) }}"
|
||||
netif: >
|
||||
{"net0": "name=eth0,gw={{ lxc_gateway }},ip={{ lxc_ipv4 }},ip6={{ lxc_ipv6 | default(omit) }},bridge=vmbr0"}
|
||||
pubkey: "{{ lookup('file', lxc_pubkey_file) | default(omit) }}"
|
||||
onboot: "{{ lxc_onboot | default(false) }}"
|
||||
startup: "{{ lxc_startup | default(omit) }}"
|
||||
unprivileged: "{{ lxc_unprivileged | default(true) }}"
|
||||
features: "{{ lxc_features | default(omit) }}"
|
||||
timezone: "{{ lxc_timezone | default(omit) }}"
|
||||
nameserver: "{{ lxc_nameserver | default(omit) }}"
|
||||
state: present
|
||||
tags: "{{ lxc_tags | default(omit) }}"
|
||||
register: lxc_result
|
||||
12
roles/proxmox_lxc_provision/tasks/delete.yml
Executable file
12
roles/proxmox_lxc_provision/tasks/delete.yml
Executable file
@@ -0,0 +1,12 @@
|
||||
---
|
||||
- ansible.builtin.include_tasks: stop.yaml
|
||||
- name: Delete a container
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_vmid | default(omit) }}"
|
||||
hostname: "{{ lxc_hostname | default(omit) }}"
|
||||
state: absent
|
||||
register: delete_result
|
||||
failed_when: |
|
||||
delete_result.failed and
|
||||
('does not exist' not in delete_result.msg)
|
||||
43
roles/proxmox_lxc_provision/tasks/edit-config.yml
Executable file
43
roles/proxmox_lxc_provision/tasks/edit-config.yml
Executable file
@@ -0,0 +1,43 @@
|
||||
---
|
||||
|
||||
- name: Remove all existing ID mappings
|
||||
lineinfile:
|
||||
path: "/etc/pve/lxc/{{ lxc_vmid }}.conf"
|
||||
regexp: '^lxc\.idmap:'
|
||||
state: absent
|
||||
when: lxc_id_mappings is defined
|
||||
|
||||
- name: Add ID mappings
|
||||
blockinfile:
|
||||
path: "/etc/pve/lxc/{{ lxc_vmid }}.conf"
|
||||
block: "{{ lxc_id_mappings }}"
|
||||
insertafter: EOF
|
||||
when: lxc_id_mappings is defined
|
||||
|
||||
- name: Remove existing GPU configuration
|
||||
lineinfile:
|
||||
path: "/etc/pve/lxc/{{ lxc_vmid }}.conf"
|
||||
regexp: "{{ item }}"
|
||||
state: absent
|
||||
loop:
|
||||
- '^lxc\.cgroup2\.devices\.allow: c {{ gpu_device_id }}:\* rwm'
|
||||
- '^lxc\.cgroup2\.devices\.allow: c {{ uvm_device_id }}:\* rwm'
|
||||
- '^lxc\.mount\.entry: /dev/nvidia0'
|
||||
- '^lxc\.mount\.entry: /dev/nvidiactl'
|
||||
- '^lxc\.mount\.entry: /dev/nvidia-uvm '
|
||||
- '^lxc\.mount\.entry: /dev/nvidia-uvm-tools'
|
||||
when: lxc_nvidia_gpu_mount
|
||||
|
||||
- name: Add GPU device for passthrough
|
||||
blockinfile:
|
||||
path: /etc/pve/lxc/{{ lxc_vmid }}.conf
|
||||
block: |
|
||||
lxc.cgroup2.devices.allow: c {{ gpu_device_id }}:* rwm
|
||||
lxc.cgroup2.devices.allow: c {{ uvm_device_id }}:* rwm
|
||||
lxc.mount.entry: /dev/nvidia0 dev/nvidia0 none bind,optional,create=file
|
||||
lxc.mount.entry: /dev/nvidiactl dev/nvidiactl none bind,optional,create=file
|
||||
lxc.mount.entry: /dev/nvidia-uvm dev/nvidia-uvm none bind,optional,create=file
|
||||
lxc.mount.entry: /dev/nvidia-uvm-tools dev/nvidia-uvm-tools none bind,optional,create=file
|
||||
when: lxc_nvidia_gpu_mount
|
||||
|
||||
|
||||
50
roles/proxmox_lxc_provision/tasks/main.yml
Executable file
50
roles/proxmox_lxc_provision/tasks/main.yml
Executable file
@@ -0,0 +1,50 @@
|
||||
---
|
||||
- name: Check if container exists
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_vmid }}"
|
||||
state: current
|
||||
register: existing_container
|
||||
ignore_errors: true
|
||||
|
||||
- name: Skip if container already exists
|
||||
meta: end_host
|
||||
when: existing_container is succeeded
|
||||
|
||||
- name: Container source must be defined (lxc_clone_from or lxc_template)
|
||||
ansible.builtin.fail:
|
||||
msg: "Neither lxc_clone_from or lxc_template are defined"
|
||||
when: lxc_clone_from is undefined and lxc_template is undefined
|
||||
|
||||
- name: Clone container from another container or template, then update
|
||||
when: lxc_clone_from is defined
|
||||
block:
|
||||
- name: Clone from template
|
||||
ansible.builtin.include_tasks: clone.yaml
|
||||
register: clone_result
|
||||
|
||||
- name: Update container
|
||||
ansible.builtin.include_tasks: update.yaml
|
||||
vars:
|
||||
lxc_vmid: "{{ clone_result.vmid }}"
|
||||
register: lxc_result
|
||||
|
||||
- name: Create the new container
|
||||
ansible.builtin.include_tasks: create.yaml
|
||||
when: lxc_template is defined and lxc_clone_from is undefined
|
||||
|
||||
- name: Start the created container and wait for ssh
|
||||
vars:
|
||||
lxc_vmid: "{{ lxc_result.vmid }}"
|
||||
ansible.builtin.include_tasks: "{{ item }}"
|
||||
loop:
|
||||
- start.yaml
|
||||
- wait.yaml
|
||||
when: lxc_start
|
||||
|
||||
- name: Post clone updates
|
||||
when: lxc_clone_from is defined
|
||||
delegate_to: "{{ lxc_hostname }}"
|
||||
block:
|
||||
- name: Include post-clone tasks
|
||||
ansible.builtin.include_tasks: post-clone.yaml
|
||||
27
roles/proxmox_lxc_provision/tasks/post-clone.yml
Executable file
27
roles/proxmox_lxc_provision/tasks/post-clone.yml
Executable file
@@ -0,0 +1,27 @@
|
||||
---
|
||||
- name: Change root password
|
||||
ansible.builtin.user:
|
||||
name: root
|
||||
password: "{{ lxc_password | password_hash('sha512') }}"
|
||||
update_password: always
|
||||
when: lxc_password is defined
|
||||
|
||||
- name: Change admin password
|
||||
ansible.builtin.user:
|
||||
name: admin
|
||||
password: "{{ password | password_hash('sha512') }}"
|
||||
update_password: always
|
||||
when: password is defined
|
||||
|
||||
- name: Regenerate SSH host keys
|
||||
ansible.builtin.include_role:
|
||||
name: system_setup
|
||||
tasks_from: ssh
|
||||
vars:
|
||||
regenerate_ssh_keys: true
|
||||
|
||||
- name: Remove previous entry from known hosts
|
||||
ansible.builtin.known_hosts:
|
||||
name: "{{ hostvars[lxc_hostname]['ansible_host'] }}"
|
||||
state: absent
|
||||
delegate_to: localhost
|
||||
11
roles/proxmox_lxc_provision/tasks/start.yml
Executable file
11
roles/proxmox_lxc_provision/tasks/start.yml
Executable file
@@ -0,0 +1,11 @@
|
||||
---
|
||||
- name: Start the LXC container
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_result.vmid }}"
|
||||
state: started
|
||||
register: start_result
|
||||
retries: 3
|
||||
delay: 5
|
||||
until: start_result is success
|
||||
failed_when: start_result.failed and ('already running' not in start_result.msg)
|
||||
12
roles/proxmox_lxc_provision/tasks/stop.yml
Executable file
12
roles/proxmox_lxc_provision/tasks/stop.yml
Executable file
@@ -0,0 +1,12 @@
|
||||
---
|
||||
- name: Stop container if it is running
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_vmid | default(omit) }}"
|
||||
hostname: "{{ lxc_hostname | default(omit) }}"
|
||||
state: "stopped"
|
||||
register: stop_result
|
||||
failed_when: |-
|
||||
stop_result.failed and
|
||||
('not running' not in stop_result.msg) and
|
||||
('does not exist' not in stop_result.msg)
|
||||
22
roles/proxmox_lxc_provision/tasks/update.yml
Executable file
22
roles/proxmox_lxc_provision/tasks/update.yml
Executable file
@@ -0,0 +1,22 @@
|
||||
---
|
||||
- name: Update an LXC container
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_vmid }}"
|
||||
hostname: "{{ lxc_hostname }}"
|
||||
password: "{{ lxc_password | default(omit) }}" # Updating password does not work
|
||||
cores: "{{ lxc_cores }}"
|
||||
memory: "{{ lxc_memory }}"
|
||||
swap: "{{ lxc_swap }}"
|
||||
disk: "{{ lxc_disk }}"
|
||||
netif: '{"net0": "name=eth0,gw={{ lxc_gateway }},ip={{ lxc_ipv4 }},ip6={{ lxc_ipv6 | default(omit) }},bridge=vmbr0"}'
|
||||
pubkey: "{{ lookup('file', lxc_pubkey_file) | default(omit) }}"
|
||||
onboot: "{{ lxc_onboot | default(false) }}"
|
||||
startup: "{{ lxc_startup | default(omit) }}"
|
||||
features: "{{ lxc_features | default(omit) }}"
|
||||
timezone: "{{ lxc_timezone | default(omit) }}"
|
||||
nameserver: "{{ lxc_nameserver | default(omit) }}"
|
||||
state: present
|
||||
tags: "{{ lxc_tags | default(omit) }}"
|
||||
update: true
|
||||
register: lxc_result
|
||||
20
roles/proxmox_lxc_provision/tasks/wait.yml
Executable file
20
roles/proxmox_lxc_provision/tasks/wait.yml
Executable file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
- ansible.builtin.meta: refresh_inventory
|
||||
|
||||
- name: Wait a moment for container to be available in inventory
|
||||
ansible.builtin.pause:
|
||||
seconds: 2
|
||||
|
||||
- name: Debug - Check if container is in inventory
|
||||
ansible.builtin.debug:
|
||||
msg: "Container {{ lxc_hostname }} found with IP: {{ hostvars[lxc_hostname]['ansible_host'] | default('NOT FOUND') }}"
|
||||
|
||||
|
||||
- name: Wait for SSH to become available
|
||||
ansible.builtin.wait_for:
|
||||
host: "{{ hostvars[lxc_hostname]['ansible_host'] }}"
|
||||
port: 22
|
||||
delay: 3
|
||||
state: started
|
||||
register: ssh_wait_result
|
||||
until: ssh_wait_result is not failed
|
||||
Reference in New Issue
Block a user