我写了一本小剧本,它会随着时间的推移而增加。
首先,我在删除服务器上编写了一个脚本,它允许访问"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剧本运行它,它就会给出“无法解决主机: 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
发布于 2022-08-16 05:36:34
关于
我写了一本小剧本,它会随着时间的推移而增加。
根据给定的注释,您可以简单地从下面的示例开始
---
- 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"
Further文档
建议阅读每个模块文档,并熟悉某些属性和不同的行为。
https://unix.stackexchange.com/questions/713651
复制相似问题