How to access an host variable defined in inventory

297 views
Skip to first unread message

Valentino Gagliardi

unread,
Jun 1, 2014, 10:28:29 AM6/1/14
to ansible...@googlegroups.com
Hello friends
maybe a noob question but I'm unable to get host variable work as expected. This is my question.

Given this template:

object Host "{{ hostvars[item]['ansible_fqdn'] }}" {
  import "generic-host"
  address = "{{ hostvars[item]['ansible_eth0']['ipv4']['address'] }}"
  vars.os = "Linux"
  vars.sla = "24x7"
  vars.http = "yes"
  vars.smtp = "yes"
}

generated by this task:

- name: Copy Host Definitions
  template: src=host_def.j2
            dest={{ icinga2_hosts_dir }}{{ hostvars[item]['ansible_fqdn'] }}.conf
            owner=root
            group=root
            mode=0644
  with_items: groups['monitored']
  notify:
   - restart icinga2

How I can access an host variable eventually defined in inventory?

[monitored:children]
myservers

[myserver]
host1 vars_http=no

What I want to do is something like this:

object Host "{{ hostvars[item]['ansible_fqdn'] }}" {
  import "generic-host"
  address = "{{ hostvars[item]['ansible_eth0']['ipv4']['address'] }}"
  vars.os = "Linux"
  vars.sla = "24x7"
{% if vars_http == "no" %}
  vars.http = "no"
{% else %}
  vars.http = "yes"
{% endif %}
  vars.smtp = "yes"
}

Thanks for support.

Michael DeHaan

unread,
Jun 1, 2014, 3:09:22 PM6/1/14
to ansible...@googlegroups.com

Since you appear to be walking over "hostvars" are you trying to have a template that includes the variables for every host in a certain group?

If so, we know how to do that.

Let us know.

 


--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ba7b9704-ed42-4054-ab2b-95595a9dcaf0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages