- template/ : build Packer du template Windows 11 (ex /data/Installer/MyTemplate) - deploy/ : OpenTofu + playbooks Ansible (ex /data/Tofu) - bin/Deploy : menu de déploiement (lien depuis /usr/local/bin/Deploy) - jeton API Proxmox sorti des playbooks vers deploy/proxmox-secret.yml (hors git) - README.md : documentation de la structure et du mode opératoire - Ameliorations.md : recommandations pour fiabiliser les mesures - tests/ (dépôt beWeapon-LT) et archives/ exclus Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
2.9 KiB
YAML
78 lines
2.9 KiB
YAML
---
|
|
- name: Orchestration Démarrage et IPs des VMs
|
|
hosts: localhost
|
|
connection: local
|
|
gather_facts: false
|
|
vars_files:
|
|
- proxmox-secret.yml # secret hors git (voir proxmox-secret.yml.sample)
|
|
|
|
vars:
|
|
ansible_python_interpreter: "/data/bin/python3"
|
|
proxmox_api_host: "192.168.111.220" # Votre IP Proxmox
|
|
proxmox_user: "root@pam"
|
|
proxmox_token_id: "Stid"
|
|
csv_file_path: "./machines_ip.csv"
|
|
|
|
tasks:
|
|
- name: 1. Récupérer les données OpenTofu
|
|
command: tofu output -json vms_info
|
|
register: tofu_output_raw
|
|
changed_when: false
|
|
|
|
- name: Extraire la liste des VMs
|
|
set_fact:
|
|
vms_dict: "{{ tofu_output_raw.stdout | from_json }}"
|
|
|
|
- name: 3. Interroger l'agent QEMU via l'API Proxmox pour récupérer l'IP
|
|
uri:
|
|
url: "https://{{ proxmox_api_host }}:8006/api2/json/nodes/{{ item.value.node }}/qemu/{{ item.value.id }}/agent/network-get-interfaces"
|
|
method: GET
|
|
headers:
|
|
Authorization: "PVEAPIToken={{ proxmox_user }}!{{ proxmox_token_id }}={{ proxmox_token_secret }}"
|
|
validate_certs: false
|
|
status_code: [200]
|
|
register: agent_net_info
|
|
until:
|
|
- agent_net_info.json is defined
|
|
- agent_net_info.json.data.result is defined
|
|
- (agent_net_info.json.data.result | map(attribute='ip-addresses', default=[]) | flatten | selectattr('ip-address-type', 'equalto', 'ipv4') | map(attribute='ip-address') | reject('equalto', '127.0.0.1') | list | length) > 0
|
|
retries: 30
|
|
delay: 5
|
|
loop: "{{ vms_dict | dict2items }}"
|
|
|
|
- name: 4. Extraire et associer les IPs
|
|
set_fact:
|
|
final_vm_list: >-
|
|
{%- set result = [] -%}
|
|
{%- for res in agent_net_info.results -%}
|
|
{%- set vm_id = res.item.value.id -%}
|
|
{%- set vm_name = res.item.value.name -%}
|
|
{%- set ifaces = res.json.data.result | default([]) -%}
|
|
{%- set ips = [] -%}
|
|
{%- for iface in ifaces -%}
|
|
{%- set addrs = iface['ip-addresses'] | default([]) -%}
|
|
{%- for addr in addrs -%}
|
|
{%- if addr['ip-address-type'] == 'ipv4' and addr['ip-address'] != '127.0.0.1' -%}
|
|
{%- set _ = ips.append(addr['ip-address']) -%}
|
|
{%- endif -%}
|
|
{%- endfor -%}
|
|
{%- endfor -%}
|
|
{%- set main_ip = ips[0] | default('Non attribuee') -%}
|
|
{%- set _ = result.append({'name': vm_name, 'id': vm_id, 'ip': main_ip}) -%}
|
|
{%- endfor -%}
|
|
{{ result }}
|
|
|
|
- name: 5. Afficher les résultats à l'écran
|
|
debug:
|
|
msg: "VM {{ item.name }} (ID: {{ item.id }}) -> IP: {{ item.ip }}"
|
|
loop: "{{ final_vm_list }}"
|
|
|
|
- name: 6. Générer le fichier CSV
|
|
copy:
|
|
content: |
|
|
Nom_VM,ID_Proxmox,Adresse_IP
|
|
{% for vm in final_vm_list %}
|
|
{{ vm.name }},{{ vm.id }},{{ vm.ip }}
|
|
{% endfor %}
|
|
dest: "{{ csv_file_path }}"
|