Outils pour utilisateurs

Outils du site


tech:notes_ansible-inventory

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
tech:notes_ansible-inventory [2025/11/20 14:49] Jean-Baptistetech:notes_ansible-inventory [2025/11/20 14:55] (Version actuelle) Jean-Baptiste
Ligne 1: Ligne 1:
 +<!DOCTYPE markdown>
 +{{tag>Brouillon Ansible CA}}
 +
 +# Notes Ansible Inventory
 +
 +
 +Voir :
 +* [[ansible_inventory_script_-_inventaire_dynamique_3|Ansible inventaire script - inventaire dynamique vers statique - Conversion JSON YAML INI]]
 +* https://blog.stephane-robert.info/post/ansible-inventaire-static-precedence-tips/
 +
 +Script pour gérer un inventaire - openstack-ansible :
 +* openstack-ansible/scripts/inventory-manage.py
 +* https://superuser.openinfra.org/articles/complex-openstack-inventory-a-nightmare/
 +* https://docs.openstack.org/openstack-ansible/latest/reference/inventory/manage-inventory.html
 +
 +
 +
 +Linter un fichier inventaire 
 +~~~
 +ansible-lint inventory1.yml
 +
 +yamllint inventory1.yml
 +
 +ansible-inventory -i inventory1.yml -y --list > inventory2.yml
 +~~~
 +
 +Print all variables for all hosts from the command line
 +~~~bash
 +ansible -i inventory/local -m debug -a "var=hostvars" all
 +~~~
 +
 +Afficher les variables pour tous les hosts
 +~~~bash
 +ansible-inventory -i inventory.yml --graph --vars
 +~~~
 +
 +Afficher les variables pour un host particulier
 +~~~bash
 +# En YAML
 +ansible-inventory -i inventory.yml -y --host server1
 +
 +# En JSON
 +ansible-inventory -i inventory.yml --host server1
 +~~~
 +
 +## Plugin
 +
 +~~~
 +$ ansible-doc -t inventory -l
 +advanced_host_list                  Parses a 'host list' with ranges
 +auto                                Loads and executes an inventory plugin specified in a YAML config
 +awx.awx.controller                  Ansible dynamic inventory plugin for the Automation Platform Controller
 +community.general.cobbler           Cobbler inventory source
 +community.general.gitlab_runners    Ansible dynamic inventory plugin for GitLab runners
 +community.general.icinga2           Icinga2 inventory source
 +community.general.linode            Ansible dynamic inventory plugin for Linode
 +community.general.lxd               Returns Ansible inventory from lxd host
 +community.general.nmap              Uses nmap to find hosts to target
 +community.general.online            Scaleway (previously Online SAS or Online.net) inventory source
 +community.general.opennebula        OpenNebula inventory source
 +community.general.proxmox           Proxmox inventory source
 +community.general.scaleway          Scaleway inventory source
 +community.general.stackpath_compute StackPath Edge Computing inventory source
 +community.general.virtualbox        virtualbox inventory source
 +community.general.xen_orchestra     Xen Orchestra inventory source
 +constructed                         Uses Jinja2 to construct vars and groups based on existing inventory
 +generator                           Uses Jinja2 to construct hosts and groups from patterns
 +host_list                           Parses a 'host list' string
 +ini                                 Uses an Ansible INI file as inventory source
 +script                              Executes an inventory script that returns JSON
 +toml                                Uses a specific TOML file as an inventory source
 +yaml                                Uses a specific YAML file as an inventory source
 +~~~
 +
 +
 +
 +## Playbook
 +
 +Limiter l'inventaire en faisant une intersection
 +~~~yaml
 + hosts: "type_host:&{{ PLOP_LIMIT | default('all') }}"
 +~~~
 +
 +### Inventaire dynamique passé en extra-vars
 +
 +''play_roles_temp_inventory.yaml''
 +~~~yaml
 +#! /usr/bin/ansible-playbook
 +
 +---
 +
 +- hosts: localhost
 +  connection: local
 +  gather_facts: false
 +
 +  tasks:
 +    - name: add hosts from extra-vars
 +      add_host:
 +        name: '{{ item }}'
 +        groups: building
 +      loop: "{{ hosts_list.split('\n') }}"
 +      changed_when: false
 +
 +- hosts: building
 +  gather_facts: "{{ play_roles_gather_facts | default(false) | bool }}"
 +  tasks:
 +    - debug: msg="hosts_list={{ hosts_list | replace('\n',',') }}"
 +      run_once: true
 +      when: play_roles_debug | default(false) | bool
 +
 +    - name: Include Ansible roles
 +      include_role:
 +        name: "{{ role_name }}"
 +      loop: "{{ play_roles }}"
 +      loop_control:
 +        loop_var: role_name
 +      when: play_roles is defined
 +~~~
 +
 +## Tower
 +
 +Voir https://docs.ansible.com/ansible-tower/latest/html/administration/tipsandtricks.html#using-the-tower-cli-tool
 +
 +~~~bash
 +awx-manage inventory_import --source=inventory/ \
 +  --inventory-name="My Tower Inventory"
 +
 +awx-manage inventory_import --source=inventory/ \
 +  --inventory-name="My Tower Inventory" \
 +  --overwrite --overwrite-vars
 +
 +mkdir -p inventory-directory/group_vars
 +~~~
 +
 +Voir ''[[Ansible AWX - Tower Cli|awx import]]''
 +
 +
 +## Autres
 +
 +inventory_hostname et ansible_hostname 
 +
 +
 +
 +
 +
 +
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki