# Detect if server listen on port 80 och 443

**URL:** <https://forum.ansible.com/t/detect-if-server-listen-on-port-80-och-443/6630>\
**Category:** Get Help\
**Created:** [June 18, 2024, 12:03pm UTC](https://forum.ansible.com/t/detect-if-server-listen-on-port-80-och-443/6630 "2024-06-18T12:03:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rickard](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@Rickard](https://forum.ansible.com/u/Rickard)\
**Post date:** [June 18, 2024, 12:03pm UTC](https://forum.ansible.com/t/detect-if-server-listen-on-port-80-och-443/6630/1 "2024-06-18T12:03:15Z")

</div>

I would like to detect if a server listens on port 80 or 443, I have a list of IPs that I get from an API.

```auto
- name: "Try https on the remote server"
  uri:
    url: "https://{{ item.ipAdress }}"
    method: GET
    timeout: 1
    validate_certs: no
  register: https_result
  failed_when: "'FAILED' in https_result"
  with_items: "{{ results.json }}"

```

When I run this code I get status -1 if the connection fails and status 200 if the connection works.

After this I would like to make 2 lists with IPs, one that failed and one that worked, then I use these two lists in a template and make a working config.

But I can’t figure out how to solve this bit. Does anyone have any ideas?

---

<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:** [June 18, 2024, 12:15pm UTC](https://forum.ansible.com/t/detect-if-server-listen-on-port-80-och-443/6630/2 "2024-06-18T12:15:32Z")

</div>

If you set `failed_when` to `false` and post the results from:

```yaml
- name: Debug https_result
  ansible.builtin.debug:
    var: https_result

```

I’m sure someone could post a `set_fact` task to write the two lists you are after.

BTW I’d probably use the [community.general.listen\_ports\_facts module](https://docs.ansible.com/ansible/latest/collections/community/general/listen_ports_facts_module.html) to get a list of open ports on servers.

---

<div class="post-metadata">

**Author:** ![McGlovin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcglovin/32/3698_2.png) [@McGlovin](https://forum.ansible.com/u/McGlovin)\
**Post date:** [June 18, 2024, 10:06pm UTC](https://forum.ansible.com/t/detect-if-server-listen-on-port-80-och-443/6630/3 "2024-06-18T22:06:53Z")

</div>

As Chris mentioned, set the `failed_when` option to `false`.

After this you can then create your two lists using `set_fact` as follows:

```auto
- name: Create Lists of Addresses
  ansible.builtin.set_fact:
    https_success_list: "{{ https_success_list | default([]) + ([https_result_item['item']['ipAddress']] if https_result_item['status'] == 200 else []) }}"
    https_fail_list: "{{ https_fail_list | default([]) + ([https_result_item['item']['ipAddress']] if https_result_item['status'] != 200 else []) }}"
  loop: "{{ https_result['results'] }}"
  loop_control:
    loop_var: https_result_item

```

The `https_fail_list` will be any item that doesn’t respond with a 200 status, if you only want to match a status of -1, just change the 200 to -1.

---

<div class="post-metadata">

**Author:** ![Rickard](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@Rickard](https://forum.ansible.com/u/Rickard)\
**Post date:** [June 19, 2024, 8:19am UTC](https://forum.ansible.com/t/detect-if-server-listen-on-port-80-och-443/6630/4 "2024-06-19T08:19:12Z")

</div>

Thanks, that worked as I wanted.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 19, 2024, 8:19am UTC](https://forum.ansible.com/t/detect-if-server-listen-on-port-80-och-443/6630/5 "2024-07-19T08:19:19Z")

</div>

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