【问题标题】:Ansible VM:TypeError: __init__() takes at least 3 arguments (2 given)Ansible VM:TypeError: __init__() 至少需要 3 个参数(给定 2 个)
【发布时间】:2016-10-03 15:00:54
【问题描述】:

我正在学习虚拟机和其他人,我正在尝试配置一个需要 file.py 的 VM(使用 Ansible 和 DigitalOcean API V2)来完全正确配置这台机器(根据我的书正在学习),但是当我尝试使用命令 python do_api_v1.py 时,输出显示:

**Traceback(最近一次调用最后一次):

文件“do_api_v1.py”,第 12 行,在 do = DoManager(token) TypeError: init() 至少需要 3 个参数(给定 2 个) **

文件do_api_v1.py是这样的:

"""
dependencias: 
    sudo pip install dopy pyopenssl ndg-httpsclient pyasn1
"""

import os
from dopy.manager import DoManager
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()


api_version = os.getenv("DO_API_VERSION")
api_token=os.getenv("DO_API_

do = DoManager(None, 'api_token', 'api_version')

keys = do.all_ssh_keys()
print "ssh key nametid"
for key in keys:
    print "%s\t%d" % (key["name"], key["id"])

print "Image name\tid"
imgs = do.all_images()
for img in imgs:
    if img["slug"] == "ubuntu-14-04-x64":
        print "%s\t%d" % (img["name"], img["id"])

print "Region name\tid"
regions = do.all_regions()
for region in regions:
    if region["slug"] == "nyc2":
        print "%s\t%d" % (region["slug"], region["id"])

print "Size name\tid"
sizes = do.sizes()
for size in sizes:
    if size["slug"] == "512mb":
        print "%s\t%d" % (size["slug"], size["id"])

【问题讨论】:

  • 你没有给构造函数足够的参数。有什么问题?
  • 我认为您还需要将 api 密钥传递给 DoManager,而不仅仅是令牌。

标签: python virtual-machine ansible


【解决方案1】:

如错误消息所述,您没有向DoManager 传递足够的参数。您需要传递一个强制性的 client_id 作为第一个参数,并传递一个 api_key 作为第二个参数。

【讨论】:

  • 是的,这些书告诉我使用 client_id 和 api_key 作为 DoManager 的参数,但如果我这样做,输出会返回:dopy.manager.DoError: API v1 has reached end-of -生活。请使用 API v2。
【解决方案2】:

DoManager 需要更多参数。

来自docs

对于 API V1:

# export DO_CLIENT_ID='client_id'
# export DO_API_KEY='long_api_key'
>>> from dopy.manager import DoManager
>>> do = DoManager('client_id', 'long_api_key')

对于 V2:

# export DO_API_VERSION='2'
# export DO_API_TOKEN='api_token'
>>> from dopy.manager import DoManager
>>> do = DoManager(None, 'api_token', api_version=2)

两个版本都要求您提供 API 密钥,但 V2 不再需要客户端 ID。

【讨论】:

  • @DouglasDiasdaSilva 你没有发布它返回的内容
  • 伙计们,看,我正在使用 DigitalOcean API V2 看看这个链接,请:digitalocean.com/community/tutorials/… _________________________________________________________ DigitalOcean API V2 不再需要 client_key 和 api_key...
  • 我不知道该说什么。文档说它需要更多的构造函数参数,这解释了为什么你会收到错误。我很想说“操作指南”中有错误,或者您误读了某些内容。我非常怀疑您不再需要提供 API 密钥。我会更新我的答案。
  • 我尝试使用 do = DoManager(None, 'api_token', api_version=2) 但现在输出返回:self.api_version = int(api_version) ValueError: invalid literal for int() with base 10: 'api_version'
  • 嘿伙计,我按照你的建议编辑了代码,但输出返回:ValueError: invalid literal for int() with base 10: 'api_version'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 2017-02-08
  • 2012-10-08
相关资源
最近更新 更多