Home > Software engineering >  ansible: grabbing a string from a url
ansible: grabbing a string from a url

Time:09-17

I'm trying to automate installation of node_exporter to latest version as per release page.

I have: https://api.github.com/repos/prometheus/node_exporter/releases/latest as source and want the "tag_name" variable as a result of my action, to be used in checking version is newest.

How do I retrieve the version number ?

my code so far:

    - name: fetch version test
      debug: msg="{{item}}"
      loop: "{{ lookup('url', 'https://api.github.com/repos/prometheus/node_exporter/releases/latest', wantlist=True) }}"
      register: msgtest

with output like :

..
    "msg": {
        "assets": [
            {
                "browser_download_url": "https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.darwin-amd64.tar.gz",
                "content_type": "application/gzip",

...
...
..
            "rocket": 0,
            "total_count": 11,
            "url": "https://api.github.com/repos/prometheus/node_exporter/releases/47430704/reactions"
        },
        "tag_name": "v1.2.2",
        "tarball_url": "https://api.github.com/repos/prometheus/node_exporter/tarball/v1.2.2",
        "target_commitish": "26645363b486e12be40af7ce4fc91e731a33104e",
        "upload_url": "https://uploads.github.com/repos/prometheus/node_exporter/releases/47430704/assets{?name,label}",
        "url": "https://api.github.com/repos/prometheus/node_exporter/releases/47430704",
        "zipball_url": "https://api.github.com/repos/prometheus/node_exporter/zipball/v1.2.2"
    }
}

I don't rightfully understand how to address the results that I get.

CodePudding user response:

I would suggest using set_fact module to save the contents of the URL in a variable, then we can extract the tag_name out of it.

    - name: fetch version test
      set_fact:
        url_read: "{{ lookup('url', 'https://api.github.com/repos/prometheus/node_exporter/releases/latest') }}"
    - name: show tag_name
      debug:
        var: url_read.tag_name

CodePudding user response:

This is my result:

- set_fact:
    version: "{{ lookup('url', 'https://api.github.com/repos/prometheus/node_exporter/releases/latest') }}"

- debug:
    var: version.tag_name

- name: "Set node_exporter version "
  set_fact:
    node_exporter_version: "{{ version.tag_name[1:] }}"

- name: fetch promethous packages
  unarchive:
    src: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-amd64.tar.gz"
    dest: /tmp
    remote_src: yes
  • Related