Ansible - Download latest release binary from Github repo

前端 未结 2 768
耶瑟儿~
耶瑟儿~ 2021-02-15 15:10

With Ansible please advise how i could download the latest release binary from Github repository. As per my current understanding the steps would be: a. get URL of latest releas

相关标签:
2条回答
  • 2021-02-15 15:37

    Github has an API to manipulate the release which is documented.

    so imagine you want to get the latest release of ansible (which belong to the project ansible) you would

    • call the url https://api.github.com/repos/ansible/ansible/releases/latest
    • get an json structure like this
    {
      "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"
    }
    
    • get the value of the key tarball_url
    • download the value of the key retrieved just above

    In ansible code that would do

    - 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       
    

    I let you adapt the proper parameters to answer your question :)

    0 讨论(0)
  • 2021-02-15 15:51

    I am using the following recipe to download and extract latest watchexec binary for Linux from GitHub releases.

    - 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 explained here.

    This still downloads the binary every time a playbook is called. As an improvement, it might be possible to use set_fact for caching node_id attribute that corresponds to the unpacked file.

    0 讨论(0)
提交回复
热议问题