我如何在ansible的任务文件中写入变量 [英] How can I write variables inside the tasks file in ansible

查看:103
本文介绍了我如何在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,那么它将起作用.但是我想将vars包含在任务中,以便可以将不同任务的不同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设置事实,这些事实似乎可以与变量互换:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆