Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法使用ansible的ali_instance_facts获取ecs信息 #185

Open
billy98 opened this issue Oct 29, 2019 · 17 comments
Open

无法使用ansible的ali_instance_facts获取ecs信息 #185

billy98 opened this issue Oct 29, 2019 · 17 comments

Comments

@billy98
Copy link

billy98 commented Oct 29, 2019

playbook的yml文件如下:

- name: fetch instances details example
  hosts: localhost
  vars:
    alicloud_access_key: xxxxxxxx
    alicloud_secret_key: xxxxxxxxx
    alicloud_region: cn-shenzhen
    availability_zone: cn-shenzhen-c

  tasks:
    - name: Find all instances in the specified region
      ali_instance_facts:
        alicloud_access_key: '{{ alicloud_access_key }}'
        alicloud_secret_key: '{{ alicloud_secret_key }}'
        alicloud_region: '{{ alicloud_region }}'
        instance_ids:
          - "i-wz98diszrt8loypkf4aa"
      register: instances_by_ids
      #register: all_instances

报错信息如下:

PLAY [fetch instances details example] *************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************
ok: [localhost]

TASK [Find all instances in the specified region] **************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1572316666.62-173569628691791/AnsiballZ_ali_instance_facts.py\", line 114, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1572316666.62-173569628691791/AnsiballZ_ali_instance_facts.py\", line 106, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1572316666.62-173569628691791/AnsiballZ_ali_instance_facts.py\", line 49, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_ali_instance_facts_payload_9CvOfL/__main__.py\", line 354, in <module>\n  File \"/tmp/ansible_ali_instance_facts_payload_9CvOfL/ansible_ali_instance_facts_payload.zip/ansible/module_utils/alicloud_ecs.py\", line 33, in <module>\n  File \"/usr/lib/python2.7/site-packages/footmark/ecs/__init__.py\", line 4, in <module>\n    from footmark.ecs.connection import ECSConnection\n  File \"/usr/lib/python2.7/site-packages/footmark/ecs/connection.py\", line 29, in <module>\n    class ECSConnection(ACSQueryConnection):\n  File \"/usr/lib/python2.7/site-packages/footmark/ecs/connection.py\", line 32, in ECSConnection\n    DefaultRegionName = '杭州'.encode(\"UTF-8\")\nUnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

PLAY RECAP *****************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
@lixue323
Copy link
Contributor

我这执行并没有报错,麻烦使用-vvv查看一下具体报错信息

@billy98
Copy link
Author

billy98 commented Oct 30, 2019

ansible-playbook 2.8.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.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-playbook
  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /root/deploy/ansible/hosts as it did not pass it's verify_file() method
script declined parsing /root/deploy/ansible/hosts as it did not pass it's verify_file() method
auto declined parsing /root/deploy/ansible/hosts as it did not pass it's verify_file() method
 [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

Parsed /root/deploy/ansible/hosts inventory source with ini plugin

PLAYBOOK: get_ecs.yml ******************************************************************************************************************
1 plays in get_ecs.yml

PLAY [fetch instances details example] *************************************************************************************************
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1572424267.9-226961584106770 `" && echo ansible-tmp-1572424267.9-226961584106770="` echo /root/.ansible/tmp/ansible-tmp-1572424267.9-226961584106770 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-267245NBGrr/tmpRDDaX6 TO /root/.ansible/tmp/ansible-tmp-1572424267.9-226961584106770/AnsiballZ_setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1572424267.9-226961584106770/ /root/.ansible/tmp/ansible-tmp-1572424267.9-226961584106770/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /root/.ansible/tmp/ansible-tmp-1572424267.9-226961584106770/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1572424267.9-226961584106770/ > /dev/null 2>&1 && sleep 0'

TASK [Gathering Facts] *****************************************************************************************************************
task path: /root/deploy/ansible/get_ecs.yml:1
ok: [localhost]
META: ran handlers
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856 `" && echo ansible-tmp-1572424270.75-262686878795856="` echo /root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/alicloud/ali_instance_facts.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-267245NBGrr/tmpSKfRNO TO /root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/AnsiballZ_ali_instance_facts.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/ /root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/AnsiballZ_ali_instance_facts.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/AnsiballZ_ali_instance_facts.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/ > /dev/null 2>&1 && sleep 0'

TASK [Find all instances in the specified region] **************************************************************************************
task path: /root/deploy/ansible/get_ecs.yml:10
The full traceback is:
Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/AnsiballZ_ali_instance_facts.py", line 114, in <module>
    _ansiballz_main()
  File "/root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/AnsiballZ_ali_instance_facts.py", line 106, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/AnsiballZ_ali_instance_facts.py", line 49, in invoke_module
    imp.load_module('__main__', mod, module, MOD_DESC)
  File "/tmp/ansible_ali_instance_facts_payload_oEm44A/__main__.py", line 354, in <module>
  File "/tmp/ansible_ali_instance_facts_payload_oEm44A/ansible_ali_instance_facts_payload.zip/ansible/module_utils/alicloud_ecs.py", line 33, in <module>
  File "/usr/lib/python2.7/site-packages/footmark/ecs/__init__.py", line 4, in <module>
    from footmark.ecs.connection import ECSConnection
  File "/usr/lib/python2.7/site-packages/footmark/ecs/connection.py", line 29, in <module>
    class ECSConnection(ACSQueryConnection):
  File "/usr/lib/python2.7/site-packages/footmark/ecs/connection.py", line 32, in ECSConnection
    DefaultRegionName = '杭州'.encode("UTF-8")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)

fatal: [localhost]: FAILED! => {
    "changed": false, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/AnsiballZ_ali_instance_facts.py\", line 114, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/AnsiballZ_ali_instance_facts.py\", line 106, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1572424270.75-262686878795856/AnsiballZ_ali_instance_facts.py\", line 49, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_ali_instance_facts_payload_oEm44A/__main__.py\", line 354, in <module>\n  File \"/tmp/ansible_ali_instance_facts_payload_oEm44A/ansible_ali_instance_facts_payload.zip/ansible/module_utils/alicloud_ecs.py\", line 33, in <module>\n  File \"/usr/lib/python2.7/site-packages/footmark/ecs/__init__.py\", line 4, in <module>\n    from footmark.ecs.connection import ECSConnection\n  File \"/usr/lib/python2.7/site-packages/footmark/ecs/connection.py\", line 29, in <module>\n    class ECSConnection(ACSQueryConnection):\n  File \"/usr/lib/python2.7/site-packages/footmark/ecs/connection.py\", line 32, in ECSConnection\n    DefaultRegionName = '杭州'.encode(\"UTF-8\")\nUnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", 
    "rc": 1
}

PLAY RECAP *****************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

@lixue323
Copy link
Contributor

你使用的是最新版本的ansible-provider=1.11.0, 仅支持python3的,你的环境的python2

@billy98
Copy link
Author

billy98 commented Oct 30, 2019

什么版本支持python2?

@lixue323
Copy link
Contributor

1.10.0支持python2, 后续新功能都是基于python3开发的

@billy98
Copy link
Author

billy98 commented Oct 30, 2019

用python3还是有问题

TASK [Find all instances in the specified region] **************************************************************************************
task path: /root/deploy/ansible/get_ecs.yml:10
fatal: [dev-apollo]: FAILED! => {
    "changed": false, 
    "module_stderr": "Shared connection to 192.168.8.16 closed.\r\n", 
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/root/.ansible/tmp/ansible-tmp-1572427267.57-44715287743651/AnsiballZ_ali_instance_facts.py\", line 114, in <module>\r\n    _ansiballz_main()\r\n  File \"/root/.ansible/tmp/ansible-tmp-1572427267.57-44715287743651/AnsiballZ_ali_instance_facts.py\", line 106, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/root/.ansible/tmp/ansible-tmp-1572427267.57-44715287743651/AnsiballZ_ali_instance_facts.py\", line 49, in invoke_module\r\n    imp.load_module('__main__', mod, module, MOD_DESC)\r\n  File \"/usr/lib64/python3.6/imp.py\", line 235, in load_module\r\n    return load_source(name, filename, file)\r\n  File \"/usr/lib64/python3.6/imp.py\", line 170, in load_source\r\n    module = _exec(spec, sys.modules[name])\r\n  File \"<frozen importlib._bootstrap>\", line 618, in _exec\r\n  File \"<frozen importlib._bootstrap_external>\", line 678, in exec_module\r\n  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\r\n  File \"/tmp/ansible_ali_instance_facts_payload_edy3e1o6/__main__.py\", line 407, in <module>\r\n  File \"/tmp/ansible_ali_instance_facts_payload_edy3e1o6/__main__.py\", line 399, in main\r\n  File \"/usr/local/lib/python3.6/site-packages/footmark/ecs/connection.py\", line 266, in get_all_instances\r\n    for inst in self.get_list_new(self.build_request_params(filters), ['Instances', Instance]):\r\nUnboundLocalError: local variable 'filters' referenced before assignment\r\n", 
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", 
    "rc": 1
}

@lixue323
Copy link
Contributor

lixue323 commented Oct 30, 2019

解决方案: 卸载python3中的ansible, ansible-alicloud, footmark, 然后pip3 install ansible_alicloud --no-cache-dir, 使用ansible-playbook命令时指定python3 Interpreter.

@billy98
Copy link
Author

billy98 commented Oct 30, 2019

aliyun-python-sdk-core 这个sdk只支持python2

[root@apollo ansible]# pip3 install footmark==1.12.0
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting footmark==1.12.0
  Using cached https://files.pythonhosted.org/packages/16/3c/ae404cf60e143ad91f02c679fa8e4e2e77d1ab9f4bf8f8a2c3ff7adad61c/footmark-1.12.0.tar.gz
Collecting aliyun-python-sdk-core<2.9.6,>=2.9.5 (from footmark==1.12.0)
  Using cached https://files.pythonhosted.org/packages/ca/89/834d50f84329123b3d77c68591569e126d427a8bcd6004edb426ab438c51/aliyun-python-sdk-core-2.9.5.tar.gz
aliyun-python-sdk-core requires Python '<3' but the running Python is 3.6.8

@lixue323
Copy link
Contributor

lixue323 commented Nov 4, 2019

卸载python3中的ansible, ansible-alicloud, footmark, 然后pip3 install ansible_alicloud --no-cache-dir

@billy98
Copy link
Author

billy98 commented Nov 4, 2019

还是不行

The full traceback is:
Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1572849563.7723527-81283504198962/AnsiballZ__ali_instance_facts.py", line 102, in <module>
    _ansiballz_main()
  File "/root/.ansible/tmp/ansible-tmp-1572849563.7723527-81283504198962/AnsiballZ__ali_instance_facts.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/root/.ansible/tmp/ansible-tmp-1572849563.7723527-81283504198962/AnsiballZ__ali_instance_facts.py", line 40, in invoke_module
    runpy.run_module(mod_name='ansible.modules.cloud.alicloud._ali_instance_facts', init_globals=None, run_name='__main__', alter_sys=False)
  File "/usr/lib64/python3.6/runpy.py", line 208, in run_module
    return _run_code(code, {}, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_ali_instance_facts_payload_c127ikho/ansible_ali_instance_facts_payload.zip/ansible/modules/cloud/alicloud/_ali_instance_facts.py", line 410, in <module>
  File "/tmp/ansible_ali_instance_facts_payload_c127ikho/ansible_ali_instance_facts_payload.zip/ansible/modules/cloud/alicloud/_ali_instance_facts.py", line 402, in main
  File "/usr/local/lib/python3.6/site-packages/footmark/ecs/connection.py", line 266, in get_all_instances
    for inst in self.get_list_new(self.build_request_params(filters), ['Instances', Instance]):
UnboundLocalError: local variable 'filters' referenced before assignment

fatal: [dev-apollo]: FAILED! => {
    "changed": false,
    "module_stderr": "Shared connection to 192.168.8.16 closed.\r\n",
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/root/.ansible/tmp/ansible-tmp-1572849563.7723527-81283504198962/AnsiballZ__ali_instance_facts.py\", line 102, in <module>\r\n    _ansiballz_main()\r\n  File \"/root/.ansible/tmp/ansible-tmp-1572849563.7723527-81283504198962/AnsiballZ__ali_instance_facts.py\", line 94, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/root/.ansible/tmp/ansible-tmp-1572849563.7723527-81283504198962/AnsiballZ__ali_instance_facts.py\", line 40, in invoke_module\r\n    runpy.run_module(mod_name='ansible.modules.cloud.alicloud._ali_instance_facts', init_globals=None, run_name='__main__', alter_sys=False)\r\n  File \"/usr/lib64/python3.6/runpy.py\", line 208, in run_module\r\n    return _run_code(code, {}, init_globals, run_name, mod_spec)\r\n  File \"/usr/lib64/python3.6/runpy.py\", line 85, in _run_code\r\n    exec(code, run_globals)\r\n  File \"/tmp/ansible_ali_instance_facts_payload_c127ikho/ansible_ali_instance_facts_payload.zip/ansible/modules/cloud/alicloud/_ali_instance_facts.py\", line 410, in <module>\r\n  File \"/tmp/ansible_ali_instance_facts_payload_c127ikho/ansible_ali_instance_facts_payload.zip/ansible/modules/cloud/alicloud/_ali_instance_facts.py\", line 402, in main\r\n  File \"/usr/local/lib/python3.6/site-packages/footmark/ecs/connection.py\", line 266, in get_all_instances\r\n    for inst in self.get_list_new(self.build_request_params(filters), ['Instances', Instance]):\r\nUnboundLocalError: local variable 'filters' referenced before assignment\r\n",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

@lixue323
Copy link
Contributor

lixue323 commented Nov 6, 2019

python2与3共存,命令容易混淆,建一个3的虚拟环境,按上述重新安装包,在虚拟环境中执行在试一下。

@lixue323
Copy link
Contributor

请问这个问题已经解决了吗?

@billy98
Copy link
Author

billy98 commented Nov 25, 2019

没有解决

@lixue323
Copy link
Contributor

pip install virtualenv 安装虚拟环境
virtualenv env3 --python=python3 创建3的虚拟环境env3
source env3/bin/activate 激活虚拟环境
pip install ansible_alicloud 在虚拟环境中安装ansible_alicloud
另外最新版本中 ali_instance_facts 更名为 ali_instance_info
在虚拟环境中,麻烦再运行一下文件。

@lework
Copy link

lework commented Apr 10, 2020

为啥不支持python2呢,ansible都没放弃。。。

@afucat
Copy link

afucat commented May 21, 2020

如果必须使用python2,这个要如何解决

@zhixiangjoy
Copy link

I meet the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants