jinja2 template issues with hostvars
jinja2 template
ansible 2.7.2
config file = /home/feisa/ansible-linux/ansible.cfg
configured module search path = [u'/home/feisa/ansible-linux/library']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
jinja2 template
{% for host in groups['servers_production'] %}
{{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}
{% endfor %}
- name: Template Knownhosts
template: src=./ssh.j2 dest=/tmp/temp.conf
Centos 7.6
run normal ansible playblook
playbook runs succesfully
{"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'eth0'"}
Hello Freddie,
{"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'eth0'"}
How do you know what to look for in ansible_facts? Run the setup module against your managed hosts and see what the facts look like.
I have this on a node of mine:
"ansible_facts": {
...,
"ansible_default_ipv4": {
"address": "XXXX",
"alias": "eth0",
"broadcast": "XXXX",
"gateway": "XXXX",
"interface": "eth0",
"macaddress": "XXXXX",
"mtu": 1500,
"netmask": "255.255.248.0",
"network": "XXXXXXX",
"type": "ether"
},
}
I don't see any
hostvars[host]['ansible_facts']['eth0']['ipv4']['address']
Try $ ansible <hostgroup> -m setup and examine the output
to see how you should look for eth0's IP address.
Cheers,
Hugo
--
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/4473f03c-a4f0-f89c-5d92-77b2cf85ba01%40redhat.com.
For more options, visit https://groups.google.com/d/optout.
So it depends on the host and how they are named and was really a test case. The one I’m really concerned about is this one now
{% for host in groups['servers_production'] %}{{ hostvars[host]['ssh_host_key_ecdsa_public'] }}{% endfor %}
I think you mean: hostvars[host]['ansible_facts']['ssh_host_key_ecdsa_public']
This works for me and prints the key.---
- hosts: all
tasks:
- debug:
msg: "{% for host in groups['all'] %} {{
hostvars[host]['ansible_facts']['ssh_host_key_ecdsa_public'] }}
{% endfor %}"
Hugo G.
|
--
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/d10c2a5d-48dd-dd67-d85e-4cd3a29d819d%40redhat.com.
I had tried but still receive
fatal: [server]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ssh_host_key_ecdsa_public'"}
Please post the play you're using, or at least the relevant task and template.
This works for me and prints the key.---
- hosts: all
tasks:
- debug:
msg: "{% for host in groups['all'] %} {{ hostvars[host]['ansible_facts']['ssh_host_key_ecdsa_public'] }} {% endfor %}"
--
--
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/51fe343a-0562-0849-9161-4a0d04366503%40redhat.com.
Got to be something else. Are these linux hosts?
I tried this and it works:
playbook:
---
- hosts: all
tasks:
- template:
src: template.j2
dest: /tmp/knownhosts
--------------------
template:
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['ssh_host_key_ecdsa_public']
}}
{% endfor %}
----------------------
I suggest you run the setup module on your managed hosts and see the structure of the facts on your hosts then, and see if the host keys are there with that name.
Hugo G.
This is what I’m running
My role
- name: Template Knownhoststemplate: src=./ssh_key.j2 dest=/tmp/temp.conf
My template{% for host in groups['all'] %}"{{ hostvars[host]['ansible_facts']['ssh_host_key_ecdsa_public'] }}"{% endfor %}
My playbook
---- name: Centos 7 Servershosts: servers_allgather_facts: Trueignore_errors: yesroles:- linux-role
--
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/51fe343a-0562-0849-9161-4a0d04366503%40redhat.com.
For more options, visit https://groups.google.com/d/optout.
--
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/4B57DC00-7EAD-4209-97FA-C75F30038649%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8feca239-8c6b-4ef8-8331-4d26489f125c%40redhat.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8feca239-8c6b-4ef8-8331-4d26489f125c%40redhat.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8feca239-8c6b-4ef8-8331-4d26489f125c%40redhat.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/A7231E7C-CA93-44B8-BCCD-A0862C691B18%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08RcnnB79%2BTjdZXZxh%2BTHYTbxy9WmdLBHuK5tsmq7dT0sQ%40mail.gmail.com.
Hello Freddie
What version are you running? The linux host is centos7.6
Ansible is 2.5.1, managed hosts are two Centos 7.6 cloud servers
in my tests.
I tried the following:
- gather facts (automatic)
- locally render the template *on the controlling node*, once
- push the templated file to the managed hosts.
Curious if this will work for you:
template:
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['ssh_host_key_ecdsa_public']
}}
{% endfor %}
playbook:
---
- hosts: all
tasks:
- name: Render the known_hosts file locally
template:
src: template.j2
dest: /tmp/known_hosts
delegate_to: localhost
run_once: true
- name: Copy the rendered file to all the nodes
copy:
src: /tmp/known_hosts
dest: /tmp
---
Still not getting the error you're getting, except when trying to
use the variable as ansible_ssh_host_key_ecdsa_public and
that's not consistent, so I may be running into a completely
different issue.
template:
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['ssh_host_key_ecdsa_public'] }}
{% endfor %}
This template also works for me, and the keys are different. So
you might want to try it:
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_ssh_host_key_ecdsa_public'] }}
{% endfor %}
--
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/434eb480-74f4-5d68-f97c-fc2c9469b412%40redhat.com.
On Jan 4, 2019, at 11:47 AM, Hugo Gonzalez <hgon...@redhat.com> wrote:
--
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/434eb480-74f4-5d68-f97c-fc2c9469b412%40redhat.com.
On Jan 4, 2019, at 11:47 AM, Hugo Gonzalez <hgon...@redhat.com> wrote:
--
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/434eb480-74f4-5d68-f97c-fc2c9469b412%40redhat.com.
Can you try removing the "
Here's the full playbook and template I used to test (this does not even need to escalate privileges):
---
- hosts: all
tasks:
- name: Render the known_hosts file locally
template:
src: template.j2
dest: /tmp/known_hosts
delegate_to: localhost
run_once: true
- name: Copy the rendered file to all the nodes
copy:
src: /tmp/known_hosts
dest: /tmp
Template:
On Jan 7, 2019, at 12:57 PM, Brian Coca <bc...@redhat.com> wrote:
--
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/CACVha7coGzYr0OP80hc9SydkyoqYR3eygvuZuM_LP80ZzNcecA%40mail.gmail.com.