How to retrieve facts about host B inside a playbook which run for host A

671 views
Skip to first unread message

Valentino Gagliardi

unread,
Apr 16, 2014, 10:07:35 AM4/16/14
to ansible...@googlegroups.com
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!

Valentino Gagliardi

unread,
Apr 16, 2014, 11:30:29 AM4/16/14
to ansible...@googlegroups.com
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
---
- hosts: mon
  sudo: yes

  vars_files:
   - vars.yml

  tasks:

    - name: Copy Host Definition (Server)
      template: src=templates/host_def.j2 dest=/etc/icinga/servers/{{ fqdn }}.cfg owner=root group=root mode=0644
      notify:
        - restart icinga
      tags:
       - server_host

Error:

fatal: [mon] => One or more undefined variables: 'hostB' is undefined

Tomasz Kontusz

unread,
Apr 16, 2014, 12:49:47 PM4/16/14
to ansible...@googlegroups.com


Valentino Gagliardi <server...@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.
--
Wysłane za pomocą K-9 Mail.

Valentino Gagliardi

unread,
Apr 16, 2014, 1:31:21 PM4/16/14
to ansible...@googlegroups.com
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'

Tomasz Kontusz

unread,
Apr 16, 2014, 1:48:25 PM4/16/14
to ansible...@googlegroups.com


Valentino Gagliardi <server...@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.

>
>Il giorno mercoledì 16 aprile 2014 18:49:47 UTC+2, Tomasz Kontusz ha
>scritto:
>>
>>
>>
>> Valentino Gagliardi <server...@gmail.com <javascript:>> napisał:

Brian Coca

unread,
Apr 16, 2014, 2:19:32 PM4/16/14
to ansible...@googlegroups.com
adding a 'gather play' before your main play would work

- hosts: hostB
  gather_facts: True

- hosts: hostA
  ....

Servermanaged

unread,
Apr 16, 2014, 2:58:35 PM4/16/14
to ansible...@googlegroups.com

Hello Brian
Just added a gather_facts: yes for hostB and it worked.
Thanks all.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/KqoF9a8lusc/unsubscribe.
To unsubscribe from this group and all its topics, 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/CADn%2BHsymykUdNm8yaF91qMDh28GJ25Ct9FED6E%3DoZFCPeoyrxA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Servermanaged

unread,
Apr 16, 2014, 3:49:09 PM4/16/14
to ansible...@googlegroups.com
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.



--

Paul Durivage

unread,
Apr 16, 2014, 7:45:07 PM4/16/14
to ansible...@googlegroups.com
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.


--
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.
Reply all
Reply to author
Forward
0 new messages