On 09/12/2021 08:19, Chris Jefferies wrote:
I use ansible a lot for work and for my home lab. I love it.I keep needing an approach that so far has stumped me, so asking here for advice. I want to create a single file (an icinga2_hosts.conf) which has a separate collection of attributes from each host in my hosts inventory file. I would like to use jinja2 to template one instance and iterate through the hosts building the file in one go; not host by host in the usual ansible approach... although if a single file can be built by that method, I'm all ears.
You can iterate through your hosts with {% for host in ansible_play_hosts %} ... {% endfor %} and use hostvars[host]['myattribute'] to access an attribute from you inventory. To create the file, use template or copy with content parameter and add run_once: yes delegate_to: localhost Regards Racke
I think I could duplicate the hosts section in the vars section and then iterate the yaml data for the single file with a loop. I could actually create a file for each host and that would actually work in icinga2. But I want it in a single file for future readability and to avoid having to look in each host file for some anomaly. I could give up on ansible for this and use python and jinja2 to pass the hosts inventory file to a jinja2 render function. Is there a way to actually read the hosts inventory in traditional ansible style and iterate it into a single file using jinja2 as the template mechanism? I've wanted to do this in other scenarios but abandoned those attempts. Any tips and tricks appreciated. Chris. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com <mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5b0de281-4c29-49f7-bbcf-0fe2ef957b70n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/5b0de281-4c29-49f7-bbcf-0fe2ef957b70n%40googlegroups.com?utm_medium=email&utm_source=footer>.
-- Ecommerce and Linux consulting + Perl and web application programming. Debian and Sympa administration. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/60992f0e-5e83-0cdc-7594-8a56c7c114f8%40linuxia.de.
OpenPGP_signature
Description: OpenPGP digital signature