main.yml 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ---
  2. - name: Fail if not running Nix
  3. ansible.builtin.raw: grep Nix /etc/os-release
  4. changed_when: false
  5. - name: Install Python3 if needed
  6. ansible.builtin.raw: bash -c "which python3 || nix-env -iA nixos.python3"
  7. - name: Gather facts now that Python is installed
  8. ansible.builtin.setup:
  9. - name: Find correct device
  10. ansible.builtin.set_fact:
  11. device: "{{ item }}"
  12. when:
  13. - item.value.host is defined
  14. - item.value.host != ""
  15. - item.value.removable|int == 0
  16. with_items:
  17. - "{{ ansible_devices | dict2items }}"
  18. loop_control:
  19. label: "{{ item.key }}"
  20. - name: Wipe /dev/{{ device.key }} and label GPT
  21. ansible.builtin.shell:
  22. cmd: sgdisk -og /dev/{{ device.key }}
  23. - name: Create boot partition
  24. ansible.builtin.shell:
  25. cmd: sgdisk -n 1::+500M -t 1:ef00 /dev/{{ device.key }}
  26. - name: Create root partition
  27. ansible.builtin.shell:
  28. cmd: sgdisk -n 2::0 -t 2:8300 /dev/{{ device.key }}
  29. - name: Rescan disk
  30. community.general.parted:
  31. device: /dev/{{ device.key }}
  32. register: device
  33. - name: Build prefix
  34. ansible.builtin.set_fact:
  35. device_prefix: "{{ device.disk.dev }}{% if 'nvme' in device.disk.dev %}p{% endif %}"
  36. - name: Format boot partition
  37. community.general.filesystem:
  38. device: "{{ device_prefix }}1"
  39. fstype: vfat
  40. - name: Format root partition
  41. community.general.filesystem:
  42. device: "{{ device_prefix }}2"
  43. fstype: xfs
  44. - name: Mount root partition
  45. ansible.builtin.shell:
  46. cmd: mount {{ device_prefix }}2 /mnt
  47. - name: Create /mnt/boot
  48. ansible.builtin.file:
  49. path: /mnt/boot
  50. state: directory
  51. - name: Mount boot partition
  52. ansible.builtin.shell:
  53. cmd: mount {{ device_prefix }}1 /mnt/boot
  54. - name: Generate nix hardware config
  55. ansible.builtin.shell:
  56. cmd: nixos-generate-config --root /mnt
  57. - name: Copy configuration.nix
  58. ansible.builtin.template:
  59. src: "{{ role_path }}/../kodi-config/templates/configuration.nix.j2"
  60. dest: /mnt/etc/nixos/configuration.nix
  61. owner: root
  62. group: root
  63. mode: '0644'
  64. - name: Install Nix
  65. ansible.builtin.shell:
  66. cmd: nixos-install --no-root-password
  67. - name: Reboot asynchronously
  68. ansible.builtin.shell:
  69. cmd: "sleep 5 && /run/current-system/sw/bin/systemctl reboot"
  70. async: 1
  71. poll: 0
  72. - name: Wait for the reboot and reconnect
  73. ansible.builtin.wait_for:
  74. port: 22
  75. host: '{{ inventory_hostname }}'
  76. search_regex: OpenSSH
  77. delay: 15
  78. timeout: 60
  79. delegate_to: localhost
  80. become: false
  81. - meta: end_host