_ec2_instance_facts.py botocore.exceptions.NoCredentialsError: Unable to locate credentials

209 views
Skip to first unread message

Meir Yanovich

unread,
May 2, 2020, 11:13:41 AM5/2/20
to Ansible Project
hello all
im upgrading python from 2.7 to python 3.7 
and now I'm getting error when using aws API on module : module: ec2_instance_facts

- name: Get facts
local_action:
module: ec2_instance_facts
instance_ids:
- "xxxxxxx"
region: "xxxxxx"
register: instance
  ansible --version
ansible 2.7.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ec2-user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ec2-user/app_virtualenv/lib/python3.7/site-packages/ansible
  executable location = /home/ec2-user/app_virtualenv/bin/ansible
  python version = 3.7.6 (default, Feb 26 2020, 20:54:15) [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)]
  
  
  
ansible-config dump --only-changed
ANSIBLE_PIPELINING(/etc/ansible/ansible.cfg) = False
ANSIBLE_SSH_RETRIES(/etc/ansible/ansible.cfg) = 2
DEFAULT_CALLBACK_WHITELIST(/etc/ansible/ansible.cfg) = ['profile_tasks']
DEFAULT_LOAD_CALLBACK_PLUGINS(/etc/ansible/ansible.cfg) = True
DEFAULT_LOG_PATH(/etc/ansible/ansible.cfg) = /home/ec2-user/app/logs/ansible.log
DEFAULT_PRIVATE_KEY_FILE(/etc/ansible/ansible.cfg) = /home/ec2-user/.ssh/DEV_VPN.pem
DEFAULT_STDOUT_CALLBACK(/etc/ansible/ansible.cfg) = yaml
DEPRECATION_WARNINGS(/etc/ansible/ansible.cfg) = False
HOST_KEY_CHECKING(/etc/ansible/ansible.cfg) = False


python -m site
sys.path = [
    '/home/ec2-user/app',
    '/usr/lib64/python37.zip',
    '/usr/lib64/python3.7',
    '/usr/lib64/python3.7/lib-dynload',
    '/home/ec2-user/app_virtualenv/lib64/python3.7/site-packages',
    '/home/ec2-user/app_virtualenv/lib/python3.7/site-packages',
]
USER_BASE: '/home/ec2-user/.local' (exists)
USER_SITE: '/home/ec2-user/.local/lib/python3.7/site-packages' (exists)
ENABLE_USER_SITE: False


the error I'm getting :

2020-05-02 11:54:57,580 p=23305 u=ec2-user |  Saturday 02 May 2020  11:54:57 +0000 (0:00:00.476)       0:01:39.534 **********
2020-05-02 11:54:57,750 p=23305 u=ec2-user |  Using module file /home/ec2-user/app_virtualenv/lib/python3.7/site-packages/ansible/modules/cloud/amazon/ec2_instance_facts.py
2020-05-02 11:54:58,120 p=23305 u=ec2-user |  fatal: [10.0.5.187 -> localhost]: FAILED! => changed=false
  module_stderr: |-
    /home/ec2-user/.ansible/tmp/ansible-tmp-1588420497.6072006-50705771655817/AnsiballZ_ec2_instance_facts.py:17: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module'
s documentation for alternative uses
     
import imp
   
Traceback (most recent call last):
     
File "/home/ec2-user/.ansible/tmp/ansible-tmp-1588420497.6072006-50705771655817/AnsiballZ_ec2_instance_facts.py", line 113, in <module>
        _ansiballz_main
()
     
File "/home/ec2-user/.ansible/tmp/ansible-tmp-1588420497.6072006-50705771655817/AnsiballZ_ec2_instance_facts.py", line 105, in _ansiballz_main
        invoke_module
(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
     
File "/home/ec2-user/.ansible/tmp/ansible-tmp-1588420497.6072006-50705771655817/AnsiballZ_ec2_instance_facts.py", line 48, in invoke_module
        imp
.load_module('__main__', mod, module, MOD_DESC)
     
File "/usr/lib64/python3.7/imp.py", line 234, in load_module
       
return load_source(name, filename, file)
     
File "/usr/lib64/python3.7/imp.py", line 169, in load_source
       
module = _exec(spec, sys.modules[name])
     
File "<frozen importlib._bootstrap>", line 630, in _exec
     
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
     
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
     
File "/tmp/ansible_ec2_instance_facts_payload_zmc16g7o/__main__.py", line 555, in <module>
     
File "/tmp/ansible_ec2_instance_facts_payload_zmc16g7o/__main__.py", line 551, in main
     
File "/tmp/ansible_ec2_instance_facts_payload_zmc16g7o/__main__.py", line 505, in list_ec2_instances
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/paginate.py", line 449, in build_full_result
       
for response in self:
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/paginate.py", line 255, in __iter__
        response
= self._make_request(current_kwargs)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/paginate.py", line 332, in _make_request
       
return self._method(**current_kwargs)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/client.py", line 357, in _api_call
       
return self._make_api_call(operation_name, kwargs)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/client.py", line 648, in _make_api_call
        operation_model
, request_dict, request_context)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/client.py", line 667, in _make_request
       
return self._endpoint.make_request(operation_model, request_dict)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/endpoint.py", line 102, in make_request
       
return self._send_request(request_dict, operation_model)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/endpoint.py", line 132, in _send_request
        request
= self.create_request(request_dict, operation_model)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/endpoint.py", line 116, in create_request
        operation_name
=operation_model.name)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/hooks.py", line 356, in emit
       
return self._emitter.emit(aliased_event_name, **kwargs)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/hooks.py", line 228, in emit
       
return self._emit(event_name, kwargs)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/hooks.py", line 211, in _emit
        response
= handler(**kwargs)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/signers.py", line 90, in handler
       
return self.sign(operation_name, request)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/signers.py", line 157, in sign
        auth
.add_auth(request)
     
File "/home/ec2-user/app_virtualenv/lib/python3.7/site-packages/botocore/auth.py", line 357, in add_auth
       
raise NoCredentialsError
    botocore
.exceptions.NoCredentialsError: Unable to locate credentials
  module_stdout
: ''
  msg
: |-
    MODULE FAILURE
   
See stdout/stderr for the exact error
  rc
: 1


Although i have the credentials in .aws dir 
and when i doing ec2 simple API call I'm getting results :

python


   
Python 3.7.6 (default, Feb 26 2020, 20:54:15)
   
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
   
Type "help", "copyright", "credits" or "license" for more information.
   
>>> import boto3
   
>>> ec2_resource = boto3.resource('ec2')
   
>>> ec2_client = boto3.client('ec2')
   
>>> ec2_client.describe_instances()
   
{'Reservations': [{'Groups': [], 'Instances': [{'AmiLaunchIndex': 0, 'ImageId': 'ami-b66ed3de', 'InstanceId': 'i-be381945', 'InstanceType': 'm5.large', 'KeyName': 'DEV_VPN', 'LaunchTime': datetime.datetime(2019, 3, 4, 8, 25, 17, tzinfo=tzlocal()), 'Monitoring': {'State': 'disabled'}, 'Placement': {'Avail .................







Meir Yanovich

unread,
May 2, 2020, 11:39:30 AM5/2/20
to Ansible Project
after updating ansible to the latest I'm getting this error:



2020-05-02 11:54:57,580 p=23305 u=ec2-user |  task path: /home/ec2-user/ansible/ansible_linux/roles/package_deploy/tasks/main.yml:5
', mod, module, MOD_DESC)
      monitoring: true

Meir Yanovich

unread,
May 2, 2020, 7:56:12 PM5/2/20
to Ansible Project
after many trys i thing that  mybe the problem is that i need to install boto3 on the remote host 
fo this i did :
to install it but first, i need pip to be installed on remote
and i try to invoke it but i keep getting :


and i try to invoke it but i keep getting :


    fatal
: [10.0.6.182]: FAILED! => changed=false
      invocation
:
        module_args
:
          chdir
: null
          editable
: false
          executable
: null
          extra_args
: null
          name
:
         
- pip
          requirements
: null
          state
: latest
          umask
: null
          version
: null
          virtualenv
: null
          virtualenv_command
: virtualenv
          virtualenv_python
: null
          virtualenv_site_packages
: false
      msg
: Unable to find any of pip2, pip to use.  pip needs to be installed.


I'm using :

   
- name: Upgrade pip
      pip
: name=pip state=latest
      tags
:
       
- packages
   
   
- name: Ensure botocore and boto3 modules are installed
      pip
:
        name
: [ "boto3", "botocore"]
        extra_args
: "--user"

Meir Yanovich

unread,
May 4, 2020, 6:29:33 AM5/4/20
to Ansible Project
I'm answering my self and to help whom facing this problem
i couldn't find it on the entire internet!
the thing is that boto3 is searching for the AWS credentials in /etc/boto.cfg
and not in .aws directory ...
so to find out i had to debug ansible using this link which was very helpful
in fact, I think it is the most helpful link in ansible ecosystem.!
as this how you have suddenly eyes and can understand what is going on !

On Saturday, May 2, 2020 at 6:13:41 PM UTC+3, Meir Yanovich wrote:
Reply all
Reply to author
Forward
0 new messages