backup-disk-onoff in python
This commit is contained in:
13
.continue/rules/main.md
Normal file
13
.continue/rules/main.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Project Architecture
|
||||
|
||||
This is a ansible project for a small network of hosts:
|
||||
|
||||
- Roles in `/roles`
|
||||
- Inventory in `/inventory`
|
||||
- Host definition in `site.xml`
|
||||
|
||||
## Coding Standards
|
||||
|
||||
- There are some scripts/files in other languages like python
|
||||
- The project is managed in git.
|
||||
- Follow the existing naming conventions: All ansible yaml files should have the ending .yaml
|
||||
38
roles/backup_server/files/backup-disk-onoff.py
Normal file
38
roles/backup_server/files/backup-disk-onoff.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import subprocess
|
||||
import json
|
||||
|
||||
# Run the systemctl command and capture the output
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["systemctl", "list-units", "--type=automount", "--no-legend", "--output=json"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error executing command: {e.stderr}")
|
||||
exit(1)
|
||||
|
||||
# Parse the JSON output
|
||||
try:
|
||||
units = json.loads(result.stdout)
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Error parsing JSON: {e}")
|
||||
exit(1)
|
||||
|
||||
# Filter units that are loaded, active, and sub: running
|
||||
running_units = []
|
||||
for unit in units:
|
||||
if (
|
||||
unit.get("load") == "loaded" and
|
||||
unit.get("active") == "active" and
|
||||
unit.get("sub") == "running" and
|
||||
unit.get("unit", "").startswith("backup-")
|
||||
):
|
||||
running_units.append(unit)
|
||||
|
||||
# Output the results
|
||||
print("Units with load: loaded, active: active, sub: running:")
|
||||
for unit in running_units:
|
||||
print(f" - {unit['unit']}: {unit['description']}")
|
||||
Reference in New Issue
Block a user