1

I wonder what would be a good solution to manage an environment with a combination of dynamic and static hosts and vars and groups.

I'm thinking of a combination of physical hosts in a datacenter with a combination of specific tasks (physical database node) plus Proxmox Hosts managing some dynamic VMs, together with some cloud providers for managing new VMs in some situation, where the physical performance is not enough (outsource performance peeks).

In that case - a dynamic inventory is a good practice. Of course there are some plugins for AWS, Hetzner, etc. But is it possible to combine that together with static entries?

I build a database with all the entries and scripts that updates the list of cloud VMs per request in the database. All the dynamic hosts together with the static hosts and some group vars, host vars are stored in the database. And there is a webservice that outputs the JSON dynamic inventory that a python script loads and that is used as a dynamic inventory in my playbooks.

My question is - am I the only one with this kind of setup? When I think of a company with some VMWare hosts together with dynamic number of VMs on that hosts plus some other physical systems it would be "clear" that there should be some kind of "tool" or help for this setup. But as far as I can see, there is either a static inventory (YAML, INI) or a dynamic inventory (JSON). But does every admin develops their own dynamic inventory database? Or is there a given software "product" or "project"? Or is it a "better" practice to have different inventories for "static" and "dynamic" environments?

1 Answer 1

1

Ansible has several released inventory plugins. Yes, you can combine them, either as multiple -i options, or specify a directory containing multiple inventories.

For example, you might have all of these in inventory/stage/ and inventory/prod/ directories:

  • hcloud for Hetzner
  • aws_ec2 for AWS
  • YAML with a static host list
  • Your custom inventory script
  • constructed to generate groups and vars from expressions

Group names should be consistent for a given thing, so plays can use static patterns.

Constructed is pretty neat. Use Jinja expressions on facts from inventory or hosts to create groups and vars. For example, you might want some ec2 tag, plus some hostname regex, to be members of a group.

Should this not be sufficient, you may write your own inventory plugins. Usually, these talk to one API, say if you hack on a Proxmox inventory plugin. Or, abandon the multiple inventory on the Ansible side, find a multi cloud tool that works for you, and make that emit JSON for inventory.

1
  • Thanks, that works - I found out, that there is a different reason why my solution with my custom script is better. Because all the groups and services are highly dynamic in my environment (one day I use a machine for service a and tomorrow for service b). Also it contains informations about the "content" like the users. And then - I need always to "commit" a new static version to the repository. If I use a JSON dynamic inventory I can read all the details from the database. I would accept this answer as useful, but I don't have enough reputation on ServerFault.
    – TRW
    Feb 4, 2020 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .