Hi. We would like a way to email all the facts from a host. What’s the easiest way to achieve this?
I have tried:
ansible host.domain -m mail -a ‘to=b@t-0.be from=root@localhost body=${ansible_facts} subject=welcome’
and also a playbook with a $hosts variable, but I just get emails like: ${ansible_facts}
Thanks,
Devin
${ansible_facts} isn't an actual variable in ansible
However, you have lots of options.
One easy way of getting all of the variables is:
${hostvars.{$ansible_hostname}}
(The second set of {} are for escaping any "."'s that may occur in an
IP address)
Or if you want a specific host named 'example', for example:
${hostvars.example}
You will need to have already talked to the host in a previous play to
get the facts, otherwise you just get assigned variables.
Hi Michael -
that works if I use $ansible_fqdn. Thanks! But it only seems to work in a playbook - so is this the right way to do it:
Devin Bayer wrote:
${ansible_facts} isn't an actual variable in ansible
However, you have lots of options.
One easy way of getting all of the variables is:
${hostvars.{$ansible_hostname}}
Hi Michael -
that works if I use $ansible_fqdn. Thanks! But it only seems to work in a
playbook - so is this the right way to do it:
Facts in general only work inside of a playbook.
---
- hosts: ${hosts}
tasks:
- mail: >
to=b@t-0.be
from=root@${ansible_fqdn}
body="${hostvars.{$ansible_fqdn}}"
subject=welcome
Also, any idea about how to improve the output formatting? Right now it's
just dict.__str__ all on one line.
The mail module is basically why $TEMPLATE exists. You can have
body="$TEMPLATE(templates/file.j2)" and then use Jinja2 to format hostvars
to your liking.
Daniel
Hi Daniel - thanks for the help - I'm really having trouble finding this stuff in the documentation.
Facts in general only work inside of a playbook.
OK.
The mail module is basically why $TEMPLATE exists. You can have
body="$TEMPLATE(templates/file.j2)" and then use Jinja2 to format hostvars
to your liking.
Should this work:
{{ hostvars[ansible_fqdn] }}
because I get this exception:
jinja2.exceptions.UndefinedError: 'hostvars' is undefined
~ Devin
Hi Daniel - thanks for the help - I'm really having trouble finding this stuff in the documentation.
Probably because we don't have a "how to email facts" topic.
Seriously though, tell me what you'd like to see, and we'll work on it
Hi Daniel - thanks for the help - I'm really having trouble finding this stuff in the documentation.
Probably because we don't have a "how to email facts" topic.
I can imagine.
Seriously though, tell me what you'd like to see, and we'll work on it
Stuff like $TEMPLATE, so maybe a plugins page?
And the available variables on the command line and in playbooks. So maybe a variables page?
Cheers,
Devin
Hi Daniel - thanks for the help - I'm really having trouble finding this stuff in the documentation.
Probably because we don't have a "how to email facts" topic.
I can imagine.
Seriously though, tell me what you'd like to see, and we'll work on it
Stuff like $TEMPLATE, so maybe a plugins page?
Noted. I don't even use that one myself, but yes, we should cover more.
This is mostly covered here:
http://ansible.cc/docs/playbooks2.html#lookup-plugins-accessing-outside-data
But we should list individual ones.
And the available variables on the command line and in playbooks. So maybe a variables page?
Most of those are already there, though dispersed.
As far as CLI options, we've mostly shown those through examples in
the relevant sections.
Variables:
http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts
Noted. I don't even use that one myself, but yes, we should cover more.
This is mostly covered here:
http://ansible.cc/docs/playbooks2.html#lookup-plugins-accessing-outside-data
Yes, I see it now. The documentation is fairly complete - when first reading it I was impressed. But I find it hard to reference, since as you said everything is spread around.
Variables:
http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts
Maybe it got lost in the thread, but the jinja2 example doesn't seem to work, even using it exactly from trunk:
{{ hostvars['my.host.fqdn']['ansible_distribution'] }}
results in:
jinja2.exceptions.UndefinedError: 'hostvars' is undefined
Thanks,
Devin
That's a typo! Good catch, missing an underscore.
Try this:
{{ host_vars['my.host.fqdn']['ansible_distribution'] }}
Oh, that looked hopeful, but I'm afraid:
jinja2.exceptions.UndefinedError: 'host_vars' is undefined
~ devin
In a template?
The template plugin $TEMPLATE() macro may not have access to it. It's
new and rarely used. Probably fixable. Let me know if that's the
case.
That's a typo! Good catch, missing an underscore.
Try this:
{{ host_vars['my.host.fqdn']['ansible_distribution'] }}
Oh, that looked hopeful, but I'm afraid:
jinja2.exceptions.UndefinedError: 'host_vars' is undefined
In a template?
To be clear, I mean, the template action, there, it definitely works.
Do you mean like this:
TASK: [template src=templates/welcome.j2 dest=/tmp/abc] *********************
fatal: [banks....] => {'msg': "'host_vars' is undefined", 'failed': True}
I actuall do this by:
ansible -m setup <hostname> |mail me@mydomain.com
Show me your welcome.j2 please.
Devin Bayer wrote:
Noted. I don't even use that one myself, but yes, we should cover
more.
This is mostly covered here:
http://ansible.cc/docs/playbooks2.html#lookup-plugins-accessing-outside-data
Yes, I see it now. The documentation is fairly complete - when first
reading it I was impressed. But I find it hard to reference, since as you
said everything is spread around.
Variables:
http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts
Maybe it got lost in the thread, but the jinja2 example doesn't seem to
work, even using it exactly from trunk:
{{ hostvars['my.host.fqdn']['ansible_distribution'] }}
results in:
jinja2.exceptions.UndefinedError: 'hostvars' is undefined
What version of Jinja2? What version of ansible? What OS?
Daniel
jpmens1
February 27, 2013, 7:36am
18
There seems to be a problem with indexing `hostvars' in $TEMPLATE().
I've documented a possible solution (and the traceback when indexing
hostvars) at [1]
-JP
[1] https://gist.github.com/jpmens/5045984
jpmens1
February 27, 2013, 8:23am
19
There seems to be a problem with indexing `hostvars' in $TEMPLATE().
[1] https://gist.github.com/jpmens/5045984
For the record: Daniel says I'm missing a "name:" on the action. Adding
that indeed fixes the problem, but I think that's a bug, as 'name:' is
supposed to be optional. (Gist updated with a comment.)
-JP
Jan-Piet Mens wrote:
There seems to be a problem with indexing `hostvars' in $TEMPLATE().
[1] https://gist.github.com/jpmens/5045984
For the record: Daniel says I'm missing a "name:" on the action. Adding
that indeed fixes the problem, but I think that's a bug, as 'name:' is
supposed to be optional. (Gist updated with a comment.)
This bug is now fixed on devel.
Daniel