U1.28 Ubuntu Quick Start (QS): Swap file management with Ansible - chempkovsky/CS2WPF-and-CS2XAMARIN GitHub Wiki

Preliminary remark

sudo swapon --show
sudo swapon -s
free -h
sudo fallocate -l 1G /swapfile
ls -lh /swapfile
sudo chmod 600 /swapfile
ls -lh /swapfilels -lh /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
sudo swapon -s
free -h
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
cat /proc/sys/vm/swappiness
sudo nano /etc/sysctl.conf
  • add to /etc/sysctl.conf-file the line below
vm.swappiness=60

Ansible as productivity tool

Starting with

  • Pre-installed DHCP in the virtual environment (for example, a hardware implementation of a DHCP server in a modem)

  • Deploy four Ubuntu 20.04 TLS virtual machines. Consult the articles U1.01 and U1.02

    • Device name = u200401, ip = 192.168.100.12
    • Device name = u200402, ip = 192.168.100.3
    • Device name = u200403, ip = 192.168.100.17
    • Device name = UbuntuAnsible, ip = 192.168.100.26
  • For each machine u200401, u200402, u200403, UbuntuAnsible

    • run the command python3 --version
      • In our case it returns : Python 3.8.10
  • For each machine u200401, u200402, u200403, UbuntuAnsible

    • we have sudo-enabled user = yury with identical password for each machine
  • For each machine u200401, u200402, u200403, UbuntuAnsible

    • run the command
sudo apt install openssh-server
  • For the machine UbuntuAnsible
    • run the commands (no sudo prefix for the first four commands)
ssh-keygen
ssh-copy-id 192.168.100.12
ssh-copy-id 192.168.100.3
ssh-copy-id 192.168.100.17

Installing Ansible

$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

Installing geerlingguy.swap

  • For the machine UbuntuAnsible
    • run the command (no sudo prefix for command)
ansible-galaxy install geerlingguy.swap
Click to show the picture

picture

Installing creating inventory and sample playbook

Click to show the picture

picture

Create the folder

  • For the machine UbuntuAnsible
    • run command (no sudo prefix for command)
mkdir swapmng
cd swapmng

Create inventory

  • For the machine UbuntuAnsible
    • run command (no sudo prefix for command)
nano inventory.ini
  • insert definitions
[all]
node1 ansible_host=192.168.100.12
node2 ansible_host=192.168.100.3
node3 ansible_host=192.168.100.17

[swap128]
node1

[swap256]
node2

[swap512]
node3

Create a playbook to make swap enabled

  • For the machine UbuntuAnsible
    • run command (no sudo prefix for command)
nano swapon_pb.yml
  • modify swapon_pb.yml and save
---
- hosts: swap128
  vars:
    swap_file_size_mb: '128'
  roles:
    - geerlingguy.swap

- hosts: swap256
  vars:
    swap_file_size_mb: '256'
  roles:
    - geerlingguy.swap

- hosts: swap512
  vars:
    swap_file_size_mb: '512'
  roles:
    - geerlingguy.swap

Create a playbook to make swap disabled

  • For the machine UbuntuAnsible
    • run command (no sudo prefix for command)
nano swapoff_pb.yml
  • modify swapoff_pb.yml and save
---
- hosts: swap128
  vars:
    swap_file_state: absent
  roles:
    - geerlingguy.swap

- hosts: swap256
  vars:
    swap_file_state: absent
  roles:
    - geerlingguy.swap

- hosts: swap512
  vars:
    swap_file_state: absent
  roles:
    - geerlingguy.swap

Check the state of swap file for u200401, u200402, u200403

  • from inside u200401, u200402, u200403
    • run command
free -m
Click to show the picture

picture

Make swap enabled for u200401, u200402, u200403

  • from inside UbuntuAnsible
    • run command (no sudo prefix for command)
ansible-playbook -i inventory.ini --become --become-user=root -K swapon_pb.yml
Click to show the picture

picture

picture

Check the state of swap file for u200401, u200402, u200403

  • from inside u200401, u200402, u200403
    • run command
free -m
Click to show the picture

picture

Make swap disabled for u200401, u200402, u200403

  • from inside UbuntuAnsible
    • run command (no sudo prefix for command)
ansible-playbook -i inventory.ini --become --become-user=root -K swapoff_pb.yml
Click to show the picture

picture

Check the state of swap file for u200401, u200402, u200403

  • from inside u200401, u200402, u200403
    • run command
free -m
⚠️ **GitHub.com Fallback** ⚠️