Notes Ansible Inventory

Voir :

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/

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

ansible -i inventory/local -m debug -a "var=hostvars" all

Afficher les variables pour tous les hosts

ansible-inventory -i inventory.yml --graph --vars

Afficher les variables pour un host particulier

# 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

hosts: "type_host:&{{ PLOP_LIMIT | default('all') }}"

Inventaire dynamique passé en extra-vars

play_roles_temp_inventory.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

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 awx import

Autres

inventory_hostname et ansible_hostname