通过 Ansible 使用子网时无法创建 EC2(通过 AWS-CLI 同样有效)

Unable to create EC2 when subnet is used via Ansible(same works through AWS-CLI)

我正在尝试使用 ansible 创建 EC2 实例。如果我在没有子网(和默认安全组)的情况下尝试这样做,它会完美运行并创建 EC2。但这不是我想要的。我想使用特定的 'sg' 并使用已经存在的子网(由我的组织定义)来创建实例。

相同的子网和 'sg' 在使用 AWS-CLI(也通过控制台)、相同的配置文件、相同的图像、相同的密钥和相同的实例类型时工作正常。它在我的子网下创建实例并分配命令中传递的 sg - 完美!!我们可以在这里排除 access/role 相关问题吗(因为 CLI/console 工作正常)?如果是这样,Ansible/boto 还有什么问题?

AWS CLI:

aws ec2 run-instances --image-id ami-3d401234 --count 1 --instance-type t2.large --region us-east-1 --key-name MyKeyNameHere --security-group-ids sg-766b1234 --subnet-id subnet-09871234 --profile MyProfileNameHere

这是剧本。

 - name: Provision an EC2 node
    hosts: local
    connection: local
    gather_facts: false
    tags: provisioning
    vars:
      instance_type: t2.large
      image: ami-3d401234
      group_id: sg-766b1234
      region: us-east-1
      keypair: MyKeyNameHere
      vpc_subnet_id: subnet-09871234

    tasks:
      - name: Launch new Instance
        local_action: ec2 instance_tags="Name=MyInstance"
                      instance_type={{ instance_type}}
                      image={{ image }}
                      wait=true
                      group_id={{ group_id }}
                      profile=MyProfileNameHere
                      region={{ region }}
                      vpc_subnet_id={{ vpc_subnet_id }}
                      keypair={{ keypair }}
        register: ec2

这里是错误,不知道为什么又是 401(剧本中没有提到个人资料时得到这个)。我确定访问密钥和密钥是正确的,因为我可以使用默认 sg 创建。

    vpc_id = vpc.get_all_subnets(subnet_ids=[vpc_subnet_id])[0].vpc_id
  File "/Library/Python/2.7/site-packages/boto-2.38.0-py2.7.egg/boto/vpc/__init__.py", line 1153, in get_all_subnets
    return self.get_list('DescribeSubnets', params, [('item', Subnet)])
  File "/Library/Python/2.7/site-packages/boto-2.38.0-py2.7.egg/boto/connection.py", line 1186, in get_list
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.EC2ResponseError: EC2ResponseError: 401 Unauthorized
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>AuthFailure</Code><Message>AWS was not able to validate the provided access credentials</Message></Error></Errors><RequestID>6182f17d-f62e-4d57-b351-3498dc8a53b7</RequestID></Response>

我在 ~/.boto 文件中有访问密钥和秘密密钥信息。只是 aws_access_key_id 和 aws_secret_access_key。无IAM角色信息等

这在 boto 中缺失,aws_security_token。从来没有想过这是需要的,因为我已经传递了访问密钥和秘密密钥。我想这是需要的,因为我获得了组织组的访问权限?我添加了这个,现在可以使用了。感谢@helloV 让我再次检查你评论的差异:-)