Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- # This role will provision an Arch system by logging into the running Arch install media
- # and running raw commands through ssh. The Arch install media will not support running Ansible commands for some reason I dont quite grasp yet :p.
- # After booting the Arch install media, set a root password via 'passwd', run 'dhcpcd' to get
- #connected to the network, and run 'systemctl start sshd' to start the sshd server.
- #Alternatively I would imagine you can add these things to your own custom Arch install ISO so it is already there.
- #Also note that for the two scp commands to work, you have to have an ssh key in your ~/.ssh/ dir ,
- # and you need to put your sshd_config file in the /files directory inside your role.
- #variables are set in the playbook
- #Im sure there is a lot I did wrong or could do better, Im new to Ansible
- # Setting the password on the target system works with a hard coded password, but I cant get it to work as a variable
- # the way Im doing it. I just commented it out since you can set the password after reboot anyways.
- # I didnt add variables for the partition sizes and file system types yet, But I may eventually.
- - name: Update the System Clock
- raw: (timedatectl set-ntp true)
- - name: Create partitions on /dev/sda
- vars:
- bash_cmd: |
- sfdisk /dev/sda <<EOF
- ,1G,L,*
- ,2G,S
- , ,L
- EOF
- raw: "{{bash_cmd}}"
- - name: Format partition /dev/sda1
- raw: (mkfs.ext4 -F /dev/sda1)
- - name: Format partition /dev/sda3
- raw: (mkfs.ext4 -F /dev/sda3)
- - name: Create Swap space
- raw: (mkswap -f /dev/sda2)
- - name: Mount swap partition
- raw: (swapon /dev/sda2)
- - name: Mount the Root Filesystem on /mnt
- raw: (mount /dev/sda3 /mnt)
- - name: Create the /boot directory to mount the boot partition to
- raw: (mkdir /mnt/boot)
- - name: Mount /dev/sda1 (our boot partition) on the newly created /mnt/boot
- raw: (mount /dev/sda1 /mnt/boot)
- - name: Install base packages, and some python with pacstrap
- raw: (pacstrap /mnt base python python2 grub openssh)
- - name: Generate an fstab file (use -U or -L to define by UUID or labels, respectively)
- raw: (genfstab -U /mnt >> /mnt/etc/fstab)
- - name: Create the /root/.ssh directory
- raw: (mkdir /mnt/root/.ssh)
- # Local commands to copy files over since copy: or template: will not work
- - name: scp the public key over from local host to remote
- local_action: shell sshpass -p "{{temp_pass}}" scp {{pubkeys_path}}/{{ssh_key}} root@{{inventory_hostname}}:/mnt/root/.ssh/{{ssh_key}}
- # with_items:
- # - "{{ groups['arch_servers'] }}"
- - name: scp the sshd_config file with settings for key auth
- local_action: shell sshpass -p "{{temp_pass}}" scp "{{role_path}}/files/sshd_config" root@{{inventory_hostname}}:/mnt/etc/ssh/sshd_config
- # with_items:
- # - "{{ groups['arch_servers'] }}"
- - name: cat the public key into the authorized_keys file
- raw: (cat /mnt/root/.ssh/{{ssh_key}} >> /mnt/root/.ssh/authorized_keys)
- # Beginning of chroot commands.
- - name: Set time zone to New_York inside Chroot
- raw: (arch-chroot /mnt ln -sf /usr/share/zoneinfo/{{timezone}} /etc/localtime)
- - name: Run hwclock(8) to generate /etc/adjtime inside Chroot
- raw: (arch-chroot /mnt hwclock --systohc)
- - name: Add en_US.UTF-8 UTF-8 to the end of /etc/locale.gen inside Chroot
- raw: (arch-chroot /mnt sh -c "echo {{locale_gen}} >> /etc/locale.gen")
- - name: Generate them with locale-gen inside Chroot
- raw: (arch-chroot /mnt locale-gen)
- - name: Set the LANG variable in locale.conf(5) like LANG=en_US.UTF-8 inside Chroot
- raw: (arch-chroot /mnt sh -c "echo {{locale_conf}} >> /etc/locale.conf")
- - name: Create a unique suffix for each host in the inventory file
- set_fact: hostname_suffix={{item.0 + 1}}
- with_indexed_items: "{{ groups['arch_servers'] }}"
- when: item.1 == "{{inventory_hostname}}"
- - name: Create the hostname file inside Chroot
- raw: (arch-chroot /mnt sh -c "echo {{base_hostname}}{{hostname_suffix}} > /etc/hostname")
- - name: Add hostname and domain name in /etc/hosts inside Chroot
- raw: (arch-chroot /mnt sh -c "echo 127.0.0.1 {{base_hostname}}{{hostname_suffix}}.{{domain_name}} {{base_hostname}}{{hostname_suffix}} >> /etc/hosts")
- - name: Create a new hosts file with unique hostnames for each host
- local_action: shell echo {{base_hostname}}{{hostname_suffix}}.{{domain_name}} >> ../hosts_with_names
- - name: Enable the services inside Chroot
- raw: (arch-chroot /mnt systemctl enable dhcpcd sshd systemd-networkd systemd-resolved)
- #- name: Copy shadow file to /mnt/etc/shadow to retain root password
- # raw: (cp -a /etc/shadow /mnt/etc/shadow)
- #- name: Change the root pass on the Chroot
- # vars:
- # passwd_cmd: |
- # arch-chroot /mnt passwd root <<EOF
- # '{{temp_pass}}'
- # '{{temp_pass}}'
- # EOF
- # raw: '{{passwd_cmd}}'
- #- name: Fancy way of doing authorized_keys
- # authorized_key: user=root
- # exclusive=no
- # key="{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
- - name: Install GRUB bootloader on the /dev/sda MBR inside Chroot
- raw: (arch-chroot /mnt grub-install --target=i386-pc /dev/sda)
- - name: Generate the grub.cfg file in /boot/grb/grub.cfg inside Chroot
- raw: (arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg)
- - name: umount the /mnt directory and prepare for a reboot
- raw: (umount -R /mnt)
- - name: REBOOT!
- raw: (reboot)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement