Is it possible to define a hash at the environment (i.e. inventory) level?

Hi folks,

I need to model some data on a per-inventory basis, i.e. each inventory would define the same variables with different values. This seems simple enough when using simple variables i.e. no lists or hashes e.g.

production.ini

[jenkins-master]
mrs-jm01.dev.eddgrant.com

[mrs-database-servers]
mrs-db01.dev.eddgrant.com
mrs-dm02.dev.eddgrant.com

[all:vars]
http_proxy=www-cache.eddgrant.com:80

However, I want to use this in conjunction with Ansible’s environment feature and ideally want to be able to define a hash in each inventory file which I can then reference later on. As far as I can tell there is no longer any way to define a hash in an inventory file now that yaml inventory files have been deprecated. Is my understanding correct here?

I’m probably missing something but I can’t see an idiomatic Ansible way of doing this. Hence grateful for any tips.

Cheers,

Edd

Sorry folks, possibly bad form to self-reply purely in order to bump. Am a bit stuck on this though so some assistance would be invaluable.

Thinking about this problem has made me wonder if it would be possible/ useful for Ansible to natively support some sort of per-inventory yaml variables file? Perhaps something called ‘inventory_vars’ which worked in a similar way as ‘group_vars’ but for inventories?

Cheers,

Edd

that is just an organizational issue host_vars and group_vars are relative to current inventory,

inventory1/
hosts
group_vars/all

inventory2/
hosts
group_vars/all

Thanks Brian, I’m not sure I quite follow. Are you saying that I should be splitting my vars files up per-inventory? e.g.

production/
production.ini
host_vars/
group_vars/
webservers.yml
all.yml

development/
production.ini
host_vars/
group_vars/
webservers.yml
all.yml

If I do this then won’t I have to duplicate my group_vars files across each inventory folder? Is that considered Idiomatic usage? I’m asking as I’ve tried to follow the best practices page for layout and I haven’t noticed a layout that resembles your suggestion.

Cheers.

You don’t have to put your production and development environment files in different directories, just give them different names.

Thanks Michael, Sorry though, I’m afraid you’ve lost me, I’m not sure quite what you mean. Could you give an example?

Sure!

/some/dir/inventory
production
stage
development
group_vars/
all
webservers
host_vars/
xyz.example.com

etc

Gah, it’s clearly staring me in the face but I’m just not getting it (sorry!).

So using your example Michael, let’s say I want to define a hash called ‘proxy_details’ which, in each inventory, will hold different values for the same hash keys:

For my stage and production inventories it will look like this:

proxy_details: [http_proxy_host: abc.def.com, http_proxy_port: 80]

and for my development inventory it would look like this:

proxy_details: [http_proxy_host: zyx.wvu.com, http_proxy_port: 8080]

Where in your example would I define these hashes? (i.e. in which files and in which folders)

Cheers!

I wasn’t saying you had to, just showing that you can flexibly organize them in differnet ways, specially if you wanted different versions of the same group file (all) for each inventory. For your last question:

/some/dir/inventory
production
stage
development
group_vars/
all => proxy_details: [http_proxy_host: abc.def.com, http_proxy_port: 80]
webservers

development => proxy_details: [http_proxy_host: zwx.dev.com, http_proxy_port: 80]

or optionally, instead of in all
staging => proxy_details: [http_proxy_host: abc.def.com, http_proxy_port: 80]
production => proxy_details: [http_proxy_host: abc.def.com, http_proxy_port: 80]

Thanks both, I have a play with these suggestions and see how I get on.

Also note what you have above inside is not a hash.

If you want a hash on one line, use {}

proxy_details: { foo: bar, baz: glorp }

or express them on more than one line

proxy_details:
foo: bar
baz: glorp

Oh yeah, good point! I spend most of my time programming in Groovy where a Map (which is about as close to a hash as you can get) is defined with square brackets, as a result I sometimes find the Groovy → YAML context switch ends up in me writing nonsense YAML! :slight_smile: