Reputation: 1057
Is there a DRY way to specify host variables for hosts in multiple groups? e.g.:
all:
hosts:
mail.example.com:
ansible_host: 10.10.10.33
children:
webservers:
hosts:
foo.example.com:
ansible_host: 10.10.10.17
bar.example.com:
ansible_host: 10.10.10.18
dbservers:
hosts:
one.example.com:
ansible_host: 10.10.10.1
two.example.com:
ansible_host: 10.10.10.2
three.example.com:
ansible_host: 10.10.10.3
test:
hosts:
bar.example.com:
ansible_host: 10.10.10.18 # Don't like this being defined in a 2nd place
three.example.com:
ansible_host: 10.10.10.3 # =( Same
Is it possible to have host variables, like ansible_host
in the example (or a SW license) defined in only one place? I wasn't able to find anything suitable in the ansible documentation, so the placeholder solution is to use YAML anchors:
all:
hosts:
mail.example.com:
ansible_host: 10.10.10.33
children:
webservers:
hosts:
foo.example.com:
ansible_host: 10.10.10.17
bar.example.com: &bar
ansible_host: 10.10.10.18
dbservers:
hosts:
one.example.com:
ansible_host: 10.10.10.1
two.example.com:
ansible_host: 10.10.10.2
three.example.com: &three
ansible_host: 10.10.10.3
test:
hosts:
bar.example.com:
<< : *bar
three.example.com:
<< : *three
Upvotes: 4
Views: 1243
Reputation: 311238
Is it possible to have host variables, like ansible_host in the example (or a SW license) defined in only one place?
If you define variables for a host anywhere in your inventory file, those variables will be set for that host when Ansible runs. So for example, you could write your inventory like this:
all:
hosts:
mail.example.com:
ansible_host: 10.10.10.33
foo.example.com:
ansible_host: 10.10.10.17
bar.example.com:
ansible_host: 10.10.10.18
one.example.com:
ansible_host: 10.10.10.1
two.example.com:
ansible_host: 10.10.10.2
three.example.com:
ansible_host: 10.10.10.3
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
test:
hosts:
bar.example.com:
three.example.com:
But this would also work:
all:
hosts:
mail.example.com:
ansible_host: 10.10.10.33
children:
webservers:
hosts:
foo.example.com:
ansible_host: 10.10.10.17
bar.example.com:
ansible_host: 10.10.10.18
dbservers:
hosts:
one.example.com:
ansible_host: 10.10.10.1
two.example.com:
ansible_host: 10.10.10.2
three.example.com:
ansible_host: 10.10.10.3
test:
hosts:
bar.example.com:
three.example.com:
I would argue that the first version is a bit more obvious.
Upvotes: 8