如何在ansible的任务文件中写入变量 [英] How can I write variables inside the tasks file in ansible
问题描述
我有这个 play.yml
---
- hosts: 127.0.0.1
connection: local
sudo: false
tasks:
- include: apache.yml
我的 Apache 看起来像这样:
My Apache look like this:
vars:
url: czxcxz
- name: Download apache
shell: wget {{url}}
这给了我错误.
如果我删除 vars
那么它就可以工作了.但我想在任务中包含变量,以便我可以为不同的任务保留不同的变量.
If I remove vars
then it works. But I want to include the vars inside tasks so that I can keep different vars for different tasks separate.
推荐答案
注意: 如下所述使用 set_fact
将事实/变量设置到任务所在的远程服务器上正在反对.这个事实/变量将在你的剧本的整个持续时间内在后续任务中持续存在.
NOTE: Using set_fact
as described below sets a fact/variable onto the remote servers that the task is running against. This fact/variable will then persist across subsequent tasks for the entire duration of your playbook.
此外,这些事实是不可变的(在剧本的持续时间内),一旦设置就无法更改.
Also, these facts are immutable (for the duration of the playbook), and cannot be changed once set.
在您的任务之前使用 set_fact
来设置 facts ,它似乎可以与变量互换:
Use set_fact
before your task to set facts which seem interchangeable with variables:
- name: Set Apache URL
set_fact:
apache_url: 'http://example.com/apache'
- name: Download Apache
shell: wget {{ apache_url }}
请参阅 http://docs.ansible.com/set_fact_module.html 了解官方说法.
See http://docs.ansible.com/set_fact_module.html for the official word.
这篇关于如何在ansible的任务文件中写入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!