Not able to execute python3.8 code with ansible


I have a playbook:

- hosts: all
  gather_facts: true

    - name: test
        cmd: python /tmp/
        chdir: /tmp/
      async: 150
      poll: 5
      become: true
      register: test
    - debug: msg={{test}}

Below is the host file for me: ansible_ssh_user=xxx ansible_ssh_pass=xxxxxxxxxxxxxxx ansible_sudo_pass=xxxxxxxxxxxxxxx ansible_ssh_common_args='-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' ansible_python_interpreter=/usr/bin/pytho3.8

Below is the python code:

import time
print("Hello World")

Before upgrade to python3.8, it was working till 3.6. But after upgrading to python3.8, I am getting below error:

ERROR! Unexpected Exception, this is probably a bug: invalid syntax (, line 96)
the full traceback was:

Traceback (most recent call last):
  File "/usr/local/bin/ansible-playbook", line 106, in <module>
    exit_code =
  File "/usr/local/lib/python3.8/dist-packages/ansible/cli/", line 122, in run
    results =
  File "/usr/local/lib/python3.8/dist-packages/ansible/executor/", line 82, in run
    pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 54, in load
    pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 106, in _load_playbook_data
    entry_obj = Play.load(entry, variable_manager=variable_manager, loader=self._loader)
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 117, in load
    return p.load_data(data, variable_manager=variable_manager, loader=loader)
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 255, in load_data
    self._attributes[name] = method(name, ds[name])
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 147, in _load_tasks
    return load_list_of_blocks(ds=ds, play=self, variable_manager=self._variable_manager, loader=self._loader)
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 71, in load_list_of_blocks
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 90, in load
    return b.load_data(data, variable_manager=variable_manager, loader=loader)
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 255, in load_data
    self._attributes[name] = method(name, ds[name])
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 118, in _load_block
    return load_list_of_tasks(
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 341, in load_list_of_tasks
    t = Task.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 139, in load
    return t.load_data(data, variable_manager=variable_manager, loader=loader)
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 245, in load_data
    ds = self.preprocess_data(ds)
  File "/usr/local/lib/python3.8/dist-packages/ansible/playbook/", line 179, in preprocess_data
    (action, args, delegate_to) = args_parser.parse()
  File "/usr/local/lib/python3.8/dist-packages/ansible/parsing/", line 289, in parse
    if item in module_loader or item in action_loader or item in ['meta', 'include', 'include_tasks', 'include_role', 'import_tasks', 'import_role']:
  File "/usr/local/lib/python3.8/dist-packages/ansible/plugins/", line 323, in has_plugin
    return self.find_plugin(name) is not None
  File "/usr/local/lib/python3.8/dist-packages/ansible/plugins/", line 265, in find_plugin
    for path in (p for p in self._get_paths() if p not in self._searched_paths and os.path.isdir(p)):
  File "/usr/local/lib/python3.8/dist-packages/ansible/plugins/", line 176, in _get_paths
  File "/usr/local/lib/python3.8/dist-packages/ansible/plugins/", line 141, in _get_package_paths
    m = __import__(self.package)
  File "/usr/local/lib/python3.8/dist-packages/ansible/plugins/action/", line 96
    if self._task.async and not self._supports_async:
SyntaxError: invalid syntax

I have to use python3.8 only. So please let me know the changes I need to make in my code or any other configuration changes. I am using ansible version .

Asked By: Aadhi Verma



The error is related to the use of the async keyword, which is a reserved keyword in Python 3.7 and later versions.

To fix this issue, you need to upgrade ansible to a version that supports python 3.8

Starting with version 2.9, Ansible supports Python 3.8, so any version of Ansible from 2.9 and higher will be compatible with Python 3.8

Answered By: Yaseen