Hello friends!
Almost sure that this is my fault but at this time is still obscure for me how to retrieve facts about host B inside a playbook which run for host A.
This is the real case: I want to populate Icinga host definition running the playbook on host A, but retrieving facts about monitored host B:
Already tried http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts (templates/host_def.j2) :
define host{
use generic-host
host {{ hostvars[‘hostB’][‘ansible_fqdn’] }}
address {{ hostvars[‘hostB’][‘ansible_eth0.ipv4.address’] }}
max_check_attempts 5
BLABLA
}
but this give me an error. Is this the right way to use magic variables?
Any pointing? Thanks!
More details.
vars.yml
fqdn: “{{ hostvars[hostB][‘ansible_fqdn’] }}”
address: “{{ hostvars[hostB][‘ansible_eth0.ipv4.address’] }}”
---- separator -----
templates/host_def.j2
define host{
use generic-host
host_name {{ fqdn }}
address {{ address }}
max_check_attempts 5
check_command check-host-alive
}
---- separator -----
task.yml
Valentino Gagliardi <servermanaged@gmail.com> napisał:
More details.
vars.yml
fqdn: "{{ hostvars[hostB]['ansible_fqdn'] }}"
address: "{{ hostvars[hostB]['ansible_eth0.ipv4.address'] }}"
If hostB is the inventory name of that host (and not a variable) use 'hostB' in quotes.
hostB is defined in inventory. Unfortunately does not single quote nor strong quoting solve the issue:
fqdn: “{{ hostvars[‘hostB’][‘ansible_fqdn’] }}”
address: “{{ hostvars[‘hostB’][‘ansible_eth0’][‘ipv4’][‘address’] }}”
Error:
fatal: [mon] => One or more undefined variables: ‘dict object’ has no attribute ‘ansible_fqdn’
Valentino Gagliardi <servermanaged@gmail.com> napisał:
hostB is defined in inventory. Unfortunately does not single quote nor
strong quoting solve the issue:
fqdn: "{{ hostvars['hostB']['ansible_fqdn'] }}"
address: "{{ hostvars['hostB']['ansible_eth0']['ipv4']['address'] }}"
Error:
fatal: [mon] => One or more undefined variables: 'dict object' has no
attribute 'ansible_fqdn'
That probably means you have not yet gathered facts from that host.
I'm not sure how running action: setup with delegate_to would act, but if that doesn't work you can put a dummy play at the beginning of your playbook.
adding a ‘gather play’ before your main play would work
Hello Brian
Just added a gather_facts: yes for hostB and it worked.
Thanks all.
I’m sorry but there’s another question. This time I want to access {{ hostvars[host][‘ansible_fqdn’] }} within a task:
- name: Copy Host Definition (Server)
template: src=templates/host_def.j2 dest=/etc/icinga/servers/{{ hostvars[host][‘ansible_fqdn’] }}.cfg owner=root group=root mode=0644
notify:
- restart icinga
tags:
- server_host
This task don’t work. There’s a way to access ansible_fqdn?
Regards.
Well, what is the value of “host”?
It’s a variable – is it defined? Does it have a sane value?
If you’re looking for the value of the “current” host, you can reference ‘ansible_fqdn’ directly: {{ ansible_fqdn }}. Otherwise, make sure that host is defined and holds the value it’s supposed to with the debug module.