can't access to existing "ansible_eth1" fact

2,123 views
Skip to first unread message

Jean-Philippe Caruana

unread,
Nov 6, 2013, 11:56:33 AM11/6/13
to ansible...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I successfully managed to add a private IP address on my servers. I
can see both network interfaces eth0 and eth1 when I ask for the facts
of this server (I stuggled to death to find this command) :

$ ansible server -m setup -i production
"ansible_eth0": {
"active": true,
"device": "eth0",
"ipv4": {
"address": "x.x.x.x",
"netmask": "x.x.x.x",
"network": "x.x.x.x"
},
"ipv6": [
{
"address": "x::x:x:x:x",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "x:x:x:x:x:x",
"module": "e1000e",
"mtu": 1500,
"type": "ether"
},
"ansible_eth1": {
"active": true,
"device": "eth1",
"ipv4": {
"address": "x.x.x.x",
"netmask": "x.x.x.x",
"network": "x.x.x.x"
},
"ipv6": [
{
"address": "x::x:x:x:x",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "x:x:x:x:x:x",
"module": "e1000e",
"mtu": 9000,
"type": "ether"
},


But when I want to access the "ansible_eth1" fact from my playbook :
# dest file is /etc/hosts
# locally resolve private IPs
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_eth1']['ipv4']['address'] }} {{ host }}
{% endfor %}


I get the following error on every single server :
fatal: [server] => {'msg': "One or more undefined variables: 'dict
object' has no attribute 'ipv4'", 'failed': True}


This kind of code worked really good with eth0 ,as you can see in my
fully working iptables template extract :
{% for host in groups['all'] %}
# access from {{ host }}
- -A INPUT -p tcp -m tcp -s {{
hostvars[host]['ansible_eth0']['ipv4']['address'] }} --dport {{
some_port }} -j ACCEPT
{% endfor %}

What am I doing wrong ?
How can I debug this ? I thought "ansible server -m setup -i
production" would provide me the correct information, but I was wrong.

Thanks.
- --
Jean-Philippe Caruana
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSenS/AAoJEJ+Vqztq6gdi8o4IAJTp5BVtk5oiH14ycF3GNCZe
kl4aDRbmRwS5vXDE0Bl8/7zWILJLlrvRbwL789GtBrCquTpS7UF733+NiZaPz3GA
XAWY15lWo5FtpZ+f0VYmXbSb6ZwcQoTqP7ZhmtAR0+cJMYys5RSwFXadSkR8vX9v
D1unQhV9AwgAfNjUaJjfwdy7I8fICsgMIRzhkZjDRD4Q12+j1mnrx/EWZPk5nEX/
yVrvWTrDo2gi/Li+lVZCJB3fFaBJFi+bHV8SMduNcoRJdFlPbEY9EVNRRTqot7YQ
W458HRgZYoItCvFa+RVnyZEysTFn6iGpVMLNv9+w8DelHA70jOx9Ghb+2BpvluU=
=f8+N
-----END PGP SIGNATURE-----

Jean-Philippe Caruana

unread,
Nov 6, 2013, 12:17:16 PM11/6/13
to ansible...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

To gather more information, I decided to print the content of the
'ansible_eth1' key inside my generated hosts file... I don't know if
there is a more straightforward way to enter in debugging mode, still
I found interesting data.

I replaced
{{ hostvars[host]['ansible_eth1']['ipv4']['address'] }} {{ host }}
with
{{ hostvars[host]['ansible_eth1'] }} {{ host }}
in my template

and got this (formatted for your eyes) dict, the content of the
'ansible_eth1' key :
{
'macaddress': 'x:x:x:x:x:x',
'module': 'tg3',
'mt': 9000,
'device': 'eth1',
'ipv4': {
'netmask': 'x.x.x.x',
'network': 'x.x.x.x',
'address': 'x.x.x.x'
},
'ipv6': [
{
'scope': 'link',
'prefix': '64',
'address': 'x::x:x:x:x'
}
],
'active': True,
'type': 'ether'
}

There is a 'ipv4' key and inside this there is a 'address' key !

Informations are exactly the same with "ansible server -m setup" (cf
my previous post), so I'm satisfied.
I have as many lines in the generated file as my servers in my
inventory, so I'm satisfied again.

So why can't I access to ansible_eth1.ipv4 from my template ?

Thanks for your input.
- --
Jean-Philippe Caruana
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSenmbAAoJEJ+Vqztq6gdiIAMIAKAVxvUGxFGMe0qPDfBe4kLZ
r3sGsgRawRLF3biA+UzipxtoaXVNMf8a79bf7ICU8871yGsBl3jg4rjuJexV1Bsz
/VFBpUp8+f4JCqcwDbwPC8cNR8CxGXUC6YWgkKBNDoVfyK0ERtIsJtvReAkbzpNK
x0Uzkec4xxKOQnUhmUkiEc8AMx2FvyADkLd31ifhpzIJWBK4jXCAo04/ZEay7Beo
0C6nJzheK0g9q/fJOnEKBEYFtI+73Ro/DcVjN3idmw6v1kwdxw9pwFPcgPi517Sb
QQ64NLGz1nUH/s5X5Syk/opVuCN29EHtzpkGw2Z5DmUzU/gLv7TyhWsh0WB08Ak=
=cq3y
-----END PGP SIGNATURE-----

Jean-Philippe Caruana

unread,
Nov 8, 2013, 5:43:00 AM11/8/13
to ansible...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 06/11/2013 18:17, Jean-Philippe Caruana a �crit :
> So why can't I access to ansible_eth1.ipv4 from my template ?

It seems to be quite a tough matter, so I opened an issue for this :
https://github.com/ansible/ansible/issues/4848

- --
Jean-Philippe Caruana
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSfMAyAAoJEJ+Vqztq6gdiCcQIALc0IIkLTe92zFK+7X5TCUZ3
2vlV2qbyjwPS8oAuZmePzSc7dXbUWEWGz65nNjJLMpOVDCEcE6/urrKlyb+WLzR/
Ts8UkJmMGeLSDRk/Ob0Y0zc+Yb5buhibTllLn6TvG6kWJUIlypmm5jRdKwywiSYy
2g2kd6j6hFzwy71UqYjYgaGvI34Ecrbr/TjeMyTYTEd9fzl+L1boUDZK/TwsaE9i
/qAhg5AuSMnOIGau2f8qsF9Dfe/oknxkQkuv4MPJPIX2FztIqTb/MWNAwcCd08wo
LFd4tWnbWSm9Wk+Z7b40GtWd3aJvS3MFvGzL0lM7zgYbHiMtA2xTdp5wdyEb9s8=
=T5KG
-----END PGP SIGNATURE-----

Michael DeHaan

unread,
Nov 10, 2013, 4:32:57 PM11/10/13
to ansible...@googlegroups.com
Have you narrowed this down by using --limit to exactly one host and made sure there is an ipv4 address for that host?

I'm guessing one host might not have an ipv4 address for that interface.




On Fri, Nov 8, 2013 at 5:43 AM, Jean-Philippe Caruana <j...@target2sell.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 06/11/2013 18:17, Jean-Philippe Caruana a écrit :
> So why can't I access to ansible_eth1.ipv4 from my template ?

It seems to be quite a tough matter, so I opened an issue for this :
https://github.com/ansible/ansible/issues/4848

- --
Jean-Philippe Caruana
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSfMAyAAoJEJ+Vqztq6gdiCcQIALc0IIkLTe92zFK+7X5TCUZ3
2vlV2qbyjwPS8oAuZmePzSc7dXbUWEWGz65nNjJLMpOVDCEcE6/urrKlyb+WLzR/
Ts8UkJmMGeLSDRk/Ob0Y0zc+Yb5buhibTllLn6TvG6kWJUIlypmm5jRdKwywiSYy
2g2kd6j6hFzwy71UqYjYgaGvI34Ecrbr/TjeMyTYTEd9fzl+L1boUDZK/TwsaE9i
/qAhg5AuSMnOIGau2f8qsF9Dfe/oknxkQkuv4MPJPIX2FztIqTb/MWNAwcCd08wo
LFd4tWnbWSm9Wk+Z7b40GtWd3aJvS3MFvGzL0lM7zgYbHiMtA2xTdp5wdyEb9s8=
=T5KG
-----END PGP SIGNATURE-----

--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Jean-Philippe Caruana

unread,
Nov 12, 2013, 4:26:04 AM11/12/13
to ansible...@googlegroups.com
Hi,

Le 10/11/2013 22:32, Michael DeHaan a �crit :
> Have you narrowed this down by using --limit to exactly one host and
> made sure there is an ipv4 address for that host?
>
> I'm guessing one host might not have an ipv4 address for that interface.

I tested on a single server, I have the fact ansible_eth1 with
ipv4.address and then I had the error when executing the playbook (see
my first email).
I used "ansible myserver.com -m setup -i inventory". Is it the same
effect as --limit ?

As seen in my debug test (printing {{ hostvars[host]['ansible_eth1'] }}
on every host) gave me the following dict for every server (I masually
checked, since I just have 8 servers for now) :

{
'macaddress': 'x:x:x:x:x:x',
'module': 'tg3',
'mt': 9000,
'device': 'eth1',
'ipv4': {
'netmask': 'x.x.x.x',
'network': 'x.x.x.x',
'address': 'x.x.x.x' # <- here :)
},
'ipv6': [
{
'scope': 'link',
'prefix': '64',
'address': 'x::x:x:x:x'
}
],
'active': True,
'type': 'ether'
}

When I ask for facts for one server, there is an ipv4 address on eth1.
I checked, and all my servers do have an ipv4 address on eth1, either
with ifconfig or with ansible facts.

--
Jean-Philippe Caruana
Message has been deleted

senorsmile

unread,
Feb 4, 2014, 6:47:20 PM2/4/14
to ansible...@googlegroups.com
I am getting the exact same issue as OP.  I cloned the official ansible-examples and am trying to deploy the hadoop example.  I get part the way through and then get the error:

TASK: [common | Create the hosts file for all machines] ***********************
fatal: [hadoop1] => {'msg': "One or more undefined variables: 'dict object' has no attribute u'ansible_eth0'", 'failed': True}
fatal: [hadoop1] => {'msg': "One or more undefined variables: 'dict object' has no attribute u'ansible_eth0'", 'failed': True}
fatal: [hadoop2] => {'msg': "One or more undefined variables: 'dict object' has no attribute u'ansible_eth0'", 'failed': True}
fatal: [hadoop2] => {'msg': "One or more undefined variables: 'dict object' has no attribute u'ansible_eth0'", 'failed': True}
fatal: [hadoop3] => {'msg': "One or more undefined variables: 'dict object' has no attribute u'ansible_eth0'", 'failed': True}
fatal: [hadoop3] => {'msg': "One or more undefined variables: 'dict object' has no attribute u'ansible_eth0'", 'failed': True}

When I do ansible -m setup all, I can see that ansible_eth0 exists and has a dict of data, including the address from hosts.j2.  Any clue what is happening there?

For reference, here is my modified hosts file (as maybe there's something wrong here?)

hadoop_master_primary ansible_ssh_host=centos6-hadoop01
hadoop_master_secondary ansible_ssh_host=centos6-hadoop02
hadoop1  ansible_ssh_host=centos6-hadoop03
hadoop2  ansible_ssh_host=centos6-hadoop04
hadoop3  ansible_ssh_host=centos6-hadoop05


[hadoop_all:children]
hadoop_masters
hadoop_slaves
qjournal_servers
zookeeper_servers

[hadoop_all:vars]
ansible_ssh_user=root

[hadoop_master_primary]
hadoop1

[hadoop_master_secondary]
hadoop2

[hadoop_masters:children]
hadoop_master_primary
hadoop_master_secondary

[hadoop_slaves]
hadoop1
hadoop2
hadoop3

[qjournal_servers]
hadoop1
hadoop2
hadoop3

[zookeeper_servers]
hadoop1 zoo_id=1
hadoop2 zoo_id=2
hadoop3 zoo_id=3




On Tuesday, November 12, 2013 1:26:04 AM UTC-8, Jean Philippe Caruana wrote:
Hi,

Le 10/11/2013 22:32, Michael DeHaan a �crit :

Jean-Philippe Caruana

unread,
Feb 5, 2014, 8:30:16 AM2/5/14
to ansible...@googlegroups.com
Le 05/02/2014 00:47, senorsmile a écrit :
> I am getting the exact same issue as OP

I am feeling less alone now...

I haven't tested it since newer releases of ansible went out : what is
your ansible version ?

--
Jean-Philippe Caruana
http://www.barreverte.fr

Jean Philippe Caruana

unread,
Nov 21, 2014, 12:15:51 PM11/21/14
to ansible...@googlegroups.com
Hi,

I am resurrecting this old message (of mine) : the issue https://github.com/ansible/ansible/issues/4848 has been closed as "you can't do jinja2", but I think this is an issue in gathering ansible_eth1 fact


any idea ?

Thanks

Brian Coca

unread,
Nov 21, 2014, 12:20:46 PM11/21/14
to ansible...@googlegroups.com
ansible server -m setup -i production, should provide you the correct info.

I suspect one of your hosts does not have an ipv4 address assigned to eth1.


--
Brian Coca

Jean-Philippe Caruana

unread,
Nov 24, 2014, 5:39:03 AM11/24/14
to ansible...@googlegroups.com
Thanks, this is helpful.
As a matter of fact, one of my servers don't have an ipv4 address assign to eth1, but does have an ansible_eth1 fact:

        "ansible_eth1": {
            "active": false,
            "device": "eth1",
            <snip>
        },

I think I can use the active flag to prevent my template from crashing for this server:


{% for host in groups['all'] %}
{% if hostvars[host]['ansible_eth1']['active'] %}

{{ hostvars[host]['ansible_eth1']['ipv4']['address'] }} {{ host }}
{% endif %}
{% endfor %}
-- 
Jean-Philippe Caruana 
http://www.barreverte.fr
Reply all
Reply to author
Forward
0 new messages