【问题标题】:ansible wget then exec scripts => get_url equivalentansible wget 然后执行脚本 => get_url 等效
【发布时间】:2016-08-26 04:48:43
【问题描述】:

我一直想知道用“ansible way”(用get_url等)替换以下shell任务的好方法是什么:

- name: Install oh-my-zsh
  shell: wget -qO - https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash -

或

- name: Install nodesource repo
  shell: curl -sL https://deb.nodesource.com/setup_5.x | bash -

【问题讨论】:

    标签: bash curl ansible wget ansible-playbook


    【解决方案1】:

    这本剧本是我想出的。到目前为止,它和我来的一样接近惯用的解决方案,并且似乎是幂等的。

    它将检查命令是否存在(在本例中为starship),如果/当测试失败时,它将下载脚本。

    ---
    - name: Check for Starship command
      command: command -v starship >/dev/null 2>&1
      register: installed
      no_log: true
      ignore_errors: yes
    
    - name: Download Starship installer
      get_url:
        url: https://starship.rs/install.sh
        dest: /tmp/starship-installer.sh
        mode: 'u+rwx'
      when: installed.rc != 0
      register: download
    
    - name: Run the install script
      shell: /tmp/starship-installer.sh
      when: download.changed
    
    - name: Remove the starship-installer.sh
      file:
        path: /tmp/starship-installer.sh
        state: absent
    

    【讨论】:

      【解决方案2】:

      这对我有用:

        - name: Download zsh installer
          get_url: url=https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh dest=/tmp/zsh-installer.sh
      
        - name: Execute the zsh-installer.sh
          shell: /tmp/zsh-installer.sh
      
        - name: Remove the zsh-installer.sh
          file: path=/tmp/zsh-installer.sh state=absent
      

      【讨论】:

      • 'script' 模块将本地脚本传输到目标主机,然后执行它。 'get_url' 下载到目标主机。因此,您需要“shell”或“命令”,而不是“脚本”。
      • 这不是一个解决方案,因为仍然使用 shell 模块。所以它不能解决幂等性问题(总是改变)。这使得剧本更长(两个任务而不是一个)。这也会在 tmp 文件系统中创建一个文件(在这种情况下)。必须有 get_url 或 uri 模块功能用于此目的......希望在未来......
      • 同意draw,这不是解决办法。
      • 脚本必须启动这一事实意味着任务必须在每次迭代中运行,与所涉及的 Ansible 命令/模块无关。在我看来,这是认真处理的。
      【解决方案3】:

      考虑使用get_url 或uri 模块而不是运行curl。

      例如:

      - name: Download setup_8.x script
        get_url: url=https://deb.nodesource.com/setup_8.x dest=/opt mode=755
      - name: Setup Node.js
        command: /opt/setup_8.x
      - name: Install Node.js (JavaScript run-time environment)
        apt: name=nodejs state=present
      

      【讨论】:

        【解决方案4】:

        对我来说,以下语句有效:

         - name: "Execute Script"
           shell: curl -sL https://rpm.nodesource.com/setup_6.x | bash -
        

        【讨论】:

        • 问题是如何避免这种情况并使用 Ansible 本机模块,而不是 shell。
        【解决方案5】:

        注意:“force=yes”,它将始终下载脚本,覆盖旧脚本。 另请注意“changed_when”,您可以根据自己的情况对其进行优化。

          - name: 'Download {{ helm.install_script_url }}'
            environment:
              http_proxy:  '{{proxy_env.http_proxy | default ("") }}'
              https_proxy: '{{proxy_env.https_proxy | default ("") }}'
              no_proxy:    '{{proxy_env.no_proxy | default ("") }}'
            get_url: url={{ helm.install_script_url | default ("") }} dest=/tmp/helm_install_script force=yes mode="0755"
            when: helm.install_script_url is defined
            tags:
            - helm_x
        
          - name: Run {{ helm.install_script_url }}
            environment:
              http_proxy:  '{{proxy_env.http_proxy | default ("") }}'
              https_proxy: '{{proxy_env.https_proxy | default ("") }}'
              no_proxy:    '{{proxy_env.no_proxy | default ("") }}'
            command: "/tmp/helm_install_script"
            register: command_result
            changed_when: "'is up-to-date' not in command_result.stdout"
            when: helm.install_script_url is defined
            args:
              chdir: /tmp/
            tags:
            - helm_x
        

        【讨论】:

          【解决方案6】:

          @RaviTezu 解决方案不起作用,因为您要执行的文件/脚本必须在您执行播放/角色的机器上。

          根据文档here

          path 处的本地脚本将被传输到远程节点然后执行。

          因此,一种方法是在本地下载文件并使用如下任务:

          - name: execute the script.sh
            script: /local/path/to/script.sh
          

          或者你可以这样做:

          - name: download setup_5.x file to tmp dir
            get_url:
              url: https://deb.nodesource.com/setup_5.x
              dest: /tmp/
              mode: 0755
          
          - name: execute setup_5.x script
            shell: setup_5.x
            args:
              chdir: /tmp/
          

          如果您要上传自己的脚本,我会选择第一种方法,第二种方法在您的情况下更有用,因为脚本可能会及时更新,因此您可以确定每次执行它时都会使用最新的脚本。

          【讨论】:

            【解决方案7】:

            也许这个基本的例子可以帮助你开始:

            ---
            - name: Installing Zsh and git
              apt: pkg=zsh,git state=latest
              register: installation
            
            - name: Backing up existing ~/.zshrc
              shell: if [ -f ~/.zshrc ]; then mv ~/.zshrc{,.orig}; fi
              when: installation|success
              sudo: no
            
            - name: Cloning  oh-my-zsh
              git:
                repo=https://github.com/robbyrussell/oh-my-zsh
                dest=~/.oh-my-zsh
              when: installation|success
              register: cloning
              sudo: no
            
            - name: Creating new ~/.zshrc
              copy:
                src=~/.oh-my-zsh/templates/zshrc.zsh-template
                dest=~/.zshrc
              when: cloning|success
              sudo: no
            

            【讨论】:

              猜你喜欢
              • 2015-11-14
              • 2016-02-16
              • 1970-01-01
              • 1970-01-01
              • 2018-11-28
              • 2020-07-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多