- fail:
    msg: possibly wrong server?
  when: ansible_distribution != 'Archlinux'


- name: Disable IPv6 in live env (it is is broken)
  sysctl:
    name: net.ipv6.conf.all.disable_ipv6
    value: "1"
    state: present
    reload: yes

- name: set hostname
  hostname:
    name: "{{inventory_hostname}}"


- name: Partitions
  parted:
    device: /dev/vda
    number: 1
    part_start: 0%
    part_end: 100%
    flags: [ lvm ]
    state: present

- name: Volume group
  lvg:
    vg: vg.eval
    pvs: /dev/vda1
    pvresize: true

- name: Root lv
  lvol:
    lv: root
    resizefs: yes
    size: 100%VG
    vg: vg.eval

- name: Root FS
  filesystem:
    dev: /dev/vg.eval/root
    fstype: xfs
    opts: "-mreflink=1"

- name: Mount root
  mount:
    path: /mnt
    src: /dev/vg.eval/root
    fstype: xfs
    state: mounted

- name: Update pacman
  pacman:
    update_cache: true

- name: Install debootstrap
  pacman:
    pkg:
      - debootstrap

- name: Debian already installed?
  stat:
     path: /mnt/etc/os-release
  register: deb_installed

- name: debootstrap
  command: debootstrap stable /mnt http://deb.debian.org/debian/
  when: not deb_installed.stat.exists

- name: Root's .ssh
  file:
    state: directory
    path: /mnt/root/.ssh

- name: SSH for root
  copy:
    content: |
      ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyTqSTbfhzJUlAV3/WQPefOz/nX0xfGgBbb9mMxwXNm3OKpTLaSJZZ78aUvucCqbiWhWQKCKlUqMsaWCqHLIL5kFDgDRcfGhF8ERCjIG7TCPs1zPPoFVL8hvGeMmWsHgvm8pKjPFCp/NxjTDx3RmLzVyP1z8rLwD3X1xC2bRSETuRwwOIKLEJhzNSQ1EchTNRTH0CuMqVZHdQLVDP6Tdqo7WV61znELRW5ZPw7BPJU216+He7AziIGcLbv06JbuFyFsoZ+8dmH20rs/CwOnicFH/P44yOZmB+JWVzBosKkG8JliM/nzaru7aPPQe/7XIsOQ5iymm31WAK112htIy3N xstill@xstill-lenovo
    dest: /mnt/root/.ssh/authorized_keys

- name: Generat fstab
  shell: "genfstab /mnt > /mnt/etc/fstab"
     
- name: Install & setup sshd, grub, ONe
  command: 'arch-chroot /mnt env PATH=/usr/sbin:/usr/bin DEBIAN_FRONTEND=noninteractive {{item}}'
  loop:
    - apt-get install -yy ssh grub2 grub-pc linux-image-amd64 lvm2 wget
    - passwd --delete root
    - wget https://github.com/OpenNebula/addon-context-linux/releases/download/v5.12.0.2/one-context_5.12.0.2-1.deb -O one.deb
    - apt install -yy ./one.deb
    - grub-install --target=i386-pc /dev/vda
    - update-initramfs -u
    - update-grub

- name: Enable sshd
  file:
    src: /lib/systemd/system/ssh.service
    dest: /mnt/etc/systemd/system/multi-user.target.wants/ssh.service
    force: True # the target exists only in the final root
    follow: False
    state: link

- name: Set SSHD config
  lineinfile:
    path: /mnt/etc/ssh/sshd_config
    regexp: '{{item.regexp}}'
    line: '{{item.line}}'
  loop:
    - regexp: '^#?PermitRootLogin'
      line: 'PermitRootLogin prohibit-password'
    - regexp: '^#?MaxAuthTries'
      line: "MaxAuthTries 16"  # for the sake of having multiple keys
    - regexp: '^#?X11Forwarding'
      line: "X11Forwarding yes"
    - regexp: '^#?ChallengeResponseAuthentication'
      line: "ChallengeResponseAuthentication no"

# vim: shiftwidth=2 tabstop=2 softtabstop=2 ft=yaml.ansible
