Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
08ce3692
Commit
08ce3692
authored
Aug 11, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'network-facts' of
https://github.com/akhayyat/ansible
into devel
parents
301edb5b
f0a8e136
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
85 deletions
+81
-85
library/setup
+81
-85
No files found.
library/setup
View file @
08ce3692
...
@@ -426,6 +426,8 @@ class LinuxNetwork(Network):
...
@@ -426,6 +426,8 @@ class LinuxNetwork(Network):
This is a Linux-specific subclass of Network. It defines
This is a Linux-specific subclass of Network. It defines
- interfaces (a list of interface names)
- interfaces (a list of interface names)
- interface_<name> dictionary of ipv4, ipv6, and mac address information.
- interface_<name> dictionary of ipv4, ipv6, and mac address information.
- all_ipv4_addresses and all_ipv6_addresses: lists of all configured addresses.
- ipv4_address and ipv6_address: the first non-local address for each family.
"""
"""
platform
=
'Linux'
platform
=
'Linux'
...
@@ -433,93 +435,87 @@ class LinuxNetwork(Network):
...
@@ -433,93 +435,87 @@ class LinuxNetwork(Network):
Network
.
__init__
(
self
)
Network
.
__init__
(
self
)
def
populate
(
self
):
def
populate
(
self
):
self
.
facts
[
'interfaces'
]
=
self
.
get_interfaces
()
interfaces
,
ips
=
self
.
parse_ip_addr
()
self
.
get_interface_facts
()
self
.
get_ipv4_facts
()
self
.
facts
[
'interfaces'
]
=
interfaces
.
keys
()
self
.
get_ipv6_facts
()
for
iface
in
interfaces
:
self
.
facts
[
iface
]
=
interfaces
[
iface
]
for
key
in
ips
:
self
.
facts
[
key
]
=
ips
[
key
]
return
self
.
facts
return
self
.
facts
# get list of interfaces
def
parse_ip_addr
(
self
):
def
get_interfaces
(
self
):
interfaces
=
{}
names
=
[]
ips
=
{
'all_ipv4_addresses'
:
[],
'all_ipv6_addresses'
:
[],
data
=
get_file_content
(
'/proc/net/dev'
)
'ipv4_address'
:
None
,
'ipv6_address'
:
None
}
# Format of /proc/net/dev is:
# Inter-| Receive ...
output
=
subprocess
.
Popen
([
'ip'
,
'addr'
],
stdout
=
subprocess
.
PIPE
)
.
communicate
()[
0
]
# face |bytes ...
for
line
in
output
.
split
(
'
\n
'
):
# lo: 595059
if
line
:
for
line
in
data
.
split
(
'
\n
'
):
words
=
line
.
split
()
if
':'
in
line
:
if
not
line
.
startswith
(
' '
):
names
.
append
(
line
.
split
(
':'
)[
0
]
.
strip
())
device
=
words
[
1
][
0
:
-
1
]
return
names
mtu
=
words
[
4
]
elif
words
[
0
]
.
startswith
(
'link/'
):
def
get_iface_hwaddr
(
self
,
iface
):
iface_type
=
words
[
0
]
.
split
(
'/'
)[
1
]
data
=
get_file_content
(
'/sys/class/net/
%
s/address'
%
iface
)
if
iface_type
==
'void'
:
if
data
is
None
:
macaddress
=
'unknown'
return
'unknown'
else
:
else
:
macaddress
=
words
[
1
]
return
data
.
strip
()
elif
words
[
0
]
==
'inet'
:
address
,
netmask_length
=
words
[
1
]
.
split
(
'/'
)
def
get_interface_facts
(
self
):
address_bin
=
struct
.
unpack
(
'!L'
,
socket
.
inet_aton
(
address
))[
0
]
for
iface
in
self
.
facts
[
'interfaces'
]:
if
iface
not
in
self
.
facts
:
netmask_bin
=
(
1
<<
32
)
-
(
1
<<
32
>>
int
(
netmask_length
))
self
.
facts
[
iface
]
=
{}
netmask
=
socket
.
inet_ntoa
(
struct
.
pack
(
'!L'
,
netmask_bin
))
self
.
facts
[
iface
]
=
{
'macaddress'
:
self
.
get_iface_hwaddr
(
iface
)
}
if
os
.
path
.
exists
(
'/sys/class/net/
%
s/mtu'
%
iface
):
network
=
socket
.
inet_ntoa
(
struct
.
pack
(
'!L'
,
address_bin
&
netmask_bin
))
mtu
=
get_file_content
(
'/sys/class/net/
%
s/mtu'
%
iface
)
self
.
facts
[
iface
][
'mtu'
]
=
mtu
.
strip
()
iface
=
words
[
-
1
]
# If an interface has multiple IPv4 addresses, make up an
def
get_ipv4_facts
(
self
):
# interface name for each address
for
iface
in
self
.
facts
[
'interfaces'
]:
if
iface
in
interfaces
:
# This is lame, but there doesn't appear to be a good way
i
=
0
# to get all addresses for both IPv4 and IPv6.
while
'{0}:{1}'
.
format
(
iface
,
i
)
in
interfaces
:
cmd
=
subprocess
.
Popen
(
"env LANG=
\"\"
/sbin/ifconfig
%
s"
%
iface
,
shell
=
True
,
i
+=
1
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
iface
=
'{0}:{1}'
.
format
(
iface
,
i
)
out
,
err
=
cmd
.
communicate
()
for
line
in
out
.
split
(
'
\n
'
):
interfaces
[
iface
]
=
{}
is_ipv4
=
False
interfaces
[
iface
][
'macaddress'
]
=
macaddress
data
=
line
.
split
()
interfaces
[
iface
][
'mtu'
]
=
mtu
if
'inet addr'
in
line
:
interfaces
[
iface
][
'device'
]
=
device
if
'ipv4'
not
in
self
.
facts
[
iface
]:
interfaces
[
iface
][
'ipv4'
]
=
{
'address'
:
address
,
self
.
facts
[
iface
][
'ipv4'
]
=
{}
'netmask'
:
netmask
,
is_ipv4
=
True
'network'
:
network
}
self
.
facts
[
iface
][
'ipv4'
]
=
{
'address'
:
data
[
1
]
.
split
(
':'
)[
1
],
'netmask'
:
data
[
-
1
]
.
split
(
':'
)[
1
]
}
ips
[
'all_ipv4_addresses'
]
.
append
(
address
)
# Slightly different output in net-tools-1.60-134.20120127git
if
not
ips
[
'ipv4_address'
]
or
ips
[
'ipv4_address'
]
.
startswith
(
'127'
):
# Looks like
ips
[
'ipv4_address'
]
=
address
# inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
elif
'inet '
in
line
:
elif
words
[
0
]
==
'inet6'
:
is_ipv4
=
True
address
,
prefix
=
words
[
1
]
.
split
(
'/'
)
if
'ipv4'
not
in
self
.
facts
[
iface
]:
scope
=
words
[
3
]
self
.
facts
[
iface
][
'ipv4'
]
=
{}
self
.
facts
[
iface
][
'ipv4'
]
=
{
'address'
:
data
[
1
],
iface
=
device
'netmask'
:
data
[
3
]
}
if
iface
not
in
interfaces
:
if
is_ipv4
:
interfaces
[
iface
]
=
{}
ip
=
struct
.
unpack
(
"!L"
,
socket
.
inet_aton
(
self
.
facts
[
iface
][
'ipv4'
][
'address'
]))[
0
]
interfaces
[
iface
][
'macaddress'
]
=
macaddress
mask
=
struct
.
unpack
(
"!L"
,
socket
.
inet_aton
(
self
.
facts
[
iface
][
'ipv4'
][
'netmask'
]))[
0
]
interfaces
[
iface
][
'mtu'
]
=
mtu
self
.
facts
[
iface
][
'ipv4'
][
'network'
]
=
socket
.
inet_ntoa
(
struct
.
pack
(
"!L"
,
ip
&
mask
))
interfaces
[
iface
][
'device'
]
=
device
if
'ipv6'
not
in
interfaces
[
iface
]:
def
get_ipv6_facts
(
self
):
interfaces
[
iface
][
'ipv6'
]
=
[]
if
not
socket
.
has_ipv6
:
interfaces
[
iface
][
'ipv6'
]
.
append
(
{
return
'address'
:
address
,
data
=
get_file_content
(
'/proc/net/if_inet6'
)
'prefix'
:
prefix
,
if
data
is
None
:
'scope'
:
scope
}
)
return
for
line
in
data
.
split
(
'
\n
'
):
ips
[
'all_ipv6_addresses'
]
.
append
(
address
)
l
=
line
.
split
()
if
not
ips
[
'ipv6_address'
]
or
ips
[
'ipv6_address'
]
==
'::1'
:
iface
=
l
[
5
]
ips
[
'ipv6_address'
]
=
address
if
'ipv6'
not
in
self
.
facts
[
iface
]:
self
.
facts
[
iface
][
'ipv6'
]
=
[]
return
interfaces
,
ips
scope
=
l
[
3
]
if
Network
.
IPV6_SCOPE
.
has_key
(
l
[
3
]):
scope
=
Network
.
IPV6_SCOPE
[
l
[
3
]]
prefix
=
int
(
l
[
2
],
16
)
str_addr
=
':'
.
join
(
[
l
[
0
][
i
:
i
+
4
]
for
i
in
range
(
0
,
len
(
l
[
0
]),
4
)
]
)
# Normalize ipv6 address from format in /proc/net/if_inet6
addr
=
socket
.
inet_ntop
(
socket
.
AF_INET6
,
socket
.
inet_pton
(
socket
.
AF_INET6
,
str_addr
))
self
.
facts
[
iface
][
'ipv6'
]
.
append
(
{
'address'
:
addr
,
'prefix'
:
prefix
,
'scope'
:
scope
}
)
class
Virtual
(
Facts
):
class
Virtual
(
Facts
):
"""
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment