Using Ansible’s Documentation#
Ansible configures Linux (and other OS) VMs to a specification. Writing Ansible playbooks is tricky at first because it’s usually easier to just enter the commands to do the Linux configuration. However, like Terraform, Ansible ensures that the VM is in the configuration you want, even if something has changed or gone wrong. Just like with Terraform you need to use Ansible’s documentation to figure out how to do things. Unlike Terraform, Ansible does not change based on the cloud vendor. In this lab we’ll explore the Ansible Documentation to find out how to format a disk.
Note
Reuse the lab09
directory you created in the Using Terraform’s Documentation lab.
Start Manually#
If you followed the in-class demonstration for the Using Terraform’s Documentation lab you named your extra disk lab09
. That will make a block device appear named /dev/disk/by-id/google-lab09
. Here are the Linux commands to format and mount the disk:
you@cis91:~$ sudo mkfs.ext4 /dev/disk/by-id/google-lab09
you@cis91:~$ sudo mount /dev/disk/by-id/google-lab09 /mnt
You can verify that the disk is now available with the df
command:
you@cis91:~$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 9974088 1882232 8075472 19% /
devtmpfs 490268 0 490268 0% /dev
tmpfs 494588 0 494588 0% /dev/shm
tmpfs 98920 928 97992 1% /run
tmpfs 5120 0 5120 0% /run/lock
tmpfs 494588 0 494588 0% /sys/fs/cgroup
/dev/loop0 56960 56960 0 100% /snap/core18/2566
/dev/loop1 64768 64768 0 100% /snap/core20/1623
/dev/loop2 308480 308480 0 100% /snap/google-cloud-cli/77
/dev/loop3 69504 69504 0 100% /snap/lxd/22753
/dev/loop4 49152 49152 0 100% /snap/snapd/17029
/dev/sda15 106858 5321 101537 5% /boot/efi
tmpfs 98916 0 98916 0% /run/user/1002
/dev/sdb 16337788 24 15482520 1% /mnt
If you want to make the filesystem available after a reboot you have to add a line to the /etc/fstab
file:
you@cis91:~$ echo "/dev/disk/by-id/google-lab09 /mnt ext4 defaults 0 0" | sudo tee -a /etc/fstab
Use Google to Figure Out Ansible#
We want Ansible to do the steps, instead of doing them manually. That way we can be sure they’re applied correctly and repeatably. I have found that the best way to learn how to do things in Ansible is by searching the documentation with Google. Google the term “format filesystem ansible”. At the bottom of the page there are examples. Ansible examples are super helpful! Use an example to do the equivalent of the mkfs.ext4
command.
Now search Google for, “mount filesystem ansible”. Use the first example (Mount DVD) and do some modifications. Here’s what the task should look like:
- name: Mount my filesystem
ansible.posix.mount:
path: /mnt
src: /dev/disk/by-id/google-lab09
fstype: ext4
opts: defaults
state: present
Turn In#
There’s nothing to turn in. This is just for practice.