简体   繁体   English

ansible playbook 的变量的值如何在另一个 paybook 中使用?

[英]How can the value of a ansible playbook's variable is used in another paybook?

I have two different playbooks named as demo1.yml and demo2.yml .我有两个名为demo1.ymldemo2.yml的不同剧本。

In demo1.yml there is a play, returned values of play is stored in register variable named as results .demo1.yml中有一个 play,play 的返回值存储在名为results的寄存器变量中。 variable results has following:变量结果如下:

"volume_details": {
    "status": "Ready",
    "Id": "123456",
    "name": "test_volume"
}

I want to use the value of results.volume_details.Id and results.volume_details.name in one of the plays of another playbook demo2.yml .我想在另一个剧本demo2.yml的其中一个剧本中使用results.volume_details.Idresults.volume_details.name的值。

Is there any way to do use values of variables from previous playbook to another upcoming playbooks.有什么办法可以将以前剧本中的变量值用于另一个即将到来的剧本。 I want to appreciate your efforts in advance!!!我要提前感谢您的努力!!!

You can create a main playbook and import the others with import_playbook您可以创建一个主剧本并使用import_playbook导入其他剧本

---
  - import_playbook: demo1.yml
  - import_playbook: demo2.yml

If you do so it should be possible to use registered vars from the first playbook in tasks within the second playbook.如果这样做,应该可以在第二个剧本的任务中使用第一个剧本中的注册变量。 With {{ results.volume_details }}使用{{ results.volume_details }}

EDIT: To save a variable to a file you can use the copy module, with the parameter content=.编辑:要将变量保存到文件中,您可以使用复制模块,参数为 content=。

 - copy: 
     content="{{ variable }}" 
     dest=dest_file.yml

To load this file use the include_var module要加载此文件,请使用include_var模块

- name: Include var of dest_file.yml into the new_var 
  include_vars:
    file: dest_file.yml
    name: new_var

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

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