Ansible - Inventaire dynamique depuis Proxmox
1. Création d'un inventaire dynamique Ansible avec Proxmox
1.1. Installer la collection ansible community.general
- Installer la collection ansible
community.general
$ ansible-galaxy collecction install community.general
1.2. Créer le fichier d'inventaire dynamique 'homelab.proxmox.yml'
- Créer le fichier
inventories/homelab.proxmox.yml- Je vais réutiliser le token
tofucréé précédemment
- Je vais réutiliser le token
inventories/homelab.proxmox.yml
---
# doc: https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_inventory.html#examples
plugin: community.general.proxmox
url: https://pve1.home.lab:8006
user: 'tofu@pve'
token_id: 'tofu'
token_secret: '85b13993-73c5-4z72-87b8-54t80bca6e62'
- #todo créer une variable d'environnement ou ansible vault pour stocker le
token_secret
1.3. Modifier le fichier ansible.cfg
- Référencer le chemin vers le fichier d'inventaire par défaut dans le fichier
ansible.cfg
ansible.cfg
[defaults]
inventory = inventories/homelab.proxmox.yml
host_key_checking = false
forks = 2
roles_path = roles
[ssh_connection]
pipelining = true
2. Utilisation du plugin community.general.proxmox
2.1. Lister les machines virtuelles du cluster Proxmox (par défaut)
- Utiliser la commande
ansible-inventoy --list
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped",
"proxmox_all_lxc",
"proxmox_all_qemu",
"proxmox_all_running",
"proxmox_all_stopped",
"proxmox_nodes",
"proxmox_pve1_lxc",
"proxmox_pve1_qemu",
"proxmox_pve2_lxc",
"proxmox_pve2_qemu"
]
},
"proxmox_all_qemu": {
"hosts": [
"vault1",
"gitlab1",
"kube-master1",
"ipa1",
"kube-worker1",
"awx1",
"bdd1",
"bootstrap1",
"prometheus1",
"bdd2",
"refalmalinux9",
"grafana1",
"labstrap1",
"harbor1",
"kube-worker2",
"buildah1",
"log1",
"nautobot1"
]
},
"proxmox_all_running": {
"hosts": [
"vault1",
"gitlab1",
"kube-master1",
"ipa1",
"kube-worker1",
"awx1",
"bdd1",
"bootstrap1",
"bdd2",
"refalmalinux9",
"harbor1",
"kube-worker2",
"buildah1",
"nautobot1"
]
"proxmox_all_stopped": {
"hosts": [
"prometheus1",
"grafana1",
"labstrap1",
"log1"
]
},
"proxmox_nodes": {
"hosts": [
"pve1",
"pve2"
]
},
"proxmox_pve1_qemu": {
"hosts": [
"vault1",
"gitlab1",
"kube-master1",
"ipa1",
"kube-worker1",
"awx1",
"bdd1"
]
},
"proxmox_pve2_qemu": {
"hosts": [
"bootstrap1",
"prometheus1",
"bdd2",
"refalmalinux9",
"grafana1",
"labstrap1",
"harbor1",
"kube-worker2",
"buildah1",
"log1",
"nautobot1"
]
}
}
2.2. Tester la connexion ansible aux serveurs
- Tester la connexion au serveurs en ligne du domaine
home.lab
$ ansible -i inventories/homelab.proxmox.yml -m ping proxmox_all_running