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:
@@ -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:
|
||||||
|
|||||||
Reference in New Issue
Block a user