---
- name: Gather node information
  openshift_facts:
    role: node
    local_facts:
      bootstrapped: true

- name: Copy master bootstrap config locally
  slurp:
    src: "/etc/origin/master/bootstrap.kubeconfig"
  register: bootstrap_credentials
  delegate_to: "{{ groups.oo_first_master.0 }}"
  run_once: true
  no_log: true

- name: Distribute bootstrap kubeconfig if one does not exist
  copy:
    content: "{{ bootstrap_credentials.content | b64decode }}"
    dest: "/etc/origin/node/bootstrap.kubeconfig"
    mode: 0700
    owner: root
    group: root
    force: no
  no_log: true

- name: Start and enable node for bootstrapping
  systemd:
    name: "{{ openshift_service_type }}-node"
    state: restarted
    enabled: yes
    no_block: yes
  register: node_start

- when: node_start is failed
  block:
  - name: Get node logs
    command: journalctl --no-pager -n 300 -u {{ openshift_service_type }}-node
    register: logs_node
    ignore_errors: true
  - debug:
      msg: "{{ logs_node.stdout_lines }}"
  - fail:
      msg: Node start failed.

# The restart above triggers previously approved nodes to go NotReady
# We should wait for previously approved nodes to go Ready again
- name: Wait for node to be ready
  oc_obj:
    state: list
    kind: node
    name: "{{ openshift.node.nodename | lower }}"
  register: node_output
  delegate_to: "{{ groups.oo_first_master.0 }}"
  when: inventory_hostname in groups.oo_nodes_to_config
  until:
  - node_output.results is defined
  - node_output.results.returncode is defined
  - node_output.results.results is defined
  - node_output.results.returncode == 0
  - node_output.results.results[0].status is defined
  - node_output.results.results[0].status.conditions | selectattr('type', 'match', '^Ready$') | map(attribute='status') | join | bool == True
  # Give the node three minutes to come back online.
  retries: 36
  delay: 5
  failed_when: False
