azlin context use¶
Switch to a different Azure context
Description¶
The azlin context use command sets the specified context as the current active context. All subsequent azlin commands will use this context's subscription and tenant IDs for Azure operations.
Contexts provide kubectl-style multi-tenant Azure access, allowing you to easily switch between different Azure subscriptions, tenants, and authentication profiles without changing environment variables or configuration files.
Usage¶
Arguments¶
| Argument | Description |
|---|---|
NAME | Name of the context to activate (required) |
Options¶
| Option | Type | Description |
|---|---|---|
--config TEXT | Path | Custom config file path (default: ~/.azlin/config.toml) |
-h, --help | Flag | Show command help and exit |
Examples¶
Basic Context Switching¶
# Switch to production context
azlin context use production
# Switch to development context
azlin context use dev
# Switch to staging context
azlin context use staging
Using Custom Config File¶
# Use context from custom config location
azlin context use production --config ~/custom-config.toml
# Switch between team configs
azlin context use team-a --config ~/.azlin/team-a-config.toml
Workflow Examples¶
# List available contexts
azlin context list
# Switch to production
azlin context use production
# Verify current context
azlin context current
# Provision VM in production subscription
azlin new --name prod-vm
# Switch back to dev
azlin context use dev
# Provision VM in dev subscription
azlin new --name dev-vm
How It Works¶
When you run azlin context use NAME:
- Validation - Checks that the context exists in configuration
- Activation - Sets the context as the current active context
- Persistence - Saves the current context selection to config file
- Immediate Effect - All subsequent commands use the new context
The context configuration is stored in ~/.azlin/config.toml under the [contexts] section.
Context Structure¶
Each context contains:
- subscription_id - Azure subscription ID
- tenant_id - Azure tenant ID
- auth_profile - (Optional) Service principal authentication profile name
Example context in config:
[contexts.production]
subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
tenant_id = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"
auth_profile = "prod-sp"
[contexts.dev]
subscription_id = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
tenant_id = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
Troubleshooting¶
Context Not Found¶
Symptoms: Error "Context 'NAME' not found"
Solutions:
# List available contexts
azlin context list
# Create the context first
azlin context create NAME \
--subscription xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx \
--tenant yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy
Authentication Fails After Switching¶
Symptoms: Azure authentication errors after switching contexts
Solutions:
# Verify context is set correctly
azlin context current
# Test authentication
azlin auth test
# If using service principal, verify auth profile exists
azlin auth list
# Re-authenticate with Azure CLI
az login --tenant YOUR-TENANT-ID
Wrong Resources Appearing¶
Symptoms: Seeing VMs or resources from wrong subscription
Solutions:
# Verify current context
azlin context current
# Check which subscription Azure CLI is using
az account show
# Switch to correct context
azlin context use CORRECT-CONTEXT
# Verify again
azlin context current
Best Practices¶
Naming Conventions¶
Use clear, descriptive context names:
# Good naming
azlin context use prod-us-east
azlin context use dev-team-a
azlin context use staging-europe
# Avoid ambiguous names
azlin context use context1 # Bad
azlin context use temp # Bad
Team Workflows¶
# Personal contexts
azlin context use personal-dev
azlin context use personal-test
# Team contexts
azlin context use team-shared-dev
azlin context use team-shared-staging
azlin context use team-shared-prod
Multi-Tenant Organizations¶
# Client A contexts
azlin context use clienta-prod
azlin context use clienta-dev
# Client B contexts
azlin context use clientb-prod
azlin context use clientb-dev
# Internal contexts
azlin context use internal-prod
azlin context use internal-dev
Related Commands¶
azlin context list- List all available contextsazlin context current- Show current active contextazlin context create- Create a new contextazlin context delete- Delete a contextazlin context rename- Rename a contextazlin auth list- List authentication profiles
Source Code¶
- context.py - Context management logic
- cli.py - CLI command definition