docker_image/docker_container not working - The error was: SyntaxError: invalid syntax

Hi,

Can someone please help me resolving this issue.

Summary

I am trying to build docker image passing in Dockerfile on the same host where Dockerfile and other related project files resides. Without using ansible, I am able to run my Dockerfile build an image out of it and start a container that is if i use docker itself. But when i try to put these steps into ansible playbook, it fails. On this host machine, docker, ansible, python-pip and docker sdk for python are installed

Outputs of different commands:
ansible localhost -m debug -a “var=ansible_python_interpreter”
localhost | SUCCESS => {
“ansible_python_interpreter”: “/usr/bin/python2”
}
pip --version
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)

pip show docker

Metadata-Version: 2.1
Name: docker
Version: 5.0.3

ansible --version | grep “python version”
python version = 2.7.5

inventory file
[local]
localhost ansible_connection=local

ansible --version

ansible 2.9.27 config file = /etc/ansible/ansible.cfg configured module search path = [u’/home/username/.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, Nov 13 2020, 02:52:00) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44.0.3)]

Operating System: Oracle Linux Server 7.9

ansible-playbook -i inventory docker-container.yml OR ansible-playbook docker-container.yml

hosts: local
vars:

  • dockerfile_sample: “Dockerfile”

  • docker_image_name: “built-thru-ansible:1”

  • docker_container_name: “running-thru-ansible”

  • proxy: “http://validproxyurl.com:XXXX”
    tasks:

  • name: “Build docker image”
    docker_image:
    name: “{{docker_image_name}}”
    build:
    path: “path/to/dockerfiledir/”
    pull: no
    dockerfile: “{{dockerfile_sample}}”
    buildargs:
    HTTP_PROXY: “{{proxy}}”
    HTTPS_PROXY: “{{proxy}}”
    state: present
    source: build

  • name: “launch docker container”
    docker_container:
    name: “{{docker_container_name}}”
    image: “{{docker_image_name}}”
    state: started
    exposed_ports:

  • “XXXX”
    ports:

  • “XXXX:XXXX”
    register: docker_info

Error: -

PLAY [local] ********************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************
[WARNING]: Platform linux on host localhost is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [localhost]

TASK [Build docker image] *******************************************************************************************************************************************************************************
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 "/home/infra/.ansible/tmp/ansible-tmp-1643737614.37-38618-81901358275286/AnsiballZ_docker_image.py", line 102, in \n _ansiballz_main()\n File "/home/infra/.ansible/tmp/ansible-tmp-1643737614.37-38618-81901358275286/AnsiballZ_docker_image.py", line 94, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File "/home/infra/.ansible/tmp/ansible-tmp-1643737614.37-38618-81901358275286/AnsiballZ_docker_image.py", line 40, in invoke_module\n runpy.run_module(mod_name=‘ansible.modules.cloud.docker.docker_image’, 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_image_payload_pKjB0q/ansible_docker_image_payload.zip/ansible/modules/cloud/docker/docker_image.py", line 436, in \n File "/tmp/ansible_docker_image_payload_pKjB0q/ansible_docker_image_payload.zip/ansible/module_utils/docker/common.py", line 44, in \n File "/usr/lib/python2.7/site-packages/docker/init.py", line 2, in \n from .api import APIClient\n File "/usr/lib/python2.7/site-packages/docker/api/init.py", line 2, in \n from .client import APIClient\n File "/usr/lib/python2.7/site-packages/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}

Per the changelog from the docker python module (https://docker-py.readthedocs.io/en/stable/change-log.html), support for Python 2.7 was removed in version 5.0.0.

You will need to downgrade to an older version, or switch to using python3 to run your tasks.

Hi,

when asking the same question in multiple locations
(https://github.com/ansible-collections/community.docker/issues/290),
please mention that so that people can check whether it has already
been answered before spending time answering it again.

Thanks,
Felix