我有一个剧本,其中有一个步骤使用get_url下载文件
- name: Download file
get_url:
url: https://website.com/file.sh
dest: /tmp/file.sh
mode: 0777
我不想将硬编码的URL放在任务中。相反,我想把它作为一个常量变量,如下所示
url: https://website.com/file.sh
并在剧本中声明它
- name: Download file
get_url:
url: {{$url}}
dest: /tmp/file.sh
mode: 0777
我不知道是否有可能。
阅读ansible文档,您将在命令行上找到“传递变量”部分,该部分提供了以下示例:
ansible-playbook test.yml --extra-vars "version=1.23.45 other_variable=foo"
这是你如何将变量传递给你的剧本,其他方法是使用Jinja模板,你必须详细阅读它,这也存在于ansible文档中。
1)你可以在剧本中使用vars选项,如下所示
---
- name: Play
hosts: HOST01
vars:
url: https://website.com/file.sh
tasks:
- name: Download file
get_url:
url: {{ url }}
dest: /tmp/file.sh
mode: 0777
...
2)使用ansible提供的'set_fact'模块
tasks:
- name : Setting the variable url
set_fact:
url: https://website.com/file.sh
- name: Download file
get_url:
url: {{ url }}
dest: /tmp/file.sh
mode: 0777