---

- block:

  - debug:
      msg: "limit robots: {{limit_robots}}"
      verbosity: 0

  - name: Fetch robot information from the robot info service
    uri:
      url: "{{ cobbler_robot_info_url }}"
      method: "GET"
      return_content: yes
      status_code: 200
      body_format: json
    register: cobbler_all_robot_info
    delegate_to: localhost
    when: limit_robots == 'all'
    retries: 10
    delay: 30
    become: no

  - name: Fetch a single robot's information from the robot info service
    uri:
      url: "{{ cobbler_robot_info_url }}?name={{limit_robots}}"
      method: "GET"
      return_content: yes
      status_code: 200
      body_format: json
    register: cobbler_filtered_robot_info
    when: limit_robots != 'all'
    delegate_to: localhost
    retries: 10
    delay: 30
    become: no

  - name: "Configuring for all robots"
    set_fact:
      cobbler_nodes: '{{ cobbler_all_robot_info.json["items"] }}'
    when: cobbler_all_robot_info.skipped is not defined
    delegate_to: localhost
    become: no

  - name: "Configuring for just one robot"
    set_fact:
      cobbler_nodes: '{{ cobbler_filtered_robot_info.json["items"] }}'
    when: cobbler_filtered_robot_info.skipped is not defined
    delegate_to: localhost
    become: no

  - name: Verify robot exists
    fail:
      msg: Robot not found
    when: cobbler_nodes|length == 0


  - name: Get the vault info
    set_fact:
      cobbler_robot_vault_password_info: "{{ cobbler_robot_vault_password_info | combine({ item.name: lookup('hashi_vault', 'secret=' + item.vaultIPMIPath) }) }}"
    with_items:
      - "{{ cobbler_nodes }}"
    delegate_to: localhost
    become: no

  - include: 01_nat_masquerading.yml

  - include: 02_compose_container.yml

  - include: 03_import_images.yml
    with_items:
      - "{{ cobbler_images }}"

  - name: "Unmount dir /etc/docker-compose/cobbler/dist/image"
    mount:
      name: "/etc/docker-compose/cobbler/dist/image"
      state: absent

  # reposync takes huge amount of time and we don't need it to complete
  # as we don't depend on the internal debian mirror instead depend on ubuntu's
  # This is needed to make cobbler happy.
  - name: Run reposync
    command: "timeout {{ cobbler_reposync_timeout_in_seconds }} docker exec cobbler cobbler reposync"
    failed_when: False

  - include: 04_profiles.yml
    with_subelements:
      - "{{ cobbler_nodes }}"
      - machines
    when: deploy_mode != "operator"

  - include: 04_profiles_operator.yml
    when: deploy_mode == "operator"

  - include: 05_systems.yml
    with_subelements:
      - "{{ cobbler_nodes }}"
      - machines
    when: deploy_mode != "operator"

  - include: 05_systems_operator.yml
    when: deploy_mode == "operator"

  - name: restart cobbler
    docker_service:
      project_src: /etc/docker-compose/cobbler
      state: present
      restarted: yes

  - name: reload internal_nic
    shell: "ifdown {{ cobbler_internal_network_nic }} ; ifup {{ cobbler_internal_network_nic }}"

  - name: sync cobbler
    command: "docker exec cobbler cobbler sync"
    register: cobbler_sync
    retries: 2
    delay: 10
    until: cobbler_sync|succeeded

  become: yes


