还是着急用了,没有首先去看官方的第一手文档
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: SyntaxError: invalid syntax
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/root/.ansible/tmp/ansible-tmp-1640316635.85-35343-270231667758987/AnsiballZ_docker_login.py\", line 102, in \n _ansiballz_main()\n File \"/root/.ansible/tmp/ansible-tmp-1640316635.85-35343-270231667758987/AnsiballZ_docker_login.py\", line 94, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/root/.ansible/tmp/ansible-tmp-1640316635.85-35343-270231667758987/AnsiballZ_docker_login.py\", line 40, in invoke_module\n runpy.run_module(mod_name='ansible.modules.cloud.docker.docker_login', init_globals=None, run_name='__main__', alter_sys=True)\n File \"/usr/lib64/python2.7/runpy.py\", line 176, in run_module\n fname, loader, pkg_name)\n File \"/usr/lib64/python2.7/runpy.py\", line 82, in _run_module_code\n mod_name, mod_fname, mod_loader, pkg_name)\n File \"/usr/lib64/python2.7/runpy.py\", line 72, in _run_code\n exec code in run_globals\n File \"/tmp/ansible_docker_login_payload_NFbK0Z/ansible_docker_login_payload.zip/ansible/modules/cloud/docker/docker_login.py\", line 133, in \n File \"/usr/lib/python2.7/site-packages/docker-5.1.0_dev-py2.7.egg/docker/__init__.py\", line 2, in \n from .api import APIClient\n File \"/usr/lib/python2.7/site-packages/docker-5.1.0_dev-py2.7.egg/docker/api/__init__.py\", line 2, in \n from .clientimport APIClient\n File \"/usr/lib/python2.7/site-packages/docker-5.1.0_dev-py2.7.egg/docker/api/client.py\", line 222\n f'Error while fetching server API version: {e}'\n ^\nSyntaxError: invalid syntax\n" , "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
官方文档
For Python 2.7, you need to use a version between 2.0.0 and 4.4.4 since the Python package for Docker removed support for Python 2.7 on 5.0.0. You can install the specific version of the Docker SDK for Python as follows:
$ pip install 'docker==4.4.4'
因为我这里是的使用场景是用在项目现场的,所以无法连接互联网,所以打算通过离线方式安装上面的docker模块,然后这里就是各种环境问题,我这里先是用本地环境测试的,本地做好了才方便做成roles方案直接调用,现在说说自己踩过的坑或走的弯路吧。
# yum 源使用的是本地内网搭建的,现场也是如此
$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
$ ssh -V
OpenSSH_8.8p1, OpenSSL 1.1.1l 24 Aug 2021
$ ansible --version
ansible 2.9.25
$ python --version
Python 2.7.5
# 先学下模块用法
$ ansible-doc -l|grep docker
# 直接测试登录模块
$ cat test.yaml
---
- name: test module docker_login
hosts: localhost
tasks:
- name: Log into private registry and force re-authorization
docker_login:
registry: harbor.dockerregistry.com
username: admin
password: Harbor12345
reauthorize: yes
# 然后就有了此文档开篇的那个报错
# 然后各种博客帖子里查,现在回想这里是在干蠢事,无用功。按照别人博客的意思将docker与requests两个模块装了,然后还是各种报错,这时候报的是语法错误。
# 后面偶然查到了官方文档对这个模块的介绍的一开始的地方。
这里沿着上面官方给的版本要求给docker-py对应4.4.4装了,不过我这里要走离线模式。
https://github.com/docker/docker-py/releases/tag/4.4.4
$ cat docker-py-4.4.4/requirements.txt
appdirs==1.4.3
asn1crypto==0.22.0
backports.ssl-match-hostname==3.5.0.1
cffi==1.14.4
cryptography==3.2
enum34==1.1.6
idna==2.5
ipaddress==1.0.18
packaging==16.8
paramiko==2.4.2
pycparser==2.17
pyOpenSSL==18.0.0
pyparsing==2.2.0
pywin32==227; sys_platform == 'win32'
requests==2.20.0
six==1.10.0
urllib3==1.24.3
websocket-client==0.56.0
上面模块给的是一种依赖,实际操作又是另一种,若哪位朋友使用有缘使用我这份文档也仍有可能存在这个问题,不过整体来讲,我这环境还算清爽,基本没装什么,估计问题也不大
py模块依赖的下载地址
# 后面安装py模块时模块所需要的环境依赖
$ yum -y install libffi-devel python-devel
# 源码包清单
$ cat list-tar.txt
appdirs-1.4.3
asn1crypto-0.22.0
backports.ssl_match_hostname-3.5.0.1
enum34-1.1.6
idna-2.5
ipaddress-1.0.18
packaging-16.8
pycparser-2.17
pyparsing-2.2.0
requests-2.20.0
six-1.10.0
urllib3-1.24.3
websocket_client-0.56.0
cffi-1.14.4
PyNaCl-1.0.1
bcrypt-3.1.3
paramiko-2.4.2
setuptools-18.5
cryptography-2.4.2
pyOpenSSL-18.0.0
$ cd docker-py-4.4.4/
$ python setup.py install
/usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'project_urls'
warnings.warn(msg)
/usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
warnings.warn(msg)
/usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'long_description_content_type'
warnings.warn(msg)
running install
running bdist_egg
running egg_info
writing requirements to docker.egg-info/requires.txt
writing docker.egg-info/PKG-INFO
writing top-level names to docker.egg-info/top_level.txt
writing dependency_links to docker.egg-info/dependency_links.txt
reading manifest file 'docker.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'README.rst'
writing manifest file 'docker.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/docker
copying build/lib/docker/__init__.py -> build/bdist.linux-x86_64/egg/docker
copying build/lib/docker/auth.py -> build/bdist.linux-x86_64/egg/docker
copying build/lib/docker/client.py -> build/bdist.linux-x86_64/egg/docker
copying build/lib/docker/constants.py -> build/bdist.linux-x86_64/egg/docker
copying build/lib/docker/errors.py -> build/bdist.linux-x86_64/egg/docker
copying build/lib/docker/tls.py -> build/bdist.linux-x86_64/egg/docker
copying build/lib/docker/version.py -> build/bdist.linux-x86_64/egg/docker
creating build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/__init__.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/build.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/client.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/config.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/container.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/daemon.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/exec_api.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/image.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/network.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/plugin.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/secret.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/service.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/swarm.py -> build/bdist.linux-x86_64/egg/docker/api
copying build/lib/docker/api/volume.py -> build/bdist.linux-x86_64/egg/docker/api
creating build/bdist.linux-x86_64/egg/docker/context
copying build/lib/docker/context/__init__.py -> build/bdist.linux-x86_64/egg/docker/context
copying build/lib/docker/context/api.py -> build/bdist.linux-x86_64/egg/docker/context
copying build/lib/docker/context/config.py -> build/bdist.linux-x86_64/egg/docker/context
copying build/lib/docker/context/context.py -> build/bdist.linux-x86_64/egg/docker/context
creating build/bdist.linux-x86_64/egg/docker/credentials
copying build/lib/docker/credentials/__init__.py -> build/bdist.linux-x86_64/egg/docker/credentials
copying build/lib/docker/credentials/constants.py -> build/bdist.linux-x86_64/egg/docker/credentials
copying build/lib/docker/credentials/errors.py -> build/bdist.linux-x86_64/egg/docker/credentials
copying build/lib/docker/credentials/store.py -> build/bdist.linux-x86_64/egg/docker/credentials
copying build/lib/docker/credentials/utils.py -> build/bdist.linux-x86_64/egg/docker/credentials
creating build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/__init__.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/configs.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/containers.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/images.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/networks.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/nodes.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/plugins.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/resource.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/secrets.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/services.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/swarm.py -> build/bdist.linux-x86_64/egg/docker/models
copying build/lib/docker/models/volumes.py -> build/bdist.linux-x86_64/egg/docker/models
creating build/bdist.linux-x86_64/egg/docker/transport
copying build/lib/docker/transport/__init__.py -> build/bdist.linux-x86_64/egg/docker/transport
copying build/lib/docker/transport/basehttpadapter.py -> build/bdist.linux-x86_64/egg/docker/transport
copying build/lib/docker/transport/npipeconn.py -> build/bdist.linux-x86_64/egg/docker/transport
copying build/lib/docker/transport/npipesocket.py -> build/bdist.linux-x86_64/egg/docker/transport
copying build/lib/docker/transport/sshconn.py -> build/bdist.linux-x86_64/egg/docker/transport
copying build/lib/docker/transport/ssladapter.py -> build/bdist.linux-x86_64/egg/docker/transport
copying build/lib/docker/transport/unixconn.py -> build/bdist.linux-x86_64/egg/docker/transport
creating build/bdist.linux-x86_64/egg/docker/types
copying build/lib/docker/types/__init__.py -> build/bdist.linux-x86_64/egg/docker/types
copying build/lib/docker/types/base.py -> build/bdist.linux-x86_64/egg/docker/types
copying build/lib/docker/types/containers.py -> build/bdist.linux-x86_64/egg/docker/types
copying build/lib/docker/types/daemon.py -> build/bdist.linux-x86_64/egg/docker/types
copying build/lib/docker/types/healthcheck.py -> build/bdist.linux-x86_64/egg/docker/types
copying build/lib/docker/types/networks.py -> build/bdist.linux-x86_64/egg/docker/types
copying build/lib/docker/types/services.py -> build/bdist.linux-x86_64/egg/docker/types
copying build/lib/docker/types/swarm.py -> build/bdist.linux-x86_64/egg/docker/types
creating build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/__init__.py -> build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/build.py -> build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/config.py -> build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/decorators.py -> build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/fnmatch.py -> build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/json_stream.py -> build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/ports.py -> build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/proxy.py -> build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/socket.py -> build/bdist.linux-x86_64/egg/docker/utils
copying build/lib/docker/utils/utils.py -> build/bdist.linux-x86_64/egg/docker/utils
byte-compiling build/bdist.linux-x86_64/egg/docker/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/auth.py to auth.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/client.py to client.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/constants.py to constants.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/errors.py to errors.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/tls.py to tls.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/version.py to version.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/build.py to build.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/client.py to client.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/config.py to config.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/container.py to container.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/daemon.py to daemon.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/exec_api.py to exec_api.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/image.py to image.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/network.py to network.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/plugin.py to plugin.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/secret.py to secret.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/service.py to service.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/swarm.py to swarm.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/api/volume.py to volume.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/context/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/context/api.py to api.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/context/config.py to config.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/context/context.py to context.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/credentials/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/credentials/constants.py to constants.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/credentials/errors.py to errors.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/credentials/store.py to store.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/credentials/utils.py to utils.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/configs.py to configs.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/containers.py to containers.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/images.py to images.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/networks.py to networks.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/nodes.py to nodes.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/plugins.py to plugins.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/resource.py to resource.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/secrets.py to secrets.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/services.py to services.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/swarm.py to swarm.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/models/volumes.py to volumes.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/transport/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/transport/basehttpadapter.py to basehttpadapter.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/transport/npipeconn.py to npipeconn.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/transport/npipesocket.py to npipesocket.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/transport/sshconn.py to sshconn.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/transport/ssladapter.py to ssladapter.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/transport/unixconn.py to unixconn.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/types/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/types/base.py to base.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/types/containers.py to containers.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/types/daemon.py to daemon.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/types/healthcheck.py to healthcheck.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/types/networks.py to networks.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/types/services.py to services.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/types/swarm.py to swarm.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/build.py to build.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/config.py to config.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/decorators.py to decorators.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/fnmatch.py to fnmatch.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/json_stream.py to json_stream.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/ports.py to ports.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/proxy.py to proxy.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/socket.py to socket.pyc
byte-compiling build/bdist.linux-x86_64/egg/docker/utils/utils.py to utils.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying docker.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying docker.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying docker.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying docker.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying docker.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying docker.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating 'dist/docker-4.4.4-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing docker-4.4.4-py2.7.egg
removing '/usr/lib/python2.7/site-packages/docker-4.4.4-py2.7.egg' (and everything under it)
creating /usr/lib/python2.7/site-packages/docker-4.4.4-py2.7.egg
Extracting docker-4.4.4-py2.7.egg to /usr/lib/python2.7/site-packages
docker 4.4.4 is already the active version in easy-install.pth
Installed /usr/lib/python2.7/site-packages/docker-4.4.4-py2.7.egg
Processing dependencies for docker==4.4.4
Searching for backports.ssl-match-hostname==3.5.0.1
Best match: backports.ssl-match-hostname 3.5.0.1
Adding backports.ssl-match-hostname 3.5.0.1 to easy-install.pth file
Using /usr/lib/python2.7/site-packages
Searching for ipaddress==1.0.18
Best match: ipaddress 1.0.18
Processing ipaddress-1.0.18-py2.7.egg
ipaddress 1.0.18 is already the active version in easy-install.pth
Using /usr/lib/python2.7/site-packages/ipaddress-1.0.18-py2.7.egg
Searching for requests==2.14.2
Best match: requests 2.14.2
Processing requests-2.14.2-py2.7.egg
requests 2.14.2 is already the active version in easy-install.pth
Using /usr/lib/python2.7/site-packages/requests-2.14.2-py2.7.egg
Searching for websocket-client==0.56.0
Best match: websocket-client 0.56.0
Processing websocket_client-0.56.0-py2.7.egg
websocket-client 0.56.0 is already the active version in easy-install.pth
Installing wsdump.py script to /usr/bin
Using /usr/lib/python2.7/site-packages/websocket_client-0.56.0-py2.7.egg
Searching for six==1.10.0
Best match: six 1.10.0
Processing six-1.10.0-py2.7.egg
six 1.10.0 is already the active version in easy-install.pth
Using /usr/lib/python2.7/site-packages/six-1.10.0-py2.7.egg
Finished processing dependencies for docker==4.4.4
$ ansible-playbook test.yaml
PLAY [test module docker_login] ****************************************************************************************************************
TASK [Log into private registry and force re-authorization] ************************************************************************************
changed: [localhost]
PLAY RECAP *************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
# 实测可用
$ cat ansible.cfg
[defaults]
inventory = inventory
forks = 200
remote_user = root
roles_path = roles
pipelining = True
host_key_checking = False
gathering = smart
fact_caching = jsonfile
fact_caching_timeout = 86400
fact_caching_connection = fact_cache
ssh_args = -C -o ControlMaster=auto -o ControlPersist=5d