variables - 从另一个文件 ansible 获取 vars
问题描述
我想知道允许我检索文件内容并使用它在新文件中创建变量的语法
在我的示例中,我有我的 YAML 配置文件,并且我有一个 VARIABLE 文件,但我不知道如何获取我的第一行并将其作为我的第一个文件中的变量
########## config.yml ############
---
- hosts: my-computer
remote_user: toto
tasks:
-vars:
- my-first-vars: {{ THIS IS VARS }}
- name: "config host"
lineinfile:
path: /etc/file.cfg
regexp: '(.*)this_to_change_with_vars(.*)'
line: '{{ my-first-vars }}'
```########### file.vars #########
THIS IS VARS : 10.10.10.10
THIS IS OTHER VARS: 10.10.9.9
解决方案
你也可以使用vars_files
.
例如
- hosts: my-computer
remote_user: toto
vars_files:
- file.vars
tasks:
- name: "config host"
lineinfile:
path: /etc/file.cfg
regexp: '(.*)this_to_change_with_vars(.*)'
line: '{{ my-first-vars }}'
并在您的 file.vars 中直接将您的 var 添加为
my-first-vars: 10.10.10.10
推荐阅读
- amazon-web-services - K8s 节点路由
- google-cloud-dataflow - 避免连接字符串在数据流作业模板中出现
- android - 以编程方式使用 Amazon pinpiont 发送推送通知
- ckeditor - 在 CKEditor 中选择可编辑节点内的文本
- sql-server - 如何在mac上使用docker连接sql server?
- javascript - 以下代码是否容易受到基于 dom 的 xss 的攻击?
- android - 房间查询返回列子集时出错
- php - Woocommerce 从购物车商品计数中删除 free_gift
- c - 从传递给符合 MISRA 规则 17.4 的函数的 void 指针参数访问所有数组元素
- .htaccess - 如何将目录重写为查询字符串