无法使用boto创建示例VPC。 socket.gaierror:[Errno -2]名称或服务未知

时间:2015-11-04 11:26:34

标签: python sockets ansible boto amazon-vpc

我是初学者,正在尝试使用boto创建一个VPC。

 [root@localhost orchestration]# cat create-vpc.py
#!/usr/bin/python

import boto.vpc
import pdb


from boto.vpc import VPCConnection

#c = VPCConnection()

c=boto.vpc.connect_to_region('us-east-1')

vpc = c.create_vpc('10.0.12.0/16')
subnet = c.create_subnet(vpc.id, '10.0.12.1/24')

这是我创建VPC的示例脚本。 但是我收到错误“gaierror:[Errno -2]名称或服务未知”

这是完整的追溯。

Traceback (most recent call last):
  File "create-vpc.py", line 13, in <module>
    vpc = c.create_vpc('10.0.12.0/16')
  File "/usr/lib/python2.7/site-packages/boto/vpc/__init__.py", line 135, in create_vpc
    return self.get_object('CreateVpc', params, VPC)
  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 1192, in get_object
    response = self.make_request(action, params, path, verb)
  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 1116, in make_request
    return self._mexe(http_request)
  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 913, in _mexe
    self.is_secure)
  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 705, in get_http_connection
    return self.new_http_connection(host, port, is_secure)
  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 747, in new_http_connection
    connection = self.proxy_ssl(host, is_secure and 443 or 80)
  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 792, in proxy_ssl
    int(self.proxy_port)), timeout)
  File "/usr/lib64/python2.7/socket.py", line 559, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

我还使用访问权限和密钥配置了〜/ .boto文件。 仅供参考,

[root@localhost orchestration]# cat ~/.boto
[Credentials]

aws_access_key_id=<my-access-key>
aws_secret_access_key=<my-secret-key>

我还经历了一些其他类似的问题,建议在/ etc / hosts文件中添加yun.local条目。

这是我的/ etc / hosts文件和/etc/resolv.conf文件。

[root@localhost orchestration]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.240.1 yun.local

[root@localhost orchestration]# cat /etc/resolv.conf
# Generated by NetworkManager
search comviva.com
nameserver 172.19.1.10
nameserver 172.19.1.11
nameserver 172.16.1.20
# NOTE: the libc resolver may not support more than 3 nameservers.
# The nameservers listed below may not be recognized.
nameserver 172.16.1.22

也是telnet的o / p

[root@localhost orchestration]# telnet yun.local 80
Trying 192.168.240.1...
telnet: connect to address 192.168.240.1: Connection refused

FYI,

[root@localhost orchestration]# nslookup ec2.us-east-1.amazonaws.com
Server:         172.19.1.11
Address:        172.19.1.11#53

Name:   ec2.us-east-1.amazonaws.com
Address: 54.239.26.182

[root@localhost orchestration]#
[root@localhost orchestration]#
[root@localhost orchestration]# dig ec2.us-east-1.amazonaws.com

; <<>> DiG 9.9.4-RedHat-9.9.4-18.el7 <<>> ec2.us-east-1.amazonaws.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24619
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;ec2.us-east-1.amazonaws.com.   IN      A

;; ANSWER SECTION:
ec2.us-east-1.amazonaws.com. 0  IN      CNAME   ec2.amazonaws.com.
ec2.amazonaws.com.      48      IN      A       54.239.20.1

;; Query time: 153 msec
;; SERVER: 172.19.1.10#53(172.19.1.10)
;; WHEN: Tue Nov 17 23:32:12 EST 2015
;; MSG SIZE  rcvd: 90

我还尝试使用Ansible创建此VPC,并且正如预期的那样也会出现相同的错误。 我如何克服这种恶搞?任何答案都应该感激不尽。

1 个答案:

答案 0 :(得分:0)

要在boto中创建VPCConnection(或任何服务连接对象),您应该使用此咒语:

import boto.vpc
c = boto.vpc.connect_to_region('us-east-1')  # or whatever

通过这种方式,您可以保证正确初始化的VPCConnection对象指向正确的端点。