# Fail if /etc/zoox/role exists and contains something other than the requested machine_role.
# Leading or trailing whitespace is ignored.
# This check is mainly to prevent unintended use of `setup.sh dev` on vehicles.
# Pass '--skip-tags check_role' to skip this check.
- block:

  - name: Read /etc/zoox/role
    slurp:
      src: "/etc/zoox/role"
    register: role_slurp_result
    changed_when: false
    failed_when: role_slurp_result is failed and 'file not found' not in role_slurp_result.msg | lower

  - name: Fail if content of /etc/zoox/role conflicts with the requested role
    fail:
      msg: "Role '{{ zoox_machine_role }}' was specified but /etc/zoox/role already contains '{{ role_slurp_result['content'] | b64decode | trim }}'"
    when:
      - "'content' in role_slurp_result"
      - "role_slurp_result['content'] | b64decode | trim != zoox_machine_role"

  tags: check_role

- name: Set machine role
  copy:
    content: "{{ zoox_machine_role }}"
    dest: /etc/zoox/role
