refactor ♻️: Improve wait for LXC container boot and IP assignment

Enhanced the wait logic for LXC container boot and IP assignment by adding a check for container status and handling different scenarios. This change improves the reliability of the script.
This commit is contained in:
2025-11-08 09:05:36 +01:00
parent d855be2c80
commit b0318b073a

View File

@@ -44,12 +44,27 @@
args: args:
creates: "/etc/pve/lxc/{{ lxc_id }}.conf" creates: "/etc/pve/lxc/{{ lxc_id }}.conf"
- name: Wait for LXC to boot and get IP - name: Check if LXC container {{ lxc_name }} is running
ansible.builtin.command:
cmd: pct status {{ lxc_id }}
register: pct_status
changed_when: false
- name: Start the LXC container {{ lxc_name }} if stopped
ansible.builtin.command:
cmd: pct start {{ lxc_id }}
when: "'status: stopped' in pct_status.stdout"
register: start_result
changed_when: "'status: stopped' in pct_status.stdout"
- name: Wait until container has an IP address
ansible.builtin.shell: "pct exec {{ lxc_id }} -- hostname -I | awk '{print $1}'" ansible.builtin.shell: "pct exec {{ lxc_id }} -- hostname -I | awk '{print $1}'"
register: lxc_ip register: lxc_ip
until: lxc_ip.stdout | ansible.utils.ipaddr until: lxc_ip.stdout != ''
retries: 20 retries: 10
delay: 3 delay: 5
changed_when: false
failed_when: lxc_ip.stdout == ''
- name: Add temporary LXC to in-memory inventory - name: Add temporary LXC to in-memory inventory
ansible.builtin.add_host: ansible.builtin.add_host: