简体   繁体   English

是否include_vars和ansible中的vars的用法相同

[英]Is include_vars and and vars in ansible are same in their usage

I am confused with the usage of include_vars and vars 我对include_varsvars的用法感到困惑

Example: 例:

- name: "Testing include_vars"
  include_vars: "roles/testrole/vars/test.yml"

The above module works, because I think include_vars is a pre-defined module to include variables from a given file. 上面的模块有效,因为我认为include_vars是一个预定义的模块,可以包含给定文件中的变量。

The below doesn't work: 以下无效:

- name: "Defining variables locally"
  vars: 
    testvar : "value1"

It throws the following error: 它引发以下错误:

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path

I understand the error that there is no action. 我了解没有采取任何措施的错误。 But why is there no way to specify variables locally without creating a task. 但是,为什么不创建任务就无法在本地指定变量。 Why a variable has to go with an action - what's the logic or philosophy behind it? 为什么变量必须与动作一起使用-背后的逻辑或哲学是什么?

And while it is also possible to set_facts without another action, as set_facts itself is a module, there is no module to set variables locally. 尽管没有其他动作也可以设置set_facts,因为set_facts本身是一个模块,但是没有模块可以在本地设置变量。 Why? 为什么?

And if you could also add why there can't be multiple actions added on a single task like: set_facts and shell command in same task? 如果您还可以添加一个为什么不能在单个任务上添加多个操作的原因,例如:同一任务中的set_facts和shell命令?

vars is not a module. vars不是模块。 It's the declaration of variables . 这是变量的声明。 To solve the error add a module to the task. 要解决该错误,请在任务中添加一个模块。 For example debug. 例如调试。

- name: "Defining variables locally"
  vars: 
    testvar: "value1"
  debug:
    var: testvar

You might want to review the Intro to Playbooks . 您可能需要查看Playbook简介

To answer your question 回答你的问题

Why there is no module to just define variables? 为什么没有模块只定义变量?

Because of the flexibility. 因为灵活性。 Start planning your architecture with the question "Where does the configuration data come from?". 开始使用问题“配置数据从何而来”计划您的体系结构。 You might want to review the Variable precedence: Where should I put a variable? 您可能需要查看变量优先级:我应该在哪里放置变量? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM