# Shared connection to host closed

**URL:** <https://forum.ansible.com/t/shared-connection-to-host-closed/41118>\
**Category:** Get Help\
**Tags:** ubuntu\
**Created:** [March 19, 2025, 8:37am UTC](https://forum.ansible.com/t/shared-connection-to-host-closed/41118 "2025-03-19T08:37:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sickdyd](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sickdyd/32/7487_2.png) [@sickdyd](https://forum.ansible.com/u/sickdyd)\
**Post date:** [March 19, 2025, 8:37am UTC](https://forum.ansible.com/t/shared-connection-to-host-closed/41118/1 "2025-03-19T08:37:45Z")

</div>

I have a simple task that runs a bash script. When the script exits with 0, the stderr still contains “Shared connection to host closed.”. This is quite misleading, since there was no actual error.

This is the script:

```auto
#!/bin/bash

systemctl is-active nginx

```

The task:

```auto
- name: Check if Nginx service is running
  script: check.sh
  register: s3q2

- debug:
    var: s3q2

- name: Store s3q2 result as a persistent fact
  set_fact:
    s3q2_result:
      exit_code: "{{ s3q2.rc }}"
      additional_data:
        script: "{{ lookup('file', 'check.sh') }}"
        stdout: "{{ s3q2.stdout }}"
        stderr: "{{ s3q2.stderr }}"

```

How can I prevent stderr containing “Shared connection to … closed.”?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [March 19, 2025, 11:59am UTC](https://forum.ansible.com/t/shared-connection-to-host-closed/41118/2 "2025-03-19T11:59:51Z")

</div>

I stop failures from causing tasks to fail like this:

```yaml
- name: Check if Nginx service is running
  script: check.sh
  register: s3q2
  failed_when: s3q2.rc is not ansible.builtin.regex('^0|1$')

```

However this might not be the answer to your _“Shared connection to host closed.”_ issue? 🤷‍♀️

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [March 19, 2025, 1:17pm UTC](https://forum.ansible.com/t/shared-connection-to-host-closed/41118/3 "2025-03-19T13:17:30Z")

</div>

If the shell script really is just checking the state of a service, you might consider using [ansible.builtin.service\_facts](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_facts_module.html) instead. This uses a builtin module that will already have appropriate fact gathering/setting, as well as failure detection built in.

The [examples](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_facts_module.html#examples) section shows how to poll a host, and then act on the state of a specific service as needed.

---

<div class="post-metadata">

**Author:** ![sickdyd](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sickdyd/32/7487_2.png) [@sickdyd](https://forum.ansible.com/u/sickdyd)\
**Post date:** [March 20, 2025, 12:13am UTC](https://forum.ansible.com/t/shared-connection-to-host-closed/41118/4 "2025-03-20T00:13:23Z")

</div>

The system service check is just one of the examples. I will need to test a bunch of other things such as if a file was created, if the contents of the file match X, if an endpoint returns a certain value and many more.

It sounds that the “Shared connection to host closed.” really has no workaround.

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [March 20, 2025, 11:33am UTC](https://forum.ansible.com/t/shared-connection-to-host-closed/41118/5 "2025-03-20T11:33:16Z")

</div>

Usually exit codes (as @Chris suggested) are the way to determine script failure.

If you really need to, then you can supply `-o LogLevel=QUIET` to the ssh command via [ssh\_common\_args](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html#parameter-ssh_common_args) option for the ssh connection plugin (the link lists the ways to configure it).

But this will be thrown out the windows if you ever choose to use `-v` flag with ansible which also enables ssh verbosity with a lot more messages.

Alternatively you could use [copy](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html) and [command](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html) module together to avoid this.

That said, it’s always the best option to existing modules instead of resorting to shell scripts whenever possible.

Here are some examples for the things you listed:

> if a file was created

Check [stat](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html) module, and it’s `result.stat.exists` return option.

> if the contents of the file match X

Check [stat](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html) module, and it’s `result.stat.checksum` to compare the content.

> if an endpoint returns a certain value

Check [uri](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html) module

* * *

Feel free to ask help for other tasks you need to do! 🙂
