ansible/pis/01-download-and-prepare-ras...

95 lines
3.1 KiB
YAML
Raw Normal View History

# Run with
# ansible-playbook pis/download-and-prepare-raspi-image.yml
---
- hosts: 127.0.0.1
connection: local
gather_facts: false
vars:
target_folder: "/media/martin/data_linux/tmp/"
vars_prompt:
- name: "ansible_become_pass"
prompt: "Sudo password to mount raspi image"
- name: "new_hostname"
prompt: "New hostname for the PI"
private: no
tasks:
# --- Preparation ---
- name: Download Raspian image
get_url:
url: "https://downloads.raspberrypi.org/raspbian_lite_latest"
dest: "{{target_folder}}/raspian_lite_latest.zip"
- name: Unpack Image
unarchive:
src: "{{target_folder}}/raspian_lite_latest.zip"
dest: "{{target_folder}}"
creates: "{{target_folder}}/*raspbian*.img"
- name: Make Folders to mount to
file:
path: "{{item}}"
state: directory
with_items:
- "{{target_folder}}/mounted_raspi_image"
- "{{target_folder}}/mounted_raspi_image/boot"
- "{{target_folder}}/mounted_raspi_image/system"
- name: Setup Loopback
become: true
shell:
cmd: "losetup -P /dev/loop42 {{target_folder}}/*raspbian*.img"
creates: "/dev/loop42p1"
- name: Mount Boot Partition
become: true
shell:
warn: false
cmd: "mount /dev/loop42p1 {{target_folder}}/mounted_raspi_image/boot"
creates: "{{target_folder}}/mounted_raspi_image/boot/kernel.img"
- name: Mount System Partition
become: true
shell:
warn: false
cmd: "mount /dev/loop42p2 {{target_folder}}/mounted_raspi_image/system"
creates: "{{target_folder}}/mounted_raspi_image/system/bin"
# --- Actual work ---
- name: "Add SSH File to boot partition to allow for first remote login"
become: true
file:
path: "{{target_folder}}/mounted_raspi_image/boot/ssh"
state: touch
- name: "Writing new hostname to /etc/hostname"
become: true
copy:
content: "{{new_hostname}}"
dest: "{{target_folder}}/mounted_raspi_image/system/etc/hostname"
# --- Wind-down
- name: Unmount System Partition
become: true
shell:
warn: false
cmd: "umount /dev/loop42p2"
removes: "{{target_folder}}/mounted_raspi_image/system/bin"
- name: Unmount Boot Partition
become: true
shell:
warn: false
cmd: "umount /dev/loop42p1"
removes: "{{target_folder}}/mounted_raspi_image/boot/kernel.img"
- name: Tear down loop device
become: true
shell:
cmd: "losetup -d /dev/loop42"
removes: "/dev/loop42p1"
- name: Remove folders
file:
path: "{{item}}"
state: absent
with_items:
- "{{target_folder}}/mounted_raspi_image"
- "{{target_folder}}/mounted_raspi_image/boot"
- "{{target_folder}}/mounted_raspi_image/system"
- "{{target_folder}}/raspian_lite_latest.zip"
- name: Final Image
debug: |
The prepared image is ready at {{target_folder}}.
Copy it to sdcard with
dd bs=4M if=the_image of=/dev/your/sdcard