---

- block:

  - name: make Bamboo user group
    group:
      name: bamboo
      state: present
      gid: 1101

  - name: make Bamboo user
    user:
      name: bamboo
      uid: 1101
      groups: bamboo,syslog
      state: present

  - name: make bamboo home directory
    file:
      name: /home/bamboo/.ssh
      state: directory
      owner: bamboo
      group: bamboo

  - name: install deps
    apt:
      name: "{{item}}"
      state: present
    with_items:
      - git

  - set_fact:
      vault_token: "{{ lookup('env', 'VAULT_TOKEN') }}"

  - name: Install SSH key
    copy:
      content: "{{ lookup('hashi_vault', 'secret=secret/github/zooxbors:private_key token={{ vault_token }}') }}"
      dest: /home/bamboo/.ssh/id_rsa
      owner: bamboo
      group: bamboo
      mode: 0400

  - name: set up passwordless_sudo for bamboo user
    copy:
      src: passwordless_sudo
      dest: /etc/sudoers.d/passwordless_sudo
      mode: 0600
      owner: root
      group: root
      validate: "visudo --check --file %s"

  - name: copy bazelrc
    template:
      src: bazelrc
      dest: /home/bamboo/.bazelrc
      owner: bamboo
      group: bamboo
    when: bazel_memory_mb is defined and bazel_cpu is defined and bazel_io is defined

  - name: Update /etc/hosts
    lineinfile:
      dest: /etc/hosts
      line: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }} {{ inventory_hostname }} {{ inventory_hostname_short }}"
      state: present


  - include: clean_bazel.yml
    tags:
      -  deploy_clean_bazel

  become: yes

