Maak een Ansible-playbook voor geautomatiseerde resource-tagging in mijn infrastructuur
description
Door resource-tagging te automatiseren, kun je het resourcebeheer verbeteren, naleving waarborgen en rapportageprocessen stroomlijnen, wat leidt tot een verbeterde operationele efficiëntie.
prompt
try_prompt
Help me bij het ontwerpen van een Ansible-playbook dat de tagging van resources in mijn infrastructuur automatiseert voor betere organisatie en beheer. Mijn resource-typen omvatten: {{re ... more
generate_helper
...
ai_answers
provider: openai
model: gpt-4.1-nano
Natuurlijk! Hier is een voorbeeld van een Ansible-playbook dat automatisch de tag "omgeving=productie" toepast op EC2-instances:
```yaml
---
- name: Tag EC2-instances voor productieomgeving
hosts: localhost
connection: local
gather_facts: false
vars:
region: us-east-1 # Pas dit aan naar jouw regio
ec2_instance_ids:
- i-xxxxxxxxxxxxxxxxx # Vervang door je eigen instance IDs
tasks:
- name: Voeg tags toe aan EC2-instances
community.aws.ec2_tag:
region: "{{ region }}"
resources: "{{ ec2_instance_ids }}"
tags:
omgeving: productie
```
**Let op:** Zorg dat je de `community.aws` collectie hebt geïnstalleerd (`ansible-galaxy collection install community.aws`) en dat je juiste AWS-credentials hebt geconfigureerd.
---
### Tagging-strategie en naamgevingsconventies
1. **Gebruik consistente en beschrijvende tags:**
- **Omgeving:** zoals `omgeving=productie`, `omgeving=ontwikkeling`, `omgeving=test`
- **Kostenplaats:** bijvoorbeeld `kostenplaats=marketing`
- **Applicatie:** bijvoorbeeld `app=webserver`
- **Beheerder:** bijvoorbeeld `beheerder=johndoe`
- **Versie:** bijvoorbeeld `versie=1.0.2`
2. **Standaardiseer naamgevingen:**
- Gebruik korte, maar duidelijke namen
- Vermijd spaties; gebruik koppeltekens of onderstrepingstekens indien nodig
- Bijvoorbeeld: `naam=web-server-01`
3. **Gebruik tags voor automatische organisatie:**
- Bijvoorbeeld, tags kunnen worden gebruikt voor kostenallocatie, automatiseringsscripts, monitoring, enz.
4. **Documenteer je tagging-standaarden:**
- Zorg dat alle teamleden de tagging-conventies kennen en toepassen
5. **Automatiseer waar mogelijk:**
- Gebruik Ansible, Terraform of andere tools om consistentie te waarbongen en menselijke fouten te minimaliseren
---
Mocht je meer resource-typen willen taggen of specifieke standaarden willen implementeren, laat het gerust weten!
provider: deepseek
model: deepseek-chat
Zeker, ik help je graag met het ontwerpen van een Ansible-playbook voor het taggen van EC2-instances. Hier is een complete oplossing:
## Ansible Playbook: `tag_ec2_instances.yml`
```yaml
---
- name: Automatische tagging van EC2 instances
hosts: localhost
connection: local
gather_facts: false
vars:
aws_region: "eu-west-1" # Pas aan naar jouw AWS regio
environment_tag: "productie"
tasks:
- name: Haal alle EC2 instances op
amazon.aws.ec2_instance_info:
region: "{{ aws_region }}"
register: ec2_instances
- name: Tag EC2 instances met omgevingslabel
amazon.aws.ec2_tag:
region: "{{ aws_region }}"
resource: "{{ item.instance_id }}"
state: present
tags:
Omgeving: "{{ environment_tag }}"
Beheer: "Ansible"
LaatstBijgewerkt: "{{ ansible_date_time.iso8601 }}"
loop: "{{ ec2_instances.instances }}"
when: ec2_instances.instances | length > 0
- name: Bevestig tagging operatie
debug:
msg: "Successvol getagged {{ ec2_instances.instances | length }} EC2 instances met omgeving={{ environment_tag }}"
```
## Uitgebreide versie met aanvullende tags:
```yaml
---
- name: Uitgebreide automatische tagging van EC2 instances
hosts: localhost
connection: local
gather_facts: false
vars:
aws_region: "eu-west-1"
project_name: "jouw-project" # Pas aan naar jouw project
cost_center: "IT-123" # Pas aan naar jouw cost center
contact_email: "beheer@jouwbedrijf.nl"
tasks:
- name: Haal EC2 instances op zonder omgevingstag
amazon.aws.ec2_instance_info:
region: "{{ aws_region }}"
filters:
"tag:Omgeving": absent
register: untagged_instances
- name: Tag nieuwe instances met complete set labels
amazon.aws.ec2_tag:
region: "{{ aws_region }}"
resource: "{{ item.instance_id }}"
state: present
tags:
Omgeving: "productie"
Project: "{{ project_name }}"
CostCenter: "{{ cost_center }}"
Contact: "{{ contact_email }}"
BeheerDoor: "Ansible"
AangemaaktOp: "{{ item.launch_time }}"
LaatstBijgewerkt: "{{ ansible_date_time.iso8601 }}"
AutoStop: "true" # Voor kostenoptimalisatie
loop: "{{ untagged_instances.instances }}"
when: untagged_instances.instances | length > 0
tags:
- tagging
- ec2
```
## Tagging Strategie en Standaarden
### **Verplichte Tags:**
```
Omgeving: productie | ontwikkeling | test | acceptatie
Project: project-naam
CostCenter: financiële-code
Contact: email@bedrijf.nl
BeheerDoor: Ansible
```
### **Aanbevolen Tags:**
```
AangemaaktOp: YYYY-MM-DD
LaatstBijgewerkt: YYYY-MM-DD
AutoStop: true/false
Backup: true/false
Sla: business-critical | high | medium | low
```
### **Naamgevingsconventies:**
1. **Tag Namen:** Gebruik PascalCase (Omgeving, ProjectNaam)
2. **Tag Waarden:** Gebruik lowercase (productie, ontwikkeling)
3. **Project Namen:** Gebruik kebab-case (mijn-web-app)
4. **Datum Formaat:** ISO 8601 (YYYY-MM-DD)
### **Uitvoering:**
1. **Installeer vereiste collecties:**
```bash
ansible-galaxy collection install amazon.aws
```
2. **Configureer AWS credentials:**
```bash
export AWS_ACCESS_KEY_ID="jouw-access-key"
export AWS_SECRET_ACCESS_KEY="jouw-secret-key"
```
3. **Voer het playbook uit:**
```bash
ansible-playbook tag_ec2_instances.yml
```
### **Best Practices:**
- **Consistentie:** Houd tag namen en waarden consistent across alle resources
- **Documentatie:** Houd een centrale tag policy bij
- **Review:** Voer regelmatig tag compliance checks uit
- **Automation:** Integreer tagging in je CI/CD pipeline
- **Governance:** Gebruik AWS Resource Groups voor tag-based beheer
Deze strategie zorgt voor betere kostenallocatie, security compliance en operationeel beheer van je infrastructuur.

