Skip to content

Ansible - Custom Dynamic, Constructed Inventory Using extra_vars to filter hostvars

Notifications You must be signed in to change notification settings

jseifeddine/dynamic-constructed-inventory-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

repo to document how you can use a -e, --extra_var to filter on any hostvars from a dynamic, constructed inventory using ansible.builtin.constructed as well as a custom dynamic inventory plugin.

./dynamic-mysql.yml:

#   MySQL Dynamic Inventory
#   https://github.com/j-oss2023/mysql-dynamic-inventory # Custom Dynamic Plugin
#   
#   Required columns: 
#   'inventory_group', 'inventory_hostname'
#
#   TLDR:
#   Besides the required columns, each column will be assigned 
#   as an ansible hostvar dynamically.

plugin: mysql-dynamic-inventory
db_host: backend
db_user: ansible_db_user
db_pass: ansible_db_pass
db_name: customers
db_query: |
  SELECT 
    'motorvehicle_co' as inventory_group,
    host as inventory_hostname,
    address as ansible_host,
    account as customer from dealerships

./constructed.yml

---
plugin: ansible.builtin.constructed
strict: true
use_extra_vars: true
compose:
  play_customer: play_customer
groups:
  target: customer == play_customer

ansible-playbook -i ./dynamic-mysql.yml -i ./constructed.yml playbooks/setup.yml -e "play_customer='Ferrari'" -l target

Requirements

ansible-core >= 2.11

About

Ansible - Custom Dynamic, Constructed Inventory Using extra_vars to filter hostvars

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published