Iterate host definitions into single file

67 views
Skip to first unread message

Chris Jefferies

unread,
Dec 9, 2021, 2:19:49 AM12/9/21
to Ansible Project
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. 

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.

Dick Visser

unread,
Dec 9, 2021, 2:59:58 AM12/9/21
to ansible...@googlegroups.com
On Thu, 9 Dec 2021 at 08:20, Chris Jefferies <chr...@gmail.com> 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. 

What do you mean by this?
Can you give an example?


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. 

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-proje...@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.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Stefan Hornburg (Racke)

unread,
Dec 9, 2021, 3:02:00 AM12/9/21
to ansible...@googlegroups.com
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-proje...@googlegroups.com <mailto:ansible-proje...@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.


OpenPGP_signature

Chris Jefferies

unread,
Dec 9, 2021, 4:07:25 AM12/9/21
to Ansible Project
Racke,
ansible_play_hosts - I did not know about this magic variable, as they call it.  I'll test but I assume that the attributes come with it.

Then you describe defining a task with:

run_once: yes
delegate_to: localhost

I see run_once in the documentation and they said it was like running only on the first host; which is fine because now I want to run using ansible_play_hosts which gets me all the hosts.

Then instead of using:
delegate_to: localhost

I think I can delegate to the server I am actually running against:
delegate_to: icinga.example.com

Or are you suggesting I delegate to localhost, then copy the resulting file?

dick,
I hope the above result explains what and how I would like to do.  But to explain a bit... I want to end up with a file that has something like this content, below.  I was trying to figure out a way to subvert the normal processing of an inventory host file and do it all into one file.  Racke has show a way of doing this by using a "magic variable" that holds all of the host info I need and how to run it only once so I don't get a bunch of repetitive files being generated.  It's beautiful...  if it works.  ;)   I'm sure it will.

object Host "myhost1.mine.me" {
  import "generic-host"
  address = "192.68.0.1"
  vars.os = "Linux"
}
object Host "myhost2.mine.me" {
  import "generic-host"
  address = "192.68.0.2"
  vars.os = "Linux"
}

Thank you.

Stefan Hornburg (Racke)

unread,
Dec 9, 2021, 4:12:39 AM12/9/21
to ansible...@googlegroups.com
On 09/12/2021 10:07, Chris Jefferies wrote:
> Racke,
> *ansible_play_hosts* - I did not know about this *magic* variable, as they call it.  I'll test but I assume that the attributes come with it.
>
> Then you describe defining a task with:
>
> run_once: yes
> delegate_to: localhost
>
> I see run_once in the documentation and they said it was like running only on the first host; which is fine because now I want to run using *ansible_play_hosts* which gets me all the hosts.
>
> Then instead of using:
> delegate_to: localhost
>
> I think I can delegate to the server I am actually running against:
> delegate_to: icinga.example.com

Chris,

yes you can use icinga.example.com if that host is part of your inventory.

Racke
> > 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> <https://groups.google.com/d/msgid/ansible-project/5b0de281-4c29-49f7-bbcf-0fe2ef957b70n%40googlegroups.com?utm_medium=email&utm_source=footer <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-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c0dbc61d-15d1-4ec5-8635-99f42b85fd3en%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/c0dbc61d-15d1-4ec5-8635-99f42b85fd3en%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature

Chris Jefferies

unread,
Dec 9, 2021, 5:36:24 AM12/9/21
to Ansible Project
Racke,
One glitch that I did not understand...  

I must add:

host: all

instead of just 


... and run with all hosts defined in the playbook.  I was only using icinga.example.com (the false name of my server).  Now I must add:

when: "{{inventory_hostname == 'icinga.example.com'}}"

to the other tasks.  Small price to pay.

Thank you.

Stefan Hornburg (Racke)

unread,
Dec 9, 2021, 5:43:14 AM12/9/21
to ansible...@googlegroups.com
On 09/12/2021 11:36, Chris Jefferies wrote:
> Racke,
> One glitch that I did not understand...
>
> I must add:
>
> host: all
>
> instead of just
>
> hosts: icinga.example.com
>
> ... and run with all hosts defined in the playbook.  I was only using icinga.example.com (the false name of my server).  Now I must add:
>
> when: "{{inventory_hostname == 'icinga.example.com'}}"
>
> to the other tasks.  Small price to pay.
>
> Thank you.

Chris, you can still use "hosts: icinga.example.com" and iterate over the all group:

{% for host in groups['all'] %}
...
{% endfor %}

BTW: the when condition is automatically evaluated by Jinja, so drop the curly braces.

Regards
Racke

>
> On Thursday, December 9, 2021 at 1:12:39 AM UTC-8 ra...@linuxia.de wrote:
>
> On 09/12/2021 10:07, Chris Jefferies wrote:
> > Racke,
> > *ansible_play_hosts* - I did not know about this *magic* variable, as they call it.  I'll test but I assume that the attributes come with it.
> >
> > Then you describe defining a task with:
> >
> > run_once: yes
> > delegate_to: localhost
> >
> > I see run_once in the documentation and they said it was like running only on the first host; which is fine because now I want to run using *ansible_play_hosts* which gets me all the hosts.
> >
> > Then instead of using:
> > delegate_to: localhost
> >
> > I think I can delegate to the server I am actually running against:
> > delegate_to: icinga.example.com <http://icinga.example.com>
>
> Chris,
>
> yes you can use icinga.example.com <http://icinga.example.com> if that host is part of your inventory.
>
> Racke
>
>
> >
> > Or are you suggesting I delegate to localhost, then copy the resulting file?
> >
> > dick,
> > I hope the above result explains what and how I would like to do.  But to explain a bit... I want to end up with a file that has something like this content, below.  I was trying to figure out a way to subvert the normal processing of an inventory host file and do it all into one file.  Racke has show a way of doing this by using a "magic variable" that holds all of the host info I need and how to run it only once so I don't get a bunch of repetitive files being generated.  It's beautiful...  if it works.  ;)   I'm sure it will.
> >
> > object Host "myhost1.mine.me <http://myhost1.mine.me>" {
> >   import "generic-host"
> >   address = "192.68.0.1"
> >   vars.os = "Linux"
> > }
> > object Host "myhost2.mine.me <http://myhost2.mine.me>" {
> > > 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> <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>> <https://groups.google.com/d/msgid/ansible-project/5b0de281-4c29-49f7-bbcf-0fe2ef957b70n%40googlegroups.com?utm_medium=email&utm_source=footer <https://groups.google.com/d/msgid/ansible-project/5b0de281-4c29-49f7-bbcf-0fe2ef957b70n%40googlegroups.com?utm_medium=email&utm_source=footer> <https://groups.google.com/d/msgid/ansible-project/5b0de281-4c29-49f7-bbcf-0fe2ef957b70n%40googlegroups.com?utm_medium=email&utm_source=footer
> <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-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c0dbc61d-15d1-4ec5-8635-99f42b85fd3en%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/c0dbc61d-15d1-4ec5-8635-99f42b85fd3en%40googlegroups.com> <https://groups.google.com/d/msgid/ansible-project/c0dbc61d-15d1-4ec5-8635-99f42b85fd3en%40googlegroups.com?utm_medium=email&utm_source=footer <https://groups.google.com/d/msgid/ansible-project/c0dbc61d-15d1-4ec5-8635-99f42b85fd3en%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-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a91c1402-047c-4c55-bf10-63e9b472bd58n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/a91c1402-047c-4c55-bf10-63e9b472bd58n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature

ch...@freeranger.com

unread,
Dec 16, 2021, 3:40:37 PM12/16/21
to Ansible Project
Racke.
-- {% for host in groups['all'] %}

Beautiful.  Thanks.

Reply all
Reply to author
Forward
0 new messages