Ansible:curl 命令失败

Ansible:curl 命令失败

我写了一本小剧本,它会随着时间的推移而增加。

首先我在删除服务器上编写了一个脚本,该脚本有权访问“https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/”并下载tomcat源,脚本文件的内容是

#!/bin/bash -x
version="9.0.65"
filename="apache-tomcat-"$version".tar.gz"
cd /root/ApacheTomcat/files
curl -fk https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-"$version".tar.gz -o $filename
if [[ -e /root/ApacheTomcat/files/$filename ]]
        then
                echo -e "\nApache Tomcat $version downloaded under $PWD on `hostname -i`"
else
        echo -e "\nProblem occured please check it"
fi

然后我在ansible控制器上写了一个小剧本,内容如下

---                                                         
- name: Download Latest version of Apache Tomcat            
  hosts: 172.16.8.50                                        
  tasks:                                                    
    - name: Downloading Apache on Central Repository Server 
      command: sh /root/ApacheTomcat/apachetomcat.sh        
      register: _check_download_apache_status               
    - name:                                                 
      debug:                                                
        var: _check_download_apache_status                  
...                        

        

如果我在服务器上运行 shell 脚本,它会工作并下载文件,但如果我通过 ansible playbook 运行它,它会给出此错误“无法解析主机:dlcdn.apache.org;未知错误”

ansible-playbook apache-update.yaml -b

PLAY [Download Latest version of Apache Tomcat] *********************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [172.16.8.50]

TASK [Downloading Apache on Central Repository Server] **************************************************************************************************************************************************************************************
changed: [172.16.8.50]

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [172.16.8.50] => {
    "_check_download_apache_status": {
        "changed": true,
        "cmd": [
            "sh",
            "/root/ApacheTomcat/apachetomcat.sh"
        ],
        "delta": "0:00:00.016762",
        "end": "2022-08-15 11:35:11.229642",
        "failed": false,
        "rc": 0,
        "start": "2022-08-15 11:35:11.212880",
        "stderr": "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- -
-:--:-- --:--:--     0curl: (6) Could not resolve host: dlcdn.apache.org; Unknown error",
        "stderr_lines": [
            "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current",
            "                                 Dload  Upload   Total   Spent    Left  Speed",
            "",
            "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: dlcdn.apache.org; Unknown error"
        ],
        "stdout": "\nProblem occured please check it",
        "stdout_lines": [
            "",
            "Problem occured please check it"
        ]
    }
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
172.16.8.50                : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

答案1

关于

我写了一本小剧本,它会随着时间的推移而增加。

根据给定的评论,您可以简单地从一个例子开始,例如

---
- hosts: central_repository_server # or your remote hosts
  become: false
  gather_facts: false
  
  vars:
  
    VERSION: "9.0.65"

  tasks:
  
  - name: Download latest version of Apache Tomcat
    get_url:
      url: "https://dlcdn.apache.org/tomcat/tomcat-9/v{{ VERSION }}/bin/apache-tomcat-{{ VERSION }}.tar.gz"
      dest: "/home/{{ ansible_user }}"
    register: result
    environment:
      http_proxy: "localhost:3128"
      https_proxy: "localhost:3128"

由于我之前没有提供功能齐全且经过测试的解决方案,而只是提供了解决问题的想法和方法,因此请在此处找到一个工作示例。

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    VERSION: "9.0.65"

  tasks:

  - name: Get file using 'uri' module
    uri:
      url: "https://dlcdn.apache.org/tomcat/tomcat-9/v{{ VERSION }}/bin/apache-tomcat-{{ VERSION }}.tar.gz"
      dest: "/home/{{ ansible_user }}"
      method: GET
      status_code: 200,304
      creates: "apache-tomcat-{{ VERSION }}.tar.gz"
    environment:
      http_proxy: "localhost:3128"
      https_proxy: "localhost:3128"

进一步的文档

建议阅读每个模块文档并熟悉某些属性和不同的行为。

相关内容