Import_tasks results in fatal error: "shared connection closed"

A group of tasks is used twice in a play, therefore they are placed in a separate file and invoked using ‘import_tasks’. However, the result is, in all variations I’ve tried:

TASK [linux-common : Include Xymon client configuration tasks] *************************************
fatal: [kapitein]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 192.168.0.110 closed.\r\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 0}

There are no problems with the (SSH) connection. The ansible server is running on Linux Mint 20.1, ansible version 2.9.6, python version 3.8.10. The playbook is:

---
- name: Jardinera configuration
  hosts: Linux
  roles:
  - { role: linux-locale,        when: "'Linux'       in group_names" }
  - { role: raspberry-pi-common, when: "'RaspberryPi' in group_names" }
  - { role: vision-common,       when: "'Vision'      in group_names" }
  - { role: linux-common,        when: "'Linux'       in group_names" }
  - { role: linux-zram,          when: "'Linux'       in group_names" }
  - { role: jardinera-common,    when: "'Linux'       in group_names" }

 - import_playbook: playbook.kapitein.yaml
 - import_playbook: playbook.plant0.yaml

The import_tasks is located in role linux-common. The relevant part of the tasks file in this role is:

- name: Include Xymon client configuration tasks
  ansible.builtin.import_tasks:
    file: "~/ansible/{{ jardinera_path }}tasks.xymon.client.yaml"
  when: "'XymonProxy' in group_names or 'XymonServer' in group_names"

with variable ‘jardinera_path’ containing “jardinera/”. The start of file ‘tasks.xymon.client.yaml’ is:

---
- name: Add user xymon to groups adm and video
  user:
    name: xymon
    groups: adm,video
    append: true
  when: "'Xymon' in group_names"

If the contents of file `tasks.xymon.client.yaml’ is included in the tasks section of the role, it just works.
What am I doing wrong?

import_tasks doesn’t works across roles, you need to user the import role module, for example:

- name: Include Xymon client configuration tasks from the linux-common role
  ansible.builtin.import_role:
    name: linux-common
    tasks_from: "~/ansible/{{ jardinera_path }}tasks.xymon.client.yaml"
  when: ( "XymonProxy" in group_names ) or ( "XymonServer" in group_names )

Thanks, that was something I did not find in the documentation. The tasks file is therefore moved to sub-directory …/roles/linux-common/tasks/ and the absolute path-name is changed to be only the file-name. This moves the tasks file into the same role, which is also shown in URL https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html. However, running the play results in the same error.

Then the syntax was adjusted to match the aforementioned URL, and after changing

  ansible.builtin.import_tasks:
    file: tasks.xymon.client.yaml

into

  import_tasks: tasks.xymon.client.yaml

it works. I do not know why removing ‘syntactic sugar’ makes it work.

Perhaps you might be better off using the include_tasks module, the difference between import and include might be the issue here?

See the documentation on re-using files and roles.

Using ‘include_tasks’ i.s.o. ‘import_tasks’ with the extended syntax does result in the original fatal error: “shared connection closed”. Using ‘include_tasks’ i.s.o. ‘import_tasks’ with the minimal syntax works.

I’ve tried ‘include_taks’ earlier, without any difference. In my use case the major difference between ‘include_tasks’ and ‘import_tasks’ is when the when-clause is evaluated.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.