---

# item 1 is machine info
# item 0 is robot info

# Need to do this again as we have to split profiles and systems as separate task and the reason for that is make sure
# to call cobbler sync before creating systems.

- name: "Set the machine and robot info"
  set_fact:
    machine_info: '{{ item.1 }}'
    robot_info: '{{ item.0 }}'

# Work on IPMI system

- name: "Check if system {{ machine_info.name }}-{{ robot_info.name }}-ipmi exists"
  command: "docker exec cobbler cobbler system find --name={{ machine_info.name }}-{{ robot_info.name }}-ipmi"
  register: ipmi_system_state
  changed_when: False


# Cobbler systems require an interface so just use host interface for IPMI (as IPMI can't be imaged via cobbler)

- name: "Add the system {{ machine_info.name }}-{{ robot_info.name }}-ipmi"
  command: "docker exec cobbler cobbler system add
    --name={{ machine_info.name }}-{{ robot_info.name }}-ipmi
    --hostname={{ machine_info.name }}-ipmi
    --interface={{ machine_info.host.interface }}
    --mac-address={{ machine_info.ipmi.MACAddress }}
    --ip-address={{ machine_info.ipmi.IPAddress }}
    --netmask={{ cobbler_dhcp_subnet_mask }}
    --gateway={{ cobbler_dhcp_router_ip }}
    --name-servers={{ cobbler_dhcp_router_ip }}
    --profile={{ machine_info.name }}-{{ robot_info.name }}
    --static=1
    --name-servers-search=\"{{ cobbler_dhcp_domain_name }}\""
  when: ipmi_system_state.stdout == ""

- name: "Update the system {{ machine_info.name }}-{{ robot_info.name }}-ipmi"
  command: "docker exec cobbler cobbler system edit
    --name={{ machine_info.name }}-{{ robot_info.name }}-ipmi
    --hostname={{ machine_info.name }}-ipmi
    --interface={{ machine_info.host.interface }}
    --mac-address={{ machine_info.ipmi.MACAddress }}
    --ip-address={{ machine_info.ipmi.IPAddress }}
    --netmask={{ cobbler_dhcp_subnet_mask }}
    --gateway={{ cobbler_dhcp_router_ip }}
    --name-servers={{ cobbler_dhcp_router_ip }}
    --profile={{ machine_info.name }}-{{ robot_info.name }}
    --static=1
    --name-servers-search=\"{{ cobbler_dhcp_domain_name }}\""
  when: ipmi_system_state.stdout != ""

# Work on host system

- name: "Check if system {{ machine_info.name }}-{{ robot_info.name }} exists"
  command: "docker exec cobbler cobbler system find --name={{ machine_info.name }}-{{ robot_info.name }}"
  register: system_state
  changed_when: False

- name: "Add the system {{ machine_info.name }}-{{ robot_info.name }}"
  command: "docker exec cobbler cobbler system add
    --name={{ machine_info.name }}-{{ robot_info.name }}
    --hostname={{ machine_info.name }}
    --interface={{ machine_info.host.interface }}
    --mac-address={{ machine_info.host.MACAddress }}
    --ip-address={{ machine_info.host.IPAddress }}
    --netmask={{ cobbler_dhcp_subnet_mask }}
    --gateway={{ cobbler_dhcp_router_ip }}
    --name-servers={{ cobbler_dhcp_router_ip }}
    --static=1
    --name-servers-search=\"{{ cobbler_dhcp_domain_name }}\"
    --power-type=ipmilan
    --power-address={{ machine_info.ipmi.IPAddress }}
    --power-user={{ machine_info.ipmi.userName }}
    --power-pass={{ cobbler_robot_vault_password_info[robot_info.name][machine_info.ipmi.vaultKey] }}
    --profile={{ machine_info.name }}-{{ robot_info.name }}
    --kopts=\"interface={{ machine_info.host.interface }}\""
  when: system_state.stdout == ""

- name: "Update the system {{ machine_info.name }}-{{ robot_info.name }}"
  command: "docker exec cobbler cobbler system edit
    --name={{ machine_info.name }}-{{ robot_info.name }}
    --hostname={{ machine_info.name }}
    --interface={{ machine_info.host.interface }}
    --mac-address={{ machine_info.host.MACAddress }}
    --ip-address={{ machine_info.host.IPAddress }}
    --netmask={{ cobbler_dhcp_subnet_mask }}
    --gateway={{ cobbler_dhcp_router_ip }}
    --name-servers={{ cobbler_dhcp_router_ip }}
    --static=1
    --name-servers-search=\"{{ cobbler_dhcp_domain_name }}\"
    --power-type=ipmilan
    --power-address={{ machine_info.ipmi.IPAddress }}
    --power-user={{ machine_info.ipmi.userName }}
    --power-pass={{ cobbler_robot_vault_password_info[robot_info.name][machine_info.ipmi.vaultKey] }}
    --profile={{ machine_info.name }}-{{ robot_info.name }}
    --kopts=\"interface={{ machine_info.host.interface }}\""
  when: system_state.stdout != ""
