如何在ansible中使用include_vars [英] how to use include_vars in ansible
问题描述
我已经创建了自己的自定义库,我将自定义库添加到了我的存储库的公共文件夹中.因为我需要动态传递变量.这是一个机密密码,所以我在 ansible 中使用了vault".
I have created my own custom library, I added my custom library in the common folder of my repository. In that I need to pass variables dynamically. It's a confidential password, so I am using "vault" in ansible.
因为我的要求是如何在主机之前在 tasks\main.yml
中传递 include_vars
.
In that my requirement is how to pass include_vars
in the tasks\main.yml
before hosts.
例如:mytasks.yml
e.g: mytasks.yml
- include_vars: sample_vault.yml
- include: sample_tasks.yml
- hosts: localhost
tasks:
name: "free task"
command: ls -a
我的目录结构是这样的:
my directory structure like this:
myfolder
- common
-library
-my file.py
- sample_tasks.yml
- mytasks
-mytasks.yml(my main master playbook file)
-sample_vault.yml (note:i create this using vault for confidential purpose)
- roles
-myrole
在这里,在使用 ansible 执行主机任务之前,我需要使用在 sample_vault.yml
文件中传递的变量来运行 sample_tasks 文件.如果我使用额外的变量意味着密码是可见的,所以我不需要那个.
Here I need to run sample_tasks file using a variables passed in sample_vault.yml
file before I execute the hosts tasks using ansible. If I use extra variable means password is visible so I don't need that.
当我在 tasks/main.yml
文件中使用 include_vars
时,它显示以下错误:
When I use include_vars
in my tasks/main.yml
file, it shows the following error:
错误!'include_vars' 不是 Play 的有效属性
ERROR! 'include_vars' is not a valid attribute for a Play
推荐答案
你不能这样使用include_vars
,它只能在tasks
下使用.
如果 sample_tasks.yml
是一个任务列表,你也不能在剧本级别使用它.请参阅我的其他答案以获取解释.
You can't use include_vars
this way, it's only available for use under tasks
.
If sample_tasks.yml
is a list of tasks, you also can't use it on playbook level. See my other answer for explanation.
您可以像这样使用 vars_files
:
- hosts: localhost
vars_files:
- sample_vault.yml
tasks:
name: "free task"
command: ls -a
或者传递一个文件作为额外的变量:
Or pass a file as extra variables:
ansible-playbook --ask-vault-pass -e @sample_vault.yml myplaybook.yml
这篇关于如何在ansible中使用include_vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!