main.yml 2.5 KB

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