从页面中提取经过筛选的链接,使用 Ansible。

huangapple go评论51阅读模式
英文:

Extract filtered links from page with Ansible

问题

I can provide the translation of the code part you shared:

- name: 获取标签
  uri:
    url: https://github.com/project/app/tags
    return_content: true
  register: tag_info
  until: tag_info is not failed
  retries: 10
  delay: 5

- name: 获取最新标签
  set_fact:
    release: '{{ (tag_info.content | regex_search(''/project/app/releases/tag/.*'')).split(''/'') | first }}'

- name: 构建下载链接
  set_fact:
    download_url: "https://github.com/project/app/releases/download/{{release}}/my-app.rpm"

Please note that I've translated the code only, as you requested.

英文:

I am extracting a list of tags from a github release page which I then use to determine the link of the release file I need to download.

In a nutshell, I'm currently doing this:

- name: Get tags
  uri:
    url: https://github.com/project/app/tags
    return_content: true
  register: tag_info
  until: tag_info is not failed
  retries: 10
  delay: 5

- name: Get latest tag
  set_fact:
    release: '{{ (tag_info.content | regex_search(''/project/app/releases/tag/.*'')).split(''"'') | first }}'

- name: Build download url
  set_fact:
    download_url: "https://github.com/project/app/releases/download/{{release}}/my-app.rpm"

This all works fine, but I notice that when the project releases a nightly or dev/edge build (which has something like -nightly-xyz or -git-abcd), then ansible (correctly) picks it up. This is fine, but I'd like to know if it's possible to filter out those so that ansible picks the tag that doesn't match "-nightly-xyz" "-git-abcd" -- i.e. prioritise a "stable" release above a dev/nightly release.

I've been hunting around for different questions on SO that might help me assemble the right regex, but could never get ansible to pick it correctly.

Does anyone know how to get Ansible to pick the correct tag, while filtering out specific substrings?

As an example to work with, try https://github.com/helm/helm/tags. This page has tags that also have rc and dev -- let's assume I don't want those, so the ansible play I write should pick v3.11.3

答案1

得分: 3

以下是翻译的内容:

获取发布和标签

    - uri:
        url: 'https://github.com/{{ project }}/tags'
        return_content: true
      register: tag_info

声明变量

    project: helm/helm
    regex: "/{{ project }}/releases/tag/.*"
    releases: "{{ tag_info.content|regex_findall(regex) }}"
    tags_all: "{{ releases|
                  map('split', '\"')|
                  map('first')|
                  map('basename')|unique }}"

结果

  tags_all:
  - v3.12.0-rc.1
  - v3.11.3
  - v3.11.2
  - v3.12.0-dev.1
  - v3.11.1
  - v3.11.0
  - v3.11.0-rc.2
  - v3.11.0-rc.1
  - v3.10.3
  - v3.10.2

问:"我不想要rc和dev标签。"

答:还有其他选项。如果您想要排除仅选择和删除rcdev标签,从列表中选择并删除它们

  tags_dev: "{{ tags_all|select('search', 'dev') }}"
  tags_rc: "{{ tags_all|select('search', 'rc') }}"
  tags: "{{ tags_all|difference(tags_dev)|difference(tags_rc) }}"

结果

tags:
  - v3.11.3
  - v3.11.2
  - v3.11.1
  - v3.11.0
  - v3.10.3
  - v3.10.2

如果您还想排除其他版本,下面的表达式会产生相同的结果

  tags: "{{ tags_all|reject('regex', '^.*-.*$') }}"

补充:完整的测试播放书例子

- hosts: localhost

  vars:
    
    project: helm/helm
    regex: "/{{ project }}/releases/tag/.*"
    releases: "{{ tag_info.content|regex_findall(regex) }}"
    tags_all: "{{ releases|
                  map('split', '\"')|
                  map('first')|
                  map('basename')|unique }}"
    tags1: "{{ tags_all|reject('regex', '^.*-.*$') }}"
    tags_dev: "{{ tags_all|select('search', 'dev') }}"
    tags_rc: "{{ tags_all|select('search', 'rc') }}"
    tags2: "{{ tags_all|difference(tags_dev)|difference(tags_rc) }}"

  tasks:

    - uri:
        url: 'https://github.com/{{ project }}/tags'
        return_content: true
      register: tag_info
    #- debug:
    #    var: tag_info
    - debug:
        var: releases
    - debug:
        var: tags_all
    - debug:
        var: tags_dev
    - debug:
        var: tags_rc

    - debug:
        var: tags1
    - debug:
        var: tags2
英文:

Get the releases and tags

    - uri:
        url: 'https://github.com/{{ project }}/tags'
        return_content: true
      register: tag_info

Declare the variables

    project: helm/helm
    regex: "/{{ project }}/releases/tag/.*"
    releases: "{{ tag_info.content|regex_findall(regex) }}"
    tags_all: "{{ releases|
                  map('split', '\"')|
                  map('first')|
                  map('basename')|unique }}"

gives

  tags_all:
  - v3.12.0-rc.1
  - v3.11.3
  - v3.11.2
  - v3.12.0-dev.1
  - v3.11.1
  - v3.11.0
  - v3.11.0-rc.2
  - v3.11.0-rc.1
  - v3.10.3
  - v3.10.2

Q: "I don't want rc and dev tags."

A: There are more options. If you want to reject rc and dev tags exclusively select and remove them from the list

  tags_dev: "{{ tags_all|select('search', 'dev') }}"
  tags_rc: "{{ tags_all|select('search', 'rc') }}"
  tags: "{{ tags_all|difference(tags_dev)|difference(tags_rc) }}"

gives

tags:
  - v3.11.3
  - v3.11.2
  - v3.11.1
  - v3.11.0
  - v3.10.3
  - v3.10.2

If you want to reject also other flavors the expression below gives the same result

  tags: "{{ tags_all|reject('regex', '^.*-.*$') }}"

<sup>

Example of a complete playbook for testing

- hosts: localhost

  vars:
    
    project: helm/helm
    regex: &quot;/{{ project }}/releases/tag/.*&quot;
    releases: &quot;{{ tag_info.content|regex_findall(regex) }}&quot;
    tags_all: &quot;{{ releases|
                  map(&#39;split&#39;, &#39;\&quot;&#39;)|
                  map(&#39;first&#39;)|
                  map(&#39;basename&#39;)|unique }}&quot;
    tags1: &quot;{{ tags_all|reject(&#39;regex&#39;, &#39;^.*-.*$&#39;) }}&quot;
    tags_dev: &quot;{{ tags_all|select(&#39;search&#39;, &#39;dev&#39;) }}&quot;
    tags_rc: &quot;{{ tags_all|select(&#39;search&#39;, &#39;rc&#39;) }}&quot;
    tags2: &quot;{{ tags_all|difference(tags_dev)|difference(tags_rc) }}&quot;

  tasks:

    - uri:
        url: &#39;https://github.com/{{ project }}/tags&#39;
        return_content: true
      register: tag_info
    #- debug:
    #    var: tag_info
    - debug:
        var: releases
    - debug:
        var: tags_all
    - debug:
        var: tags_dev
    - debug:
        var: tags_rc

    - debug:
        var: tags1
    - debug:
        var: tags2

</sup>

huangapple
  • 本文由 发表于 2023年5月10日 14:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定