Search code examples
pythonansiblepython-requestsansible-2.x

What is the equivalent for HTTP method LIST in the Ansbile `uri` module?


I am trying to look for the HTTP method equivalent for the following playbook code for method LIST.

- name: List all folders
  ansible.builtin.uri:
    url: https://{{ My_vault_url }}
    method: LIST
    return_content: true
    body_format: json
  register: list_url

If I try like requests.list, I get an error that list is not a request method.

If it is a custom method how can I use it through the Python?


Solution

  • What is the equivalent for HTTP method LIST in the Ansbile uri module?

    Of course one can Use Ansible to interact with web endpoints, but there is no HTTP method LIST at all. There is only the possibility to use GET in order to get the page content with all folder links and if that is the question.

    A minimal example playbook

    ---
    - hosts: localhost
      become: false
      gather_facts: false
    
      vars:
    
        URL: download.rockylinux.org/pub/rocky/9/BaseOS/x86_64/os/Packages/
    
      tasks:
    
      - name: List all folders
        ansible.builtin.uri:
          url: https://{{ URL }}
          method: GET
          return_content: true
          body_format: raw
        register: response
    
      - name: Show Content
        debug:
          var: response.content
    

    will result into an output of

    TASK [Show Content] ***********************************************************************
    ok: [localhost] =>
      response.content: |-
        <html>
        <head><title>Index of /pub/rocky/9/BaseOS/x86_64/os/Packages/</title></head>
        <body>
        <h1>Index of /pub/rocky/9/BaseOS/x86_64/os/Packages/</h1><hr><pre><a href="../">../</a>
        <a href="a/">a/</a>                                                 11-Nov-2023 23:54                   -
        <a href="b/">b/</a>                                                 05-Apr-2024 06:41                   -
        <a href="c/">c/</a>                                                 06-Mar-2024 21:12                   -
        <a href="d/">d/</a>                                                 11-Nov-2023 23:54                   -
        <a href="e/">e/</a>                                                 26-Mar-2024 20:17                   -
        <a href="f/">f/</a>                                                 05-Apr-2024 06:41                   -
        <a href="g/">g/</a>                                                 05-Apr-2024 06:41                   -
        <a href="h/">h/</a>                                                 11-Nov-2023 23:54                   -
        <a href="i/">i/</a>                                                 06-Mar-2024 21:12                   -
        <a href="j/">j/</a>                                                 12-May-2023 04:58                   -
        <a href="k/">k/</a>                                                 05-Apr-2024 06:41                   -
        <a href="l/">l/</a>                                                 08-Apr-2024 21:46                   -
        <a href="m/">m/</a>                                                 15-Nov-2023 23:47                   -
        <a href="n/">n/</a>                                                 07-Mar-2024 22:24                   -
        <a href="o/">o/</a>                                                 08-Mar-2024 00:58                   -
        <a href="p/">p/</a>                                                 05-Apr-2024 06:41                   -
        <a href="q/">q/</a>                                                 12-May-2023 04:58                   -
        <a href="r/">r/</a>                                                 04-Apr-2024 02:27                   -
        <a href="s/">s/</a>                                                 12-Apr-2024 02:40                   -
        <a href="t/">t/</a>                                                 09-Feb-2024 01:21                   -
        <a href="u/">u/</a>                                                 11-Nov-2023 23:54                   -
        <a href="v/">v/</a>                                                 11-Nov-2023 23:54                   -
        <a href="w/">w/</a>                                                 11-Nov-2023 23:54                   -
        <a href="x/">x/</a>                                                 13-Dec-2023 04:16                   -
        <a href="y/">y/</a>                                                 11-Nov-2023 23:54                   -
        <a href="z/">z/</a>                                                 11-Nov-2023 23:54                   -
        </pre><hr></body>
        </html>
    

    This needs to be parsed afterwards. One approach could be

      - name: Show Links
        debug:
          msg: "{{ link }}"
        loop: "{{ response.content | split('\n') }}"
        loop_control:
          label: "Link"
        vars:
          link: "{{ item | regex_search('<a href=(.*?)</a>') }}"
        when: link | length > 0
    

    resulting into an output of

    TASK [Show Links] ***************
    ok: [localhost] => (item=Link) =>
      msg: <a href="../">../</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="a/">a/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="b/">b/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="c/">c/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="d/">d/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="e/">e/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="f/">f/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="g/">g/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="h/">h/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="i/">i/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="j/">j/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="k/">k/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="l/">l/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="m/">m/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="n/">n/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="o/">o/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="p/">p/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="q/">q/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="r/">r/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="s/">s/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="t/">t/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="u/">u/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="v/">v/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="w/">w/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="x/">x/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="y/">y/</a>
    ok: [localhost] => (item=Link) =>
      msg: <a href="z/">z/</a>
    

    Thanks To


    If I try like requests.list, I get an error that .list is not a request method.

    Right, that's the case. In Python Requests there is no such method.