refactor: use module_defaults for proxmox API connection
- Replace YAML merge keys (<<:) with module_defaults block in main.yml - Simplify connection variables to individual vars instead of dictionary - Remove redundant connection params from individual task files - Document standalone task usage requires play-level module_defaults - Update README examples with new variable pattern
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
---
|
||||
- name: Query Proxmox for existing LXCs
|
||||
community.proxmox.proxmox_lxc_info:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
community.proxmox.proxmox_lxc_info: {}
|
||||
register: proxmox_lxcs
|
||||
|
||||
- name: Check if LXC with hostname already exists
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
---
|
||||
- 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 }}"
|
||||
@@ -23,7 +22,7 @@
|
||||
become: yes
|
||||
register: resize_result
|
||||
changed_when: resize_result.rc == 0 and 'already at specified size' not in resize_result.stderr
|
||||
failed_when:
|
||||
failed_when:
|
||||
- resize_result.rc != 0
|
||||
- "'already at specified size' not in resize_result.stderr"
|
||||
when: lxc_size is defined
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
---
|
||||
- ansible.builtin.include_tasks: stop.yaml
|
||||
- ansible.builtin.include_tasks: stop.yml
|
||||
|
||||
- name: Convert container to template
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
|
||||
state: template
|
||||
hostname: "{{ lxc_hostname }}"
|
||||
state: template
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
---
|
||||
- name: Create an LXC container
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_vmid | default(omit) }}"
|
||||
hostname: "{{ lxc_hostname }}"
|
||||
password: "{{ lxc_password }}"
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
- ansible.builtin.include_tasks: stop.yaml
|
||||
- ansible.builtin.include_tasks: stop.yml
|
||||
|
||||
- name: Delete a container
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_vmid | default(omit) }}"
|
||||
hostname: "{{ lxc_hostname | default(omit) }}"
|
||||
state: absent
|
||||
|
||||
@@ -1,50 +1,66 @@
|
||||
---
|
||||
- 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
|
||||
- name: Proxmox LXC provision
|
||||
module_defaults:
|
||||
community.general.proxmox: &proxmox_defaults
|
||||
api_host: "{{ proxmox_api_host }}"
|
||||
api_port: "{{ proxmox_api_port }}"
|
||||
api_user: "{{ proxmox_api_user }}"
|
||||
api_token_id: "{{ proxmox_api_token_id }}"
|
||||
api_token_secret: "{{ proxmox_api_token_secret }}"
|
||||
validate_certs: "{{ proxmox_api_validate_certs }}"
|
||||
node: "{{ proxmox_node }}"
|
||||
community.proxmox.proxmox_lxc_info: *proxmox_defaults
|
||||
block:
|
||||
- name: Clone from template
|
||||
ansible.builtin.include_tasks: clone.yaml
|
||||
register: clone_result
|
||||
- name: Check if container exists
|
||||
community.general.proxmox:
|
||||
vmid: "{{ lxc_vmid }}"
|
||||
state: current
|
||||
register: existing_container
|
||||
ignore_errors: true
|
||||
|
||||
- name: Update container
|
||||
ansible.builtin.include_tasks: update.yaml
|
||||
- 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:
|
||||
file: clone.yml
|
||||
register: clone_result
|
||||
|
||||
- name: Update container
|
||||
ansible.builtin.include_tasks:
|
||||
file: update.yml
|
||||
vars:
|
||||
lxc_vmid: "{{ clone_result.vmid }}"
|
||||
register: lxc_result
|
||||
|
||||
- name: Create the new container
|
||||
ansible.builtin.include_tasks:
|
||||
file: create.yml
|
||||
when: lxc_template is defined and lxc_clone_from is undefined
|
||||
|
||||
- name: Start the created container and wait for ssh
|
||||
vars:
|
||||
lxc_vmid: "{{ clone_result.vmid }}"
|
||||
register: lxc_result
|
||||
lxc_vmid: "{{ lxc_result.vmid }}"
|
||||
ansible.builtin.include_tasks:
|
||||
file: "{{ item }}"
|
||||
loop:
|
||||
- start.yml
|
||||
- wait.yml
|
||||
when: lxc_start
|
||||
|
||||
- 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
|
||||
- name: Post clone updates
|
||||
when: lxc_clone_from is defined
|
||||
delegate_to: "{{ lxc_hostname }}"
|
||||
block:
|
||||
- name: Include post-clone tasks
|
||||
ansible.builtin.include_tasks:
|
||||
file: post-clone.yml
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
---
|
||||
- name: Start the LXC container
|
||||
community.general.proxmox:
|
||||
<<: "{{ proxmox_api_connection }}"
|
||||
vmid: "{{ lxc_result.vmid }}"
|
||||
state: started
|
||||
register: start_result
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
---
|
||||
- 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"
|
||||
state: stopped
|
||||
register: stop_result
|
||||
failed_when: |-
|
||||
stop_result.failed and
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
---
|
||||
- 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
|
||||
password: "{{ lxc_password | default(omit) }}"
|
||||
cores: "{{ lxc_cores }}"
|
||||
memory: "{{ lxc_memory }}"
|
||||
swap: "{{ lxc_swap }}"
|
||||
|
||||
Reference in New Issue
Block a user