This Python script automates the creation of a network topology in OpenStack with 3 virtual machines across 2 networks, as shown in the network diagram.
The script automates all the required tasks:
- B1: Authentication using clouds.yaml
- B2: Create networks nhomXX_net1, nhomXX_net2
- B3: Create subnets nhomXX_subnet1, nhomXX_subnet2
- B4: Create ports with specific IP addresses:
- 192.168.X.11 (pc0 on net1)
- 192.168.X.12 (pc1 on net1)
- 192.168.(X+1).11 (pc0 on net2)
- 192.168.(X+1).12 (pc2 on net2)
- B5: List available images
- B6: List available flavors
- B8: List available keypairs
- B9: Create instances with base64-encoded init script
- B10: Verify the complete setup
nhomXX_pc2 ---- nhomXX_net2 ---- nhomXX_pc0 ---- nhomXX_net1 ---- nhomXX_pc1
.12 192.168.(X+1).0/24 .11/.11 192.168.X.0/24 .12
Where:
nhomXX_pc0acts as a router/gateway between the two networksnhomXX_pc1is connected to net1nhomXX_pc2is connected to net2
- OpenStack Environment: Access to an OpenStack cloud
- clouds.yaml: Configuration file with authentication details
- Python Dependencies: Install required packages
- Install Python dependencies:
pip install -r requirements.txt- Ensure your
clouds.yamlfile is properly configured in the current directory or in~/.config/openstack/clouds.yaml
python3 openstack_automation.pypython3 openstack_automation.py 5This will create networks with IP ranges:
- net1: 192.168.5.0/24
- net2: 192.168.6.0/24
# Clean up group 1 resources
python3 openstack_automation.py --cleanup
# Clean up group 5 resources
python3 openstack_automation.py --cleanup 5# Clean up all resources created in current session
python3 openstack_automation.py --cleanup-allpython3 openstack_automation.py --helpchmod +x openstack_automation.py
./openstack_automation.pyThe script uses the following naming convention:
- Networks:
Nhom{XX}_Q12_net1,Nhom{XX}_Q12_net2 - Subnets:
Nhom{XX}_Q12_subnet1,Nhom{XX}_Q12_subnet2 - Ports:
Nhom{XX}_Q12_pc0_net1,Nhom{XX}_Q12_pc1_net1,Nhom{XX}_Q12_pc0_net2,Nhom{XX}_Q12_pc2_net2 - Instances:
Nhom{XX}_Q12_pc0,Nhom{XX}_Q12_pc1,Nhom{XX}_Q12_pc2
Where XX is the group number (zero-padded to 2 digits).
For example, group 1 creates resources with prefix Nhom01_Q12.
Each instance is created with a base64-encoded initialization script that:
- Updates the system packages
- Installs basic networking tools
- Configures network interfaces
- Enables IP forwarding on the router (pc0)
- Installs and configures nginx web server
- Creates a test web page
The script includes comprehensive error handling:
- Checks for existing resources to avoid conflicts
- Provides detailed error messages
- Gracefully handles authentication failures
- Validates resource availability before creation
The script provides detailed output for each step:
- ✅ Success indicators
⚠️ Warnings for existing resources- ❌ Error messages with details
- 📊 Summary of created resources
- Authentication Issues: Verify your clouds.yaml configuration
- Resource Conflicts: The script handles existing resources gracefully
- Permission Issues: Ensure your OpenStack user has necessary permissions
- Network Issues: Check if the IP ranges don't conflict with existing networks
🚀 Starting OpenStack Network Automation
Group Number: 1
Network 1: 192.168.1.0/24
Network 2: 192.168.2.0/24
==================================================
🔐 B1. Authenticating with OpenStack...
✅ Authentication successful!
🌐 B2. Creating networks...
Creating network: nhom01_net1
✅ Network nhom01_net1 created successfully!
Creating network: nhom01_net2
✅ Network nhom01_net2 created successfully!
🔗 B3. Creating subnets...
Creating subnet: nhom01_subnet1 (192.168.1.0/24)
✅ Subnet nhom01_subnet1 created successfully!
...
🎉 All tasks completed successfully!