Ask: Ansible module to fetch speciffic value from source url

Viewed 34

wondering if i miss something to use url module, I'd like to use Ansible to download file from "https://downloads.wordpress.org/plugin/block-bad-queries.20220517.zip" - somehow that full urlpath available from source url of https://wordpress.org/plugins/block-bad-queries/ in "downloadUrl": "https://downloads.wordpress.org/plugin/block-bad-queries.20220517.zip"

I'm not quite sure if using url module can be used to fetch the value of downloadUrl or perhaps using something else to directly get the json data from that url, seems like i cant just use lookup url and using urlresult.downloadUrl as for finding tag from ansible

Many thanks in advance & appreciate it

1 Answers

It's much easier to get this information from Wordpress API which returns an easy to use json. The following demo playbook

---
- hosts: localhost
  gather_facts: false

  vars:
    wp_plugin: block-bad-queries

  tasks:
    - name: Read plugin info from wordpress api json response
      uri:
        url: "https://api.wordpress.org/plugins/info/1.0/{{ wp_plugin }}.json"
      register: plugin_info

    - name: Display download uri from gathered plugion info
      debug:
        var: plugin_info.json.download_link

    - name: Download the plugin
      uri:
        url: "{{ plugin_info.json.download_link }}"
        dest: /tmp/

Gives

PLAY [localhost] *******************************************************************************************************************************************************************************

TASK [Read plugin info from wordpress api json response] ***************************************************************************************************************************************
ok: [localhost]

TASK [Display download uri from gathered plugion info] *****************************************************************************************************************************************
ok: [localhost] => {
    "plugin_info.json.download_link": "https://downloads.wordpress.org/plugin/block-bad-queries.20220517.zip"
}

TASK [Download the plugin] *********************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP *************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

And simple check after running this playbook shows that the file is there:

$ ls -l /tmp/block-bad-*
-rw-r--r-- 1 user users 82113 sep 14 17:05 /tmp/block-bad-queries.20220517.zip
Related