import an variable file to another in ansible

假装没事ソ 提交于 2020-01-24 20:20:13

问题


I am new to ansible. I have 2 variable files one is global and another is environment specific. Here is the global one in global/group_var/all.yaml folder:

  rel:
    deployment:
      webui:
        dockerName: "rel"
        dockerTag: "Dev_{{ travis.build_number }}"

And the specific one is in develop/group_var/all.yaml folder

docker:
  registery: "xxxx"

is there any way I can import global/group_var/all.yaml to develop/group_var/all.yaml

import global/group_var/all.yaml
docker:
  registery: "xxxx"

so develop/group_var/all.yaml becomes:

rel:
  deployment:
    webui:
      dockerName: "rel"
docker:
  registery: "xxxx"
dockerTag: "Dev_1111"

Thanks


回答1:


It's possible to read the global variables into a dictionary. For example

$ cat develop/group_var/all.yaml
global: "{{ lookup('file', 'global/group_var/all.yaml')|from_yaml }}"
docker:
  registery: "xxxx"

the playbook

- hosts: localhost
  tasks:
    - include_vars: develop/group_var/all.yaml
    - debug:
        var: global
    - debug:
        var: docker

gives

    "global": {
        "rel": {
            "deployment": {
                "webui": {
                    "dockerName": "rel"
                }
            }
        }
    }

    "docker": {
        "registery": "xxxx"
    }


来源:https://stackoverflow.com/questions/59794199/import-an-variable-file-to-another-in-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!