使用动态库存时aws凭据错误

时间:2017-11-02 00:59:44

标签: amazon-web-services ansible aws-cli

当我尝试运行ansible playbook时,我收到了aws凭据身份验证错误。我做了配置并尝试手动创建凭证文件,但仍然是相同的错误,但我能够执行aws命令。

ansible 2.4.0.0
  config file = /home/centos/infrastructure/ansible.cfg
  configured module search path = [u'/home/centos/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]


[DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of become which is a generic framework . This feature will be removed in
version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
 [WARNING]:  * Failed to parse /home/centos/infrastructure/production/ec2.py with script plugin: Inventory script
(/home/centos/infrastructure/production/ec2.py) had an execution error: Traceback (most recent call last):   File
"/home/centos/infrastructure/production/ec2.py", line 1600, in <module>     Ec2Inventory()   File
"/home/centos/infrastructure/production/ec2.py", line 193, in __init__     self.do_api_calls_update_cache()   File
"/home/centos/infrastructure/production/ec2.py", line 525, in do_api_calls_update_cache     self.get_instances_by_region(region)
File "/home/centos/infrastructure/production/ec2.py", line 579, in get_instances_by_region     conn = self.connect(region)   File
"/home/centos/infrastructure/production/ec2.py", line 543, in connect     conn = self.connect_to_aws(ec2, region)   File
"/home/centos/infrastructure/production/ec2.py", line 568, in connect_to_aws     conn = module.connect_to_region(region,
**connect_args)   File "/usr/lib/python2.7/site-packages/boto/ec2/__init__.py", line 66, in connect_to_region     return
region.connect(**kw_params)   File "/usr/lib/python2.7/site-packages/boto/regioninfo.py", line 188, in connect     return
self.connection_cls(region=self, **kw_params)   File "/usr/lib/python2.7/site-packages/boto/ec2/connection.py", line 102, in __init__
profile_name=profile_name)   File "/usr/lib/python2.7/site-packages/boto/connection.py", line 1057, in __init__
profile_name=profile_name)   File "/usr/lib/python2.7/site-packages/boto/connection.py", line 568, in __init__     host, config,
self.provider, self._required_auth_capability())   File "/usr/lib/python2.7/site-packages/boto/auth.py", line 882, in get_auth_handler
'Check your credentials' % (len(names), str(names))) boto.exception.NoAuthHandlerFound: No handler was ready to authenticate. 1
handlers were checked. ['QuerySignatureV2AuthHandler'] Check your credentials

 [WARNING]:  * Failed to parse /home/centos/infrastructure/production/ec2.py with ini plugin:
/home/centos/infrastructure/production/ec2.py:3: Error parsing host definition ''''': No closing quotation

1 个答案:

答案 0 :(得分:1)

将AWS凭证与ansible一起使用的最简单方法之一是在主目录中的.aws /中创建凭证文件,并在其中放置访问密钥和秘密访问密钥(您可以创建多组凭据),即:

cat ~/.aws/credentials

[profile1]
aws_access_key_id = XXXXXXXXXXX
aws_secret_access_key = xxxxxxxxxxxxxxxx

[default]
aws_access_key_id = XXXXXXXXXXX
aws_secret_access_key = xxxxxxxxxxxxxxxx

然后你执行ansible-playbook:

AWS_PROFILE=profile1 ansible-playbook -i ec2.py playbook.yml

AWS_PROFILE是您可以通过执行

设置的环境变量
export AWS_PROFILE=profile1

请注意,您还需要一个具有默认AWS区域的环境变量,例如:

export AWS_EC2_REGION=ap-southeast-2