首页 > 解决方案 > 从另一个文件 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

标签: variablesansible

解决方案


你也可以使用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

参考:https ://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#defining-variables-in-files


推荐阅读