Ansible-从Github存储库下载最新版本的二进制文件

时间:2018-06-21 10:54:39

标签: url github download automation ansible

使用Ansible,请告知我如何从Github存储库下载最新版本的二进制文件。根据我目前的理解,这些步骤将是: 一种。获取最新版本的URL b。下载版本

我有类似的东西不能提供实际的版本(例如v0.11.53):

 - name: get latest Gogs release
  local_action:
    module: uri
    url: https://github.com/gogits/gogs/releases/latest
    method: GET
    follow_redirects: no
    status_code: 301
    register: release_url

对于b。我有下面的工作,但需要不断更新。而不是版本,我需要在a。:

中设置一个变量
- name: download latest
    become: yes
    become-user: "{{gogs_user}}"
    get_url:
     url: https://github.com/gogs/gogs/releases/download/v0.11.53/linux_amd64.tar.gz
    dest: "/home/{{gogs_user}}/linux_amd64.tar.gz"

谢谢!

4 个答案:

答案 0 :(得分:4)

Github具有一个documented发行版的API。

因此,假设您想获得最新版本的ansible(属于ansible项目)

  • 调用网址https://api.github.com/repos/ansible/ansible/releases/latest
  • 获取这样的json结构
{
  "url": "https://api.github.com/repos/ansible/ansible/releases/5120666",
  "assets_url": "https://api.github.com/repos/ansible/ansible/releases/5120666/assets",
  "upload_url": "https://uploads.github.com/repos/ansible/ansible/releases/5120666/assets{?name,label}",
  "html_url": "https://github.com/ansible/ansible/releases/tag/v2.2.1.0-0.3.rc3",
  "id": 5120666,
  "node_id": "MDc6UmVsZWFzZTUxMjA2NjY=",
  "tag_name": "v2.2.1.0-0.3.rc3",
  "target_commitish": "devel",
  "name": "THESE ARE NOT OUR OFFICIAL RELEASES",
  ...
  },
  "prerelease": false,
  "created_at": "2017-01-09T16:49:01Z",
  "published_at": "2017-01-10T20:09:37Z",
  "assets": [

  ],
  "tarball_url": "https://api.github.com/repos/ansible/ansible/tarball/v2.2.1.0-0.3.rc3",
  "zipball_url": "https://api.github.com/repos/ansible/ansible/zipball/v2.2.1.0-0.3.rc3",
  "body": "For official tarballs go to https://releases.ansible.com\n"
}
  • 获取键tarball_url的值
  • 下载刚刚检索到的密钥的值

在可以完成的Ansible代码中

- hosts: localhost                                                     
  tasks:                                                               

  - uri:                                                               
      url: https://api.github.com/repos/ansible/ansible/releases/latest
      return_content: true                                             
    register: json_reponse                                             

  - get_url:                                                           
      url: "{{ json_reponse.json.tarball_url }}"                       
      dest: ./ansible-latest.tar.gz       

我让您调整适当的参数来回答您的问题:)

答案 1 :(得分:2)

我正在使用以下食谱从GitHub版本下载并提取Linux的最新watchexec二进制文件。

- hosts: localhost                                                     
  tasks:                                                               

  - name: check latest watchexec
    uri:
      url: https://api.github.com/repos/watchexec/watchexec/releases/latest
      return_content: true
    register: watchexec_latest

  - name: "installing watchexec {{ watchexec_latest.json.tag_name }}"
    loop: "{{ watchexec_latest.json.assets }}"
    when: "'x86_64-unknown-linux-musl.tar.xz' in item.name"
    unarchive:
      remote_src: yes
      src: "{{ item.browser_download_url }}"
      dest: "{{ ansible_env.HOME }}/bin/"
      keep_newer: yes
      extra_opts:
      - --strip=1
      - --no-anchored
      - watchexec

tar extra_opts解释了here

每次调用剧本时,它仍会下载二进制文件。作为改进,可以使用set_fact来缓存与解压缩文件相对应的node_id属性。

答案 2 :(得分:0)

我得到了启发并扩展了剧本。 下载后,我将版本号添加到二进制文件并链接到程序。 所以我可以检查下一次运行时版本是否仍然是最新的。否则它会被重新下载和链接。

- hosts: localhost
  become: false
  vars:
    org: watchexec
    repo: watchexec
    filename: x86_64-unknown-linux-gnu.tar.xz
    version: latest
    project_url: https://api.github.com/repos/{{ org }}/{{ repo }}/releases
  tasks:

  - name: check {{ repo }} version
    uri:
      url: "{{ project_url }}/{{ version }}"
      return_content: true
    register: latest_version

  - name: check if {{ repo }}-{{ version }} already there
    stat:
      path: "{{ ansible_env.HOME }}/.local/bin/{{ repo }}-{{ latest_version.json.tag_name }}"
    register: newestbinary

  - name: download and link to version {{ version }}
    block:
      - name: create tempfile
        tempfile:
          state: directory
          suffix: dwnld
        register: tempfolder_1

      - name: "installing {{ repo }} {{ latest_version.json.tag_name }}"
        # idea from: https://stackoverflow.com/a/62672308/886659
        loop: "{{ latest_version.json.assets }}"
        when: "filename|string in item.name"
        unarchive:
          remote_src: yes
          src: "{{ item.browser_download_url }}"
          dest: "{{ tempfolder_1.path }}"
          keep_newer: yes
          extra_opts:
          - --strip=1
          - --no-anchored
          - "{{ repo }}"

      - name: command because no mv available
        command: mv "{{ tempfolder_1.path }}/{{ repo }}" "{{ ansible_env.HOME }}/.local/bin/{{ repo }}-{{ latest_version.json.tag_name }}"
        args:
          creates: "{{ ansible_env.HOME }}/.local/bin/{{ repo }}-{{ latest_version.json.tag_name }}"

      - name: "link {{ repo }}-{{ latest_version.json.tag_name }} -> {{ repo }} "
        file:
          src: "{{ ansible_env.HOME }}/.local/bin/{{ repo }}-{{ latest_version.json.tag_name }}"
          dest: "{{ ansible_env.HOME }}/.local/bin/{{ repo }}"
          state: link
          force: yes
    when: not newestbinary.stat.exists
    always:
      - name: delete {{ tempfolder_1.path|default("tempfolder") }}
        file:
          path: "{{ tempfolder_1.path }}"
          state: absent
        when: tempfolder_1.path is defined
        ignore_errors: true

# vim:ft=yaml.ansible:

here is the file on github

答案 3 :(得分:0)

我遇到了类似的情况,但他们没有使用发行版,所以我找到了这个解决方法 - 感觉比它应该的更hacky,但它获得了最新的标签,并在发行版不存在时使用它来代替发行版.

- name: set headers more location
  set_fact: 
    headers_more_location: /srv/headers-more-nginx-module

- name: git clone headers-more-nginx-module
  git:
    repo: https://github.com/openresty/headers-more-nginx-module
    dest: "{{ headers_more_location }}"
    depth: 1
    update: no

- name: get new tags from remote
  command: "git fetch --tags"
  args:
    chdir: "{{ headers_more_location }}"

- name: get latest tag
  command: "git rev-list --tags --max-count=1"
  args:
    chdir: "{{ headers_more_location }}"
  register: tags_rev_list

- name: get latest tag name
  command: "git describe --tags {{ tags_rev_list.stdout }}"
  args:
    chdir: "{{ headers_more_location }}"
  register: latest_tag

- name: checkout the latest version
  command: "git checkout {{ latest_tag.stdout }}"
  args:
    chdir: "{{ headers_more_location }}"