Loading repository data…
Loading repository data…
sshaufi / repository
A repository dedicated to practicing and experimenting with DevOps tools, scripts, and automation.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
This repository serves as a platform for my personal DevOps training, showcasing my skills in scripting with Python and Shell Scripting, as well as utilizing DevOps tools such as Ansible and Terraform. Its primary objectives are:
Since I try to keep it simple, I aim to maintain the scripts and code with minimal dependencies possible. Additionally, it's important that they remain readable without excessive commenting. I will thoroughly explain the solution on each section.
Throughout this README, I will reference three servers. Please refer to the table below. I will refer to the servers by their names.
| Name | Hostname | os | Comments |
|---|---|---|---|
| cloud | cloud.ss.fish | Ubuntu | My personal cloud server |
| ss | ss.fish | OpenBSD | My personal OpenBSD server |
| ec2 | Ubuntu | This server will be created on an AWS EC2 instance |
To update all servers, I'll employ both Ansible and Python scripts. The Python script is crucial for converting the CSV file into an INI format for the Ansible inventory. Since servers are listed in a CSV file for other automation purposes, it's necessary.
Ansible Playbooks:
On all three servers, the playbook will run as the user named ansible, which I created for security purposes. This user can execute specific commands without a password when using sudo and doas, as I have manually configured.
Please note that on both the cloud and ec2 server playbooks, I do not use the apt module. Instead, I manually run the command in the Ansible playbook due to this GitHub issue #51663. Additionally, in both server playbooks, it will check if a reboot is needed after every update.
The inventory is located here: ansible-playbook/hosts.csv and will be converted by the script csv_to_ini.py to ansible-playbook/hosts. This script will run automatically when using the run.sh command and can also be executed manually. Further details on the run.sh command will be explained on further section.
To create an EC2 instance in AWS, I utilized Terraform and aws-cli. aws-cli is only needed for authentication and connection to my AWS account. To make this work, I created three files: provider.tf, main.tf, and output.tf. Let me explain what each of these files does:
hashicorp/aws as the provider so Terraform will work with AWS.ap-southeast-2).ami-09c8d5d747253fb7a (Ubuntu 22.04 LTS, x86), sets the instance type to t2.micro, and names it "DevOps-Training".provider.tf file.apt and apt-get commands can run passwordless with sudo so Ansible will work.
To remotely monitor system statistics on all servers, I'll utilize the psutil Python library for reading system statistics and Python paramiko library for establishing SSH connections remotely.
Originally, I planned to create the system reader from scratch using available Unix tools and parsing, but this approach would complicate things, especially since I intend to use this script on three different platforms (Linux, OpenBSD, and Darwin [MacOS]). Instead, it's simpler to use the psutil library since it already works across all these platforms.
I prefer to start most of my scripts with a shebang to env, like below:
#!/usr/bin/env python3
This way, I can run the script without specifying python3 or python in front of it. Using env also ensures that the OS will pick the correct Python version/program, and since Python isn't always located in the same path on every platform, this approach is more robust.
This script was created with the assistance of the psutil documentation. It displays various system metrics, including Hostname, Platform, Uptime, CPU usage, Memory usage, Network Card IPs, Public IP, and Root disk usage.
Using Psutil
psutil.cpu_percent() to obtain CPU usage in percentage with half-second readings for quick updates.psutil.virtual_memory().psutil.disk_usage().Using Unix tools
psutil.boot_time(), I may revisit this approach in the future.hostname command.Using Platform Library
This script is currently incomplete and somewhat messy. In the future, I plan to improve it by adding the following readings and features:
However, if I end up adding too many features, it might be more efficient to use other specialized tools that can handle these tasks more effectively than a custom script. Nevertheless, this script serves as a valuable practice exercise for me to improve my skills and expanding my knowledge in the realm of DevOps.
The script sys_reading.py lacks the capability to SSH into servers and parse CSV files. I do not intend to add this functionality as it would complicate the script further as its already look messy. Additionally, this script is separated because it will execute sys_reading.py on the hosts themselves. In the future, I may consider combining both scripts.
This script parses ansible-playbook/hosts.csv and SSHs into each host listed in it to execute the sys_reading.py script locally.
Initially, I intended to use the pandas library for parsing the CSV file. However, I discovered that Python's built-in csv module functions well for small datasets and offers cleaner code. As a result, I opted to switch to the csv library instead of pandas.
To streamline operations, all the functionalities described above are connected using a single shell script run.sh.
This shell script also utilizes a shebang to env, similar to all the Python scripts above:
#!/usr/bin/env sh
The script accepts four arguments and performs five actions: 'create', 'destroy', 'stats', 'update', and runs everything when no argument is given. When using the argument, it's possible to specify a character like 'c', 'd', 's', or 'u'.
create|c
terraform apply with the -auto-approve argument.destroy|d
terraform destroy with the -auto-approve argument.stats|s
sys_reading_remote.py, which runs sys_reading.py locally on the server.update|u
csv_to_ini.py to convert hosts.csv to hosts.ansible-playbook commands will use the hosts file as inventory and run with the argument ANSIBLE_HOST_KEY_CHECKING=False to disable fingerprint checks on the first SSH connection.No Arguments When running run.sh with no argument, it will execute the script in the following order:
Details on usage for each section are provided below.
This section demonstrates how to utilize this repository and the run.sh command for automating and simplifying various tasks. Additionally, I provide links to the STDOUT of all the commands.
The ansible-playbook directory contains playbooks for all three servers and the inventory file hosts.
To upgrade the cloud server, run the following command in the ansible-playbook directory:
ansible-playbook -i hosts update_upgrade_cloud.yml
To execute updates on all servers easily, simply run run.sh with the argument u as shown below:
./run.sh u
The basic terraform arguments of plan, apply and destroy are working.
Plan
terraform plan
Apply
terraform apply
Destroy
terraform destroy
The creation and termination of EC2 instance can be simplify and automate with running the run script with the argument c and d. **Cre