Add Host to Group dynamically (or better: design question regarding a hacky solution)

15 views
Skip to first unread message

Daniel

unread,
Nov 6, 2018, 4:32:53 AM11/6/18
to Ansible Project
Hi everyone,

I have a playbook that installs a web application on multiple hosts (one host per customer).

The following variable hierarchy would be perfect for my environment:

- common.yml
- $environment
- $environment-$version.yml (because environment configuration can change with each new version)
- fqdn.yml

In my playbook I have solved this via host_vars, group_vars and include_vars:

./group_vars
./group_vars/devel.yml
./group_vars/test.yml
./group_vars/production.yml

./host_vars
./host_vars/customer1.yml
./host_vars/customer2.yml
./host_vars/customer3.yml

- fail:
msg: "app_version is not defined (application not yet deployed?)."
when: app_version is not defined

- name: Include common vars
include_vars:
file: "common.yml"

- name: Include system type specific vars
include_vars:
file: "{{ app_version }}/{{ app_environment }}.yml"

- name: HACK? HACK? HACK? Include hostname specific vars
include_vars:
file: "host_vars/{{ inventory_hostname }}.yml"

Because variables included with include_vars have a higher priority then group_vars and host_vars, I need to include them again, but It feels hacky.
Does anyone have any opinions on that or any suggestions?

My desired solution would be to add hosts to more specific groups. 
But I cannot configure this in the inventory, because the version to deploy is determined by a fact on the remote host.

./group_vars
./group_vars/devel.yml
./group_vars/devel-1.yml
./group_vars/devel-2.yml
./group_vars/test.yml
./group_vars/test-1.yml
./group_vars/test-2.yml
./group_vars/production.yml
./group_vars/production-1.yml
./group_vars/production-2.yml

I had a look at add_host, which first seemed to exactly what I need, but having a closer look it does not work for me.

So I will either have to live with that solution, or someone has a better idea.

Regards
Daniel
Reply all
Reply to author
Forward
0 new messages