简体   繁体   English

如何执行shell脚本并在ansible中使用结果

[英]How to execute a shell script and use the result in ansible

First of all, I'm still an ansible newbie, so please excuse my ignorance. 首先,我仍然是一名新手,所以请原谅我的无知。

I don't know if that's even possible, but I'm trying to get the URL produced by this command: 我不知道这是否可能,但我正在尝试获取此命令生成的URL:

curl -s https://api.github.com/repos/Radarr/Radarr/releases | grep linux.tar.gz | grep browser_download_url | head -1 | cut -d \" -f 4

and use it in play to download the package: 并在游戏中使用它来下载包:

here is my play: 这是我的游戏:

  - name: download Radarr
    get_url:
      url: "{{ Radarr_exe_url }}" #should be the url from above
      dest: "{{ Radarr_data_path }}"
    become: true
    become_user: "{{ Radarr_user_name }}"
    notify:
      - Restart Radarr service
- name:               Get Radar exe url
  shell:              curl -s https://api.github.com/repos/Radarr/Radarr/releases | grep linux.tar.gz | grep browser_download_url | head -1 | cut -d \" -f 4
  register:           shell_output
- set_fact:
    Radarr_exe_url : "{{ shell_output.stdout }}"

Here is pure Ansible solution, as calling shell commands when there is module available considered bad practice: 这是纯Ansible解决方案,当有模块可用时调用shell命令被认为是不好的做法:

- hosts: localhost
  gather_facts: no
  tasks:
    - uri:
        url: https://api.github.com/repos/Radarr/Radarr/releases
        return_content: yes
        body_format: json
      changed_when: no
      register: radarr_releases

    - set_fact:
        Radarr_exe_url: "{{ radarr_releases.json | json_query('[0].assets[].browser_download_url') | select('search','linux.tar.gz') | first }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM