Hi Kai,
I tried that, but it didn't work. Thanks for the reply all the same!
Here's the section of the playbook. Let me know if you can see the issue,
or if I can post anything else that would help.
- hosts: nagios-server
become: yes
tasks:
....
- name: Generate Configs
template:
src=/root/ansible/ansible-files/nagios/nagios-monitored-server.cfg.j2
dest=/usr/local/nagios/etc/servers/{{ item }}.cfg owner=root mode=0644
with_items: "{{groups['nagios-monitored']}}"
.....
This is the error the playbook spits out. It throws it for all the
servers, not just the FreePBX one.
failed: [nagios.domain.com] (item=freepbx.domain.com) => {"failed": true,
"item": "freepbx.domain.com", "msg": "AnsibleUndefinedVariable: 'dict object'
has no attribute 'ansible_os_family'"}
Thanks again!
On Saturday, 17 February 2018 13:22:12 UTC-8, Kai Stian Olstad wrote:
>
> On Saturday, 17 February 2018 21.44.06 CET Brigzzy Briggs wrote:
> > I can see that
> >
> >
> > "ansible_os_family": "Sangoma"
> >
> > In my template, I have the following if statement:
> >
> >
> > {% if ansible_os_family == "Debian" or ansible_os_family == "Ubuntu" %}
> > define service{
> > use local-service
> > host_name {{
> hostvars[item].inventory_hostname
> > }}
> > service_description Apt Status
> > check_command check_apt
> > }
> > {% endif %}
> >
> > and the issue is that the block is still being generated for that
> server,
> > even though it's not Debian or Ubuntu.
> >
> >
> > I posted this on Reddit as well
> > <
> https://www.reddit.com/r/ansible/comments/7y5uje/troubles_with_an_if_statement_in_a_jinja2_template/?st=jdrthmo2&sh=38998e5f>,
>
>
> > and I realized that the issue is that the template is being applied to
> my
> > Nagios server, which is Debian, so the ansible_os_family is always
> coming
> > back Debian. What I'm having trouble with is how to get the if
> statement
> > to get it's facts from the target server, and not the one the playbook
> is
> > running on. I'm able to get the hostname from the static hosts file
> like
> > this
> >
> >
> > {{ hostvars[item].inventory_hostname }}
> >
> > but I'm not having any luck figuring out how to get the template to
> apply
> > to the values from that host. Anyone have any ideas?
>
> Without the more of the code on where and how you are generating the
> template it's difficult to say, but I believe you are looking for this
>
> {% if hostvars[item].ansible_os_family == "Debian" or
> hostvars[item].ansible_os_family == "Ubuntu" %}
>
> or the more cleaner version
>
> {% if hostvars[item].ansible_os_family in ['Debian', 'Ubuntu'] %}
>
> --
> Kai Stian Olstad
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/acfbf84b-8cbc-43b4-9161-e011a336a2e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.