From 06269c1884f95f946c44b6a708128de69fb002f2 Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Fri, 28 Apr 2017 13:15:44 +0100 Subject: [PATCH] Use wait_for_connection instead of wait_for to check container After creating the container or configuring the bind mounts for the container, we used a task to wait for SSH to respond on the container, using the Ansible wait_for module trying to connect to port 22. When managing containers in Ansible using other connection plugins that use LXC commands on the physical host to execute modules, there may not be an SSH daemon running within the container. In these situations we can accomplish the same check without SSH by using the wait_for_connection module (new in ansible 2.3) to verify that Ansible has a working execution path into the container, but not necessarily via SSH. Change-Id: I1399b6727ec8894dd3b9c464f6addbeea1c1f4f9 --- .../common-tasks/os-lxc-container-setup.yml | 16 ++++++---------- .../inventory/group_vars/all_containers.yml | 8 ++++++++ playbooks/lxc-containers-create.yml | 13 ++++++------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/playbooks/common-tasks/os-lxc-container-setup.yml b/playbooks/common-tasks/os-lxc-container-setup.yml index 2c05b23d5b..170628cde9 100644 --- a/playbooks/common-tasks/os-lxc-container-setup.yml +++ b/playbooks/common-tasks/os-lxc-container-setup.yml @@ -124,16 +124,12 @@ tags: - common-lxc -- name: Wait for container ssh - wait_for: - port: "22" - delay: "{{ ssh_delay }}" - search_regex: "OpenSSH" - host: "{{ ansible_host }}" - delegate_to: "{{ physical_host }}" - register: ssh_wait_check - until: ssh_wait_check | success - retries: 3 +- name: Wait for container connectivity + wait_for_connection: + connect_timeout: "{{ lxc_container_wait_params.connect_timeout | default(omit) }}" + delay: "{{ lxc_container_wait_params.delay | default(omit) }}" + sleep: "{{ lxc_container_wait_params.sleep | default(omit) }}" + timeout: "{{ lxc_container_wait_params.timeout | default(omit) }}" when: - (_mc is defined and _mc | changed) or (_ec is defined and _ec | changed) - not is_metal | bool diff --git a/playbooks/inventory/group_vars/all_containers.yml b/playbooks/inventory/group_vars/all_containers.yml index 9b611ea761..e158d7b0d7 100644 --- a/playbooks/inventory/group_vars/all_containers.yml +++ b/playbooks/inventory/group_vars/all_containers.yml @@ -21,3 +21,11 @@ lxc_container_config_list: # Needed by playbooks/common-tasks/os-lxc-container-setup.yml lxc_container_log_path: "/var/log/lxc" +## Parameters provided to the wait_for_connection module after a container +## reboot is triggered by the playbook +lxc_container_wait_params: + # Wait 3 seconds before attempting the first connection + delay: 3 + # Wait 60 seconds for the container to respond + timeout: 60 + diff --git a/playbooks/lxc-containers-create.yml b/playbooks/lxc-containers-create.yml index f1fc4e6e09..1555b499a6 100644 --- a/playbooks/lxc-containers-create.yml +++ b/playbooks/lxc-containers-create.yml @@ -25,13 +25,12 @@ roles: - role: "lxc_container_create" post_tasks: - - name: Wait for ssh to be available - wait_for: - port: "22" - host: "{{ ansible_host | default(inventory_hostname) }}" - search_regex: OpenSSH - delay: 1 - delegate_to: "{{ physical_host }}" + - name: Wait for container connectivity + wait_for_connection: + connect_timeout: "{{ lxc_container_wait_params.connect_timeout | default(omit) }}" + delay: "{{ lxc_container_wait_params.delay | default(omit) }}" + sleep: "{{ lxc_container_wait_params.sleep | default(omit) }}" + timeout: "{{ lxc_container_wait_params.timeout | default(omit) }}" vars: is_metal: "{{ properties.is_metal|default(false) }}" tags: