Hi!
Is it possible to store files or templates per inventory? Similar to how we can store group_vars per inventory.
I often hit the issue with Ansible, that I would like to store a file or a template per inventory (TEST, PROD). These usually are large configuration files of some components (like Hadoop), that are almost completely different per inventory and have so many variables, that it seems worthless to create shell-like templates with a lot of variables in it. Especially, that these configuration files are nicely written, with a lot of nested structure, while ansible variables are just flat (IMHO using nested variables in Ansible is unfeasible either because it’s hard to do defaults and overrides, hard to do references between variables, etc).
Not only templates, I would also like to store binary files (zipfiles?) that could be different per inventory. Is that possible?
Thanks a lot for answers,
Krzysztof
You could pick up a different version of a file say for each environment
- name: copy environment-specific config
copy:
src: “configuration.{{environment}}.conf”
dest: /etc/someconf.conf
I’d argue that if there are lots of things that are different between test and prod then you are more likely to hit problems when your applications finally reach prod, so might be worth some pain to try and make some things (that aren’t directly related to scaling) the same in both test and production.
Also defaults in ansible are pretty easy. As well as defining defaults in your roles, you can do in-line defaults in your playbooks
- name: wait for startup
pause:
seconds: “{{ some_app_startup_pause_seconds | default(60 }}”
Hope this helps,
Jon
Hi,
You can use the magic variable inventory_dir and use it in your playbook. Here’s the default value:
ansible -m debug -a var=inventory_dir localhost
localhost | SUCCESS => {
“inventory_dir”: “/etc/ansible”
}
Regards,
Yannig
Thanks guys for your help,
I assume you confirm, that there is no such builtin feature in Ansible to manage inventory-level templates/files. I’m trying to keep away of doing custom things with Ansible like loading variables or files manually, but if that would be the only way of progressing, I’ll go for your solution.
One caveat is, that I can’t put any files in inventory directory, because Ansible tries to interpret them probably as inventory itself and fails with errors. I would need to store them on the side of the inventory directory. But I would use inventory_dir you @Yannig suggested variable to construct the directory name for this (something like {{inventory_dir}}.files or similar).
A bit off-topic I mentioned, was the default nested variables. @Jon, what I meant is that according to my knowledge it’s hard to manage defaults of nested variables and override them selectively. Sth like the following:
My default in the role would be:
master:
node1: abc
node2: def
node3: ghi
And now I would like to just specify one master.node3 in inventory group_vars, without touching the rest, sth like:
master.node3: xyz
OR
master:
node3: xyz
This didn’t work for me in Ansible. Of course there are workarounds:
- specify hash_behaviour=merge in ansible.cfg. This is unacceptable for me, because ansible.cfg is user specific, external to playbooks. So I can’t enforce users to put it in their configs.
- Copy whole defaults even if I want to modify just a single value. This solution denies the purpose of defaults.
- Do advanced yaml hacking, that I’m not yet aware of (using of aliases, etc.)
- Use jinja2 hash_merge filters to merge the defaults with what user provides. Use it in every role as a practice.
Do you agree with me, that there is no acceptable solution for this problem?
W dniu wtorek, 10 maja 2016 13:12:14 UTC+2 użytkownik Yannig Perre napisał:
Not sure I fully understand what you are trying to achieve so I may have the wrong idea here.
It maybe that you have just used node1, node2, node3 as example variable names, but if you are referring to machines you can refer to variables, including nested ones for other hosts in other groups inside your playbook - have a look at
http://docs.ansible.com/ansible/faq.html#how-do-i-access-a-variable-of-the-first-host-in-a-group
If I have a var which I can’t set declaratively in defaults or host/group vars I would calculate it using set_fact at runtime in my playbook.
Hope this helps,
Jon
Sorry, the variables “node1”,“node2”, “node3” or “master” were just example variables, unfortunate names. I just meant some children variables of some master variable.
W dniu środa, 11 maja 2016 07:44:05 UTC+2 użytkownik J Hawkesworth napisał: