CORD-985 Tests: LXC container connectivity, Juju is ready

Change-Id: I6d690cb744c09df7d3909bf04e57551606e10f6a
diff --git a/roles/create-lxd/tasks/main.yml b/roles/create-lxd/tasks/main.yml
index ebeeb8c..b6e3651 100644
--- a/roles/create-lxd/tasks/main.yml
+++ b/roles/create-lxd/tasks/main.yml
@@ -104,6 +104,11 @@
   tags:
     - skip_ansible_lint # connectivity check
 
+- name: Verify that containers have external connectivity
+  command: ansible containers -m uri -u ubuntu -a "url=http://www.google.com"
+  tags:
+    - skip_ansible_lint # connectivity check
+
 - name: Have containers use the apt-cache
   command: ansible containers -b -u ubuntu -m lineinfile -a "dest=/etc/apt/apt.conf.d/02apt-cacher-ng create=yes mode=0644 owner=root group=root regexp='^Acquire' line='Acquire::http { Proxy \"http://{{ apt_cacher_name }}:{{ apt_cacher_port | default('3142') }}\"; };'"
   tags:
diff --git a/roles/juju-setup/tasks/main.yml b/roles/juju-setup/tasks/main.yml
index 554add9..69b13bf 100644
--- a/roles/juju-setup/tasks/main.yml
+++ b/roles/juju-setup/tasks/main.yml
@@ -31,6 +31,20 @@
     src={{ juju_config_name }}_juju_config.yml.j2
     dest={{ juju_config_path }}
 
+# check that containers are ready to be accessed, juju add-machine may error
+- name: Check that machines are available for Juju
+  command: ansible containers -m ping -u ubuntu
+  tags:
+    - skip_ansible_lint # connectivity check
+  retries: 3
+  delay: 10
+
+- name: Check that Juju is actually ready
+  juju_facts:
+  until: juju_machines["juju.cord.lab"] is defined and juju_machines["juju.cord.lab"]["agent_state"] == "started"
+  retries: 40
+  delay: 15
+
 # For setwise operations on desired vs Juju state:
 # list of active juju_machines names: juju_machines.keys()
 # list of active juju_services names: juju_services.keys()
@@ -39,6 +53,10 @@
   when: "{{ head_lxd_list | map(attribute='service') | list | reject('undefined') | map('format_string', '%s.'~site_suffix ) | difference( juju_machines.keys() ) | length }}"
   command: "juju add-machine ssh:{{ item }}"
   with_items: "{{ head_lxd_list | map(attribute='service') | list | reject('undefined') | map('format_string', '%s.'~site_suffix ) | difference( juju_machines.keys() ) }}"
+  register: result
+  until: result | success
+  retries: 3
+  delay: 10
 
 # run this again, so machines will be in the juju_machines list
 - name: Obtain Juju Facts after machine creation